From 9ae630f839ac00a5cd73196e832697845f07b30e Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 25 Sep 2026 06:38:28 +0000 Subject: [PATCH 1/3] [core] fix: skip files without a document and narrow validate/hash types under strict mode (#4683) Under `strict: true`, core had 4 type errors: - `transformedDocuments.flatMap(d => d.document)` is `(DocumentNode | undefined)[]` (TS2345, x2). Filter out files without a `document` with an inline guard instead of passing `undefined` to `validateGraphQlDocuments`. - The cache key concatenated `doc.hash` (`string | null | undefined`) even though `.some(...)` had already ruled non-strings out (TS2769). Collect the hashes once and use an `every` type guard so the narrowing carries through. - `plugin.validate` narrowing was lost inside the `profiler.run` callback (TS2722). Capture it in a local, bound to the plugin. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKWv2u4brsoCsbLcCkiwcZ eddeee888:oss:issue-fix --- .changeset/calm-owls-guard.md | 5 +++++ packages/graphql-codegen-core/src/codegen.ts | 9 +++++---- packages/graphql-codegen-core/src/execute-plugin.ts | 5 +++-- 3 files changed, 13 insertions(+), 6 deletions(-) create mode 100644 .changeset/calm-owls-guard.md diff --git a/.changeset/calm-owls-guard.md b/.changeset/calm-owls-guard.md new file mode 100644 index 00000000000..5b06c7777c5 --- /dev/null +++ b/.changeset/calm-owls-guard.md @@ -0,0 +1,5 @@ +--- +'@graphql-codegen/core': patch +--- + +Skip documents without a `document` AST when validating documents against the schema, instead of passing `undefined` to validation. Generated output from the CLI is unchanged. diff --git a/packages/graphql-codegen-core/src/codegen.ts b/packages/graphql-codegen-core/src/codegen.ts index 09c84562a75..fc48721ebe4 100644 --- a/packages/graphql-codegen-core/src/codegen.ts +++ b/packages/graphql-codegen-core/src/codegen.ts @@ -135,17 +135,18 @@ export async function codegen(options: Types.GenerateOptions): Promise { rule => !ignored.some(ignoredRule => rule.name.startsWith(ignoredRule)), ); const schemaHash = extractHashFromSchema(schemaInstance); + const documentHashes = transformedDocuments.map(d => d.hash); if ( !schemaHash || !options.cache || - transformedDocuments.some(d => typeof d.hash !== 'string') + !documentHashes.every((hash): hash is string => typeof hash === 'string') ) { return Promise.resolve( validateGraphQlDocuments( schemaInstance, [ - ...transformedDocuments.flatMap(d => d.document), + ...transformedDocuments.flatMap(d => (d.document ? [d.document] : [])), ...fragments.flatMap(f => f.document), ], rules, @@ -154,7 +155,7 @@ export async function codegen(options: Types.GenerateOptions): Promise { } const cacheKey = [schemaHash] - .concat(transformedDocuments.map(doc => doc.hash)) + .concat(documentHashes) .concat(JSON.stringify(fragments)) .join(','); @@ -163,7 +164,7 @@ export async function codegen(options: Types.GenerateOptions): Promise { validateGraphQlDocuments( schemaInstance, [ - ...transformedDocuments.flatMap(d => d.document), + ...transformedDocuments.flatMap(d => (d.document ? [d.document] : [])), ...fragments.flatMap(f => f.document), ], rules, diff --git a/packages/graphql-codegen-core/src/execute-plugin.ts b/packages/graphql-codegen-core/src/execute-plugin.ts index b426a1ec8d0..b9d97ca8406 100644 --- a/packages/graphql-codegen-core/src/execute-plugin.ts +++ b/packages/graphql-codegen-core/src/execute-plugin.ts @@ -46,12 +46,13 @@ export async function executePlugin( const pluginContext = options.pluginContext || {}; const profiler = options.profiler ?? createNoopProfiler(); - if (plugin.validate && typeof plugin.validate === 'function') { + const validate = plugin.validate?.bind(plugin); + if (validate && typeof validate === 'function') { try { // FIXME: Sync validate signature with plugin signature await profiler.run( async () => - plugin.validate( + validate( outputSchema, documents, options.config, From ba33da98512a968fb42b37ad6c452e6433762861 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 26 Sep 2026 07:50:08 +0000 Subject: [PATCH 2/3] [core] refactor: drop bind on captured validate (#4683) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKWv2u4brsoCsbLcCkiwcZ --- packages/graphql-codegen-core/src/execute-plugin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/graphql-codegen-core/src/execute-plugin.ts b/packages/graphql-codegen-core/src/execute-plugin.ts index b9d97ca8406..003383856a8 100644 --- a/packages/graphql-codegen-core/src/execute-plugin.ts +++ b/packages/graphql-codegen-core/src/execute-plugin.ts @@ -46,7 +46,7 @@ export async function executePlugin( const pluginContext = options.pluginContext || {}; const profiler = options.profiler ?? createNoopProfiler(); - const validate = plugin.validate?.bind(plugin); + const validate = plugin.validate; if (validate && typeof validate === 'function') { try { // FIXME: Sync validate signature with plugin signature From 5109cc46d1b5058cdc796ba8baf625025d380af2 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 26 Sep 2026 07:53:12 +0000 Subject: [PATCH 3/3] [core] refactor: rely on inferred type predicate for document hashes (#4683) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKWv2u4brsoCsbLcCkiwcZ --- packages/graphql-codegen-core/src/codegen.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/graphql-codegen-core/src/codegen.ts b/packages/graphql-codegen-core/src/codegen.ts index fc48721ebe4..60293b8a23c 100644 --- a/packages/graphql-codegen-core/src/codegen.ts +++ b/packages/graphql-codegen-core/src/codegen.ts @@ -140,7 +140,7 @@ export async function codegen(options: Types.GenerateOptions): Promise { if ( !schemaHash || !options.cache || - !documentHashes.every((hash): hash is string => typeof hash === 'string') + !documentHashes.every(hash => typeof hash === 'string') ) { return Promise.resolve( validateGraphQlDocuments(