client-preset is generating wrong type definition for operations that use fragments #8639
-
Which packages are impacted by your issue?
Describe the bugHi there, I'm trying to migrate from apollo-client to graphql-code-generator and we found this "bug" When using the following graphql operations: #import "@my-org/web/dist/Graphql/Fragments/userFragment.graphql"
query MeAuthContextAuthQuery {
me {
...UserFragment
}
} # content of @my-org/web/dist/Graphql/Fragments/userFragment.graphql"
fragment UserFragment on MeNode {
id
originalId
email
segmentation
...
} After running the codegen, the The generated type is: export type MeAuthContextAuthQuery = { __typename?: 'Query', me?: (
{ __typename?: 'MeNode' }
& { ' $fragmentRefs'?: { 'UserFragmentFragment': UserFragmentFragment } }
) | null }; Which makes the following code fails: const {data} = useQuery(MeAuthContextAuthDocument)
const email = data?.me?.email; // => email doesn't exists in me object :( Your Example Website or Apphttps://codesandbox.io/s/awesome-lucy-ck9kwo?file=/test.tsx Steps to Reproduce the Bug or IssueI have added a minimal reproduction case using codesanbox.
Expected behaviorThe type generated when using a fragment should contain all the fields defined in the fragment. Screenshots or VideosNo response Platform
"@graphql-codegen/cli": "2.13.12",
"@graphql-codegen/client-preset": "1.1.4",
"@graphql-typed-document-node/core": "3.1.1", Codegen Config FileThis is the real config that we are using in our project. For simplicity, the codesanbox contains a minimal config. const config: CodegenConfig = {
overwrite: true,
ignoreNoDocuments: true,
generates: {
'packages/auth/src/gql-auth/': {
schema: GRAPH_ENDPOINT_AUTH,
documents: 'packages/auth/src/**/*.auth.graphql',
preset: 'client',
plugins: [],
},
'packages/auth/src/gql/': {
schema: GRAPH_ENDPOINT,
documents: [
'packages/auth/src/**/*.graphql',
'packages/auth/node_modules/@my-org/web/dist/**/*.graphql',
'!packages/auth/node_modules/@my-org/web/dist/**/*.*.graphql',
'!packages/auth/src/**/*.auth.graphql',
],
preset: 'client',
plugins: [],
},
},
}; Additional contextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
It’s because with client preset it is doing fragment masking. https://the-guild.dev/graphql/codegen/plugins/presets/gql-tag-operations-preset#fragment-masking |
Beta Was this translation helpful? Give feedback.
-
@saihaj do u know how to disable that? I can't find the docs related to the client preset😢 |
Beta Was this translation helpful? Give feedback.
-
It's already visible in the source: graphql-code-generator/packages/presets/client/src/index.ts Lines 24 to 36 in 4e3cfdc |
Beta Was this translation helpful? Give feedback.
It's already visible in the source:
graphql-code-generator/packages/presets/client/src/index.ts
Lines 24 to 36 in 4e3cfdc