-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Description
Which packages are impacted by your issue?
@graphql-codegen/core
Describe the bug
In our project we have an interface that says that some items should have an ID. This is nice to use in the frontend, where we use that generated interface type in some of our viewmodels.
However when we generate queries, where only a subset of items are wanted we find that such a query is not satisfying the interface, even though the individual fragments, used in the query, do satisfy it. The root cause is that the generator still includes "the rest" of the types that implements the interface, but only with the field __typename.
(see https://github.com/wiegell/gql-bug/blob/main/src/generated/graphql.ts)
Which results in an error like this:
(see https://github.com/wiegell/gql-bug/blob/main/src/problem.ts)
Your Example Website or App
https://github.com/wiegell/gql-bug/
Steps to Reproduce the Bug or Issue
- Create fragments that all satisfy a common interface
- Create a query that only uses those fragments
- See that the generated query does not satisfy the interface. This also i the case for fragments used deeper in a query (not demonstrated in the repro)
Expected behavior
This could be mitigated by filtering out the "empty" types before creating the query union type (or union fragment, when the fragments are used deeper in a query)
Screenshots or Videos
Platform
- OS: macos
- NodeJS: v20.11.1
graphqlversion: 16.12.0@graphql-codegen/*version(s): 6.0.1
Codegen Config File
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
schema: 'graphql/spec.graphql',
documents: 'graphql/queries/**/*.graphql',
generates: {
'./src/generated/graphql.ts': {
plugins: [
'typescript',
'typescript-operations',
'typed-document-node'
]
}
}
};
export default config;
Additional context
No response