dotansimha / dotansimha/graphql-code-generator
client-preset with nested fragment for union type may generates fragment types that use undefined fragment types
- Dominant language
- TypeScript
- Stars
- 11.3k
- Forks
- 1.4k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 23
Description
### Which packages are impacted by your issue?
@graphql-codegen/client-preset
### Describe the bug
When using client preset, codegen generates unexpected invalid type definitions for nested fragment reference (*) for union type. In particular, fragment type definitions for unused (non-referenced) types of union are not emitted, although they are used from another fragment type definitions.
(*) The fragments are such as:
```ts
// type User = Human | Robot
const HumanNameFragment = graphql(`
fragment HumanName_User on User {
... on Human {
name
}
}
`);
const TaskUserFragment = graphql(`
fragment TaskUser_User on User {
... on Human {
id
}
... on Robot {
id
}
...HumanName_User
}
`);
const TaskDetailFragment = graphql(`
fragment TaskDetail_Task on Task {
id
assignee {
...TaskUser_User
}
}
`);
const GetTaskDocument = graphql(`
query GetTask($id: String!) {
task(id: $id) {
...TaskDetail_Task
}
}
`);
```
### Your Example Website or App
https://github.com/jet2jet/graphql-code-generator-issue-fragment-type-undefined
### Steps to Reproduce the Bug or Issue
1. Clone the repository above
2. Run `npm ci` and `npm run codegen`
3. Run `npm run typecheck` to check type definitions
### Expected behavior
No error is reported for `npm run typecheck`
### Screenshots or Videos
_No response_
### Platform
- OS: Linux (WSL)
- NodeJS: 24.16.0
- `graphql` version: 16.14.2
- `@graphql-codegen/cli` version(s): 7.2.0
- `@graphql-codegen/client-preset` version(s): 6.1.3
### Codegen Config File
```ts
import { CodegenConfig } from "@graphql-codegen/cli";
const config: CodegenConfig = {
schema: "schema.graphql",
generates: {
'src/gql/': {
documents: ['src/**/*.mts', '!src/gql/**/*'],
preset: 'client',
config: {
nonOptionalTypename: process.env.WITH_TYPENAME === '1', // for test
},
},
},
};
export default config;
```
### Additional context
If `nonOptionalTypename` is set to true, all fragment types are emitted correctly, which results no error.
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the reproduction repository's schema.graphql, src/**/*.mts documents, and src/gql/ generated output. Run npm ci, npm run codegen, and npm run typecheck, then inspect the generated fragment type definitions for the nested union fragments. Done means the existing reproduction generates definitions for all referenced fragment types and npm run typecheck reports no errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- graphql, typescript
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100