Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading