Conversation
…om + inlineFragmentTypes combine eddeee888:oss:issue-verify
|
| const categoryFragmentDef = categoryFragmentDocument.definitions[0]; | ||
| if (categoryFragmentDef.kind !== 'FragmentDefinition') { | ||
| throw new Error('Expected fragment definition'); | ||
| } |
There was a problem hiding this comment.
That's a type-narrowing guard, not new invention — it mirrors the same pattern already used a few lines above it in ts-documents.external-fragments-enum.spec.ts in this same directory.
categoryFragmentDocument.definitions[0] is typed as DefinitionNode, but externalFragments[].node on the plugin config requires FragmentDefinitionNode. Without the runtime kind check, categoryFragmentDef can't be passed as node below without a cast. The check both satisfies TS and fails loudly (rather than misconfiguring the repro silently) if parse() ever returns something unexpected.
Happy to switch it to a cast if you'd rather not have the extra guard — let me know which you prefer.
Generated by Claude Code
| const categoryResult = mergeOutputs([ | ||
| await plugin(schema, [{ document: categoryFragmentDocument }], config, { | ||
| outputFile: './category.generated.ts', | ||
| }), | ||
| ]); |
There was a problem hiding this comment.
Why not put both documents together?
There was a problem hiding this comment.
Intentional, and actually load-bearing for reproducing the bug: this is modeling two separate generated files, the way near-operation-file-preset actually produces them — one plugin() call per source document, where book.generated.ts only learns about the Category fragment through externalFragments (as a fragment defined and compiled elsewhere), not by having both documents in the same document set.
If I merged categoryFragmentDocument and bookFragmentDocument into a single plugin() call, the two fragments would be seen together as one document, _usedSchemaTypes would be computed once for that combined set, and the enum usage from Category would "leak" into the same _usedSchemaTypes — which is exactly the assumption the bug report says is the correct one at the document level (it's used by the document), but wrong at the generated file level. The whole bug only shows up when the file that actually gets written (book.generated.ts) never itself names the enum — which requires it to be generated from a document that only contains the spread, with the enum-selecting fragment external to it.
So the split into two plugin() calls with isExternal: true isn't incidental — it's what makes this reproduce near-operation-file-preset's actual per-file generation instead of a single merged-file case where the bug doesn't occur.
Generated by Claude Code
Description
This is a checkpoint PR: it adds a failing test that proves the bug reported in
#10922 is real. It does not fix anything.
typescript-operations, when configured with bothimportSchemaTypesFromandinlineFragmentTypes: 'combine', emits an unusedimport type * as Types from '...'in generated files whose document only spreads an external fragment (and never names
a schema type itself). Under
noUnusedLocals/noUnusedImportsthis fails theTypeScript build.
The root cause (per the issue) is in
TypeScriptDocumentsVisitor.getExternalSchemaTypeImports()(
packages/plugins/typescript/operations/src/visitor.ts): the import is gated on_usedSchemaTypes, which tracks whether a schema type is used by the document(including through fragments, since it also drives local enum/input generation) —
not whether the generated file itself ends up naming the
Typesnamespace. Thosetwo only diverge when
inlineFragmentTypesis'combine'(or'mask'), becauseonly then does a spread fragment collapse to a bare
FooFragmentreference insteadof inlining the referenced schema types.
Related #10922
Type of change
Screenshots/Sandbox (if appropriate/relevant):
Reporter's reproduction: https://stackblitz.com/edit/gdtmg1pc
How Has This Been Tested?
Added
packages/plugins/typescript/operations/tests/ts-documents.import-schema-types-combine.spec.ts,mirroring the issue's repro: a
Categoryfragment that selects an enum field, and aBookfragment (external-fragment style, own file) that only spreads...Category.With
importSchemaTypesFrom+inlineFragmentTypes: 'combine', the generatedbook.generated.tsoutput should not importTypesat all, since it only everreferences
CategoryFragment.This currently fails as expected:
All 242 other existing tests in the
typescript-operationspackage still pass.ts-documents.import-schema-types-combine.spec.ts(new, failing — this is the point)Test Environment:
@graphql-codegen/typescript-operations: 6.1.6 (as reported)Checklist:
typescript-operationsemits an unused schema-types import when fragment types are referenced #10922 is reproduciblethe actual fix directly on top of this commit
Further comments
This is intentionally left red. Nothing here should be "fixed" by skipping or
loosening the test — the next step is a separate PR that makes
getExternalSchemaTypeImports()(or equivalent) decide the import based on whetherthe generated file itself names a schema type, not just whether the document
uses one.
🤖 Generated with Claude Code
https://claude.ai/code/session_01EefzkxyCDaB4zSdjEuGHou
Generated by Claude Code