diff --git a/.changeset/hungry-ties-press.md b/.changeset/hungry-ties-press.md new file mode 100644 index 00000000000..6693ba7580d --- /dev/null +++ b/.changeset/hungry-ties-press.md @@ -0,0 +1,5 @@ +--- +'@graphql-codegen/cli': patch +--- + +Fix watcher watching project root when schema URL is used diff --git a/packages/graphql-codegen-cli/src/utils/helpers.ts b/packages/graphql-codegen-cli/src/utils/helpers.ts new file mode 100644 index 00000000000..d73f7a2983a --- /dev/null +++ b/packages/graphql-codegen-cli/src/utils/helpers.ts @@ -0,0 +1,8 @@ +export function isURL(str: string): boolean { + try { + const url = new URL(str); + return !!url; + } catch { + return false; + } +} diff --git a/packages/graphql-codegen-cli/src/utils/patterns.ts b/packages/graphql-codegen-cli/src/utils/patterns.ts index f35280b7817..b584a3af58a 100644 --- a/packages/graphql-codegen-cli/src/utils/patterns.ts +++ b/packages/graphql-codegen-cli/src/utils/patterns.ts @@ -4,6 +4,7 @@ import { normalizeInstanceOrArray, Types } from '@graphql-codegen/plugin-helpers import isGlob from 'is-glob'; import mm from 'micromatch'; import { CodegenContext } from '../config.js'; +import { isURL } from './helpers.js'; type NegatedPattern = `!${string}`; @@ -175,7 +176,7 @@ const makePatternsFromSchemas = (schemas: Types.Schema[]): string[] => { for (const s of schemas) { const schema = s as string; - if (isGlob(schema) || isValidPath(schema)) { + if (!isURL(schema) && (isGlob(schema) || isValidPath(schema))) { patterns.push(schema); } } diff --git a/packages/graphql-codegen-cli/tests/watcher.spec.ts b/packages/graphql-codegen-cli/tests/watcher.spec.ts index 565f086623a..6d690042ddb 100644 --- a/packages/graphql-codegen-cli/tests/watcher.spec.ts +++ b/packages/graphql-codegen-cli/tests/watcher.spec.ts @@ -34,6 +34,23 @@ describe('Watch targets', () => { await stopWatching(); }); + test('ignores schema URLs when detecting common prefix directory', async () => { + const { stopWatching, watchDirectory } = await setupMockWatcher({ + filepath: './foo/some-config.ts', + config: { + schema: 'http://localhost/graphql', + generates: { + ['./foo/some-output.ts']: { + documents: ['./foo/bar/*.graphql'], + }, + }, + }, + }); + + expect(watchDirectory).toBe(join(process.cwd(), 'foo')); + await stopWatching(); + }); + test('watches process.cwd() when longest common prefix directory is not accessible', async () => { setupMockFilesystem({ access: async path => {