dotansimha / dotansimha/graphql-code-generator
Incorrectly reused type from unrelated operation when using multiple inline fragments
- 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/typescript-operations
### Describe the bug
I've run into a bug where the `typescript-operations` plugin will sometimes reuse a type generated from operation A in an unrelated operation B. Depending on the GraphQL schema used for generation, those types may be structurally equivalent (weird, but arguably not a bug) or may reflect distinct types (e.g. can produce type errors for consuming code which counts on the correct types being available between operations).
The clearest situation where this has occurred for me is with a schema like the following, which has a union of types with identical fields:
```graphql
type MemberA {
exampleField: String
}
type MemberB {
exampleField: String
}
# both members of the union have the same field name
union Members = MemberA | MemberB
type Parent {
parentField: Members
}
type Query {
parent: Parent
}
```
...and where multiple operations target these types via inline fragments which are included more than once at a particular type location (whether via non-inline fragments, or in the query, or elsewhere):
```graphql
# operation A:
query UnionTestA {
someParent {
parentField {
... on MemberA {
field
}
}
parentField {
... on MemberA {
field
}
}
}
}
# operation B:
query UnionTestB {
someParent {
parentField {
... on MemberA {
field
}
}
parentField {
... on MemberB {
field
}
}
}
}
```
...with a schema and query like this, `typescript-operations` will generate reproduce previously generated types from operation A as part of the types for operation B, which are then incorrect because in operation A the query doesn't select the same fields for all members of the union.
### Your Example Website or App
https://github.com/dotansimha/graphql-code-generator/compare/master...saranrapjs:graphql-code-generator:fix-fragment-cache-collision
### Steps to Reproduce the Bug or Issue
I've added a failing test to the `typescript-operations` tests on [my fork of the repo](https://github.com/dotansimha/graphql-code-generator/compare/master...saranrapjs:graphql-code-generator:fix-fragment-cache-collision) that illustrates the bug.
(this fork/branch also has a proposed fix, which narrows where the selectionSet cache can be reused — but the contributing guidelines require filing an issue first, which is what I'm doing here!)
### Expected behavior
In this situation, I would expect `typescript-operations` to not reuse types between operations where they are not structurally equivalent.
### Screenshots or Videos
_No response_
### Platform
- OS: macOS
- NodeJS: v24.5.0
- `graphql` version:
- `@graphql-codegen/*` version(s): HEAD
### Codegen Config File
This happens when using the `extractAllFieldsToTypes: true` configuration for the `typescript-operations` plugin.
### Additional context
_No response_
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.