Skip to content

[typed-document-node] generated Fragment types are incorrectly prefixed with Types. namespace on 6.1.0+ #10528

@pachuka

Description

@pachuka

Which packages are impacted by your issue?

@graphql-codegen/typed-document-node

Describe the bug

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:

  1. Operation types come from the external Types namespace (correct)
  2. Fragment types are generated locally and should NOT be prefixed (but they are)

Root Cause

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.' : '';

Your Example Website or App

https://github.com/pachuka/-graphql-codegen-typed-document-node-fragment-namespace-bug

Steps to Reproduce the Bug or Issue

  1. Pull down the reproduction repo
  2. npm install
  3. npm run codegen
  4. npm run build // should fail
  5. update package.json to downgrade @graphql-codegen/typed-document-node to 6.0.2
  6. repeat steps 2-4 (build should pass)

Expected behavior

Fragments should not be prefixed with the Types namespace when used w/import-types preset.

Screenshots or Videos

No response

Platform

  • OS: macOS
  • NodeJS: 24.7.0
  • graphql version: 16.12.0
  • @graphql-codegen/typed-document-node version(s): 6.1.0+

Codegen Config File

overwrite: true
schema: schema.graphql
documents: operations.graphql
generates:
  generated.ts:
    preset: import-types
    presetConfig:
      typesPath: ./types
    plugins:
      - typescript
      - typescript-operations
      - typed-document-node
    config:
      scalars:
        DateTime: Date
      namingConvention:
        typeNames: change-case#pascalCase
        enumValues: change-case#upperCase
  types.ts:
    plugins:
      - typescript
    config:
      scalars:
        DateTime: Date

Additional context

No response

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions