This repository demonstrates the bug in @graphql-codegen/typed-document-node@6.1.0+ where fragment types are incorrectly namespaced with Types. prefix when using the import-types preset.
When using typed-document-node@6.1.0+ with the import-types preset, the generated code incorrectly adds a Types. prefix to all DocumentNode result types, including fragments. This causes TypeScript compilation errors because:
- Operation types come from the external
Typesnamespace (correct) - Fragment types are generated locally and should NOT be prefixed (but they are)
{ ...UserFragment } as unknown as DocumentNode<UserFragment, unknown>{ ...UserFragment } as unknown as DocumentNode<Types.UserFragment, unknown>
// ❌ Error: Types namespace has no exported member 'UserFragment'In PR #10456 (commit 655b91d), the typed-document-node plugin added the importOperationTypesFrom feature. The implementation indiscriminately applies the Types. prefix to ALL non-unknown result types without distinguishing between:
- Operations (should be prefixed - they come from external Types namespace)
- Fragments (should NOT be prefixed - they're generated locally)
Source: visitor.ts lines 101-103:
const resultImportPrefix = shouldUseImportPrefix && resultType !== 'unknown' ? 'Types.' : '';-
Install dependencies:
npm install
-
Generate code:
npm run codegen
-
Attempt to compile:
npm run build
Expected result: TypeScript compilation fails with errors like:
error TS2705: Namespace 'Types' has no exported member 'UserProfileFragment'
error TS2705: Namespace 'Types' has no exported member 'UserFragment'
schema.graphql- Sample GraphQL schemaoperations.graphql- Sample queries with fragmentscodegen.ts- Code generation configurationtsconfig.json- TypeScript configuration
After running codegen, examine the generated generated.ts file. You'll see fragment references like:
// WRONG - This is the bug
} as unknown as DocumentNode<Types.UserFragment, unknown>This should be:
// CORRECT
} as unknown as DocumentNode<UserFragment, unknown>