Skip to content

Commit

Permalink
fix: Don't treat schema URLs as relative file paths for the watcher (#…
Browse files Browse the repository at this point in the history
…10282)

* fix: Don't treat schema URLs as relative file paths for the watcher

* chore: Add changeset
  • Loading branch information
oprypkhantc authored Feb 9, 2025
1 parent 978eaa8 commit 7d7760d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/hungry-ties-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/cli': patch
---

Fix watcher watching project root when schema URL is used
8 changes: 8 additions & 0 deletions packages/graphql-codegen-cli/src/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function isURL(str: string): boolean {
try {
const url = new URL(str);
return !!url;
} catch {
return false;
}
}
3 changes: 2 additions & 1 deletion packages/graphql-codegen-cli/src/utils/patterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;

Expand Down Expand Up @@ -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);
}
}
Expand Down
17 changes: 17 additions & 0 deletions packages/graphql-codegen-cli/tests/watcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down

0 comments on commit 7d7760d

Please # to comment.