Skip to content

Commit 7d7760d

Browse files
authoredFeb 9, 2025··
fix: Don't treat schema URLs as relative file paths for the watcher (#10282)
* fix: Don't treat schema URLs as relative file paths for the watcher * chore: Add changeset
1 parent 978eaa8 commit 7d7760d

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed
 

‎.changeset/hungry-ties-press.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-codegen/cli': patch
3+
---
4+
5+
Fix watcher watching project root when schema URL is used
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function isURL(str: string): boolean {
2+
try {
3+
const url = new URL(str);
4+
return !!url;
5+
} catch {
6+
return false;
7+
}
8+
}

‎packages/graphql-codegen-cli/src/utils/patterns.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { normalizeInstanceOrArray, Types } from '@graphql-codegen/plugin-helpers
44
import isGlob from 'is-glob';
55
import mm from 'micromatch';
66
import { CodegenContext } from '../config.js';
7+
import { isURL } from './helpers.js';
78

89
type NegatedPattern = `!${string}`;
910

@@ -175,7 +176,7 @@ const makePatternsFromSchemas = (schemas: Types.Schema[]): string[] => {
175176

176177
for (const s of schemas) {
177178
const schema = s as string;
178-
if (isGlob(schema) || isValidPath(schema)) {
179+
if (!isURL(schema) && (isGlob(schema) || isValidPath(schema))) {
179180
patterns.push(schema);
180181
}
181182
}

‎packages/graphql-codegen-cli/tests/watcher.spec.ts

+17
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,23 @@ describe('Watch targets', () => {
3434
await stopWatching();
3535
});
3636

37+
test('ignores schema URLs when detecting common prefix directory', async () => {
38+
const { stopWatching, watchDirectory } = await setupMockWatcher({
39+
filepath: './foo/some-config.ts',
40+
config: {
41+
schema: 'http://localhost/graphql',
42+
generates: {
43+
['./foo/some-output.ts']: {
44+
documents: ['./foo/bar/*.graphql'],
45+
},
46+
},
47+
},
48+
});
49+
50+
expect(watchDirectory).toBe(join(process.cwd(), 'foo'));
51+
await stopWatching();
52+
});
53+
3754
test('watches process.cwd() when longest common prefix directory is not accessible', async () => {
3855
setupMockFilesystem({
3956
access: async path => {

0 commit comments

Comments
 (0)
Please sign in to comment.