How to exclude empty types when using fragments? #5567
-
I'm building an app with a Craft CMS GraphQL API and Next.js and have defined this query: query GetHomepage($language: String!) {
homepage: entry(uri: "__home__", site: [$language]) {
... on homepage_homepage_Entry {
id
title
pageContent {
... on pageContent_wysiwyg_BlockType {
wysiwyg
}
}
}
}
} Looking at this query, it's clear that the type can only be export type GetHomepageQuery = {
__typename ? : 'Query',
homepage: Maybe <{
__typename ? : 'pages_pages_Entry'
} | {
__typename ? : 'homepage_homepage_Entry',
id: Maybe < string > ,
title: Maybe < string > ,
pageContent: Maybe < Array < Maybe < {
__typename ? : 'pageContent_wysiwyg_BlockType',
wysiwyg: Maybe < string >
} >>>
} >
}; So I'm not able to access my data just like this: Is there anything that I can configure to remove |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
I'm not sure that |
Beta Was this translation helpful? Give feedback.
-
I am upgrading the codegen version in my project from 1.1.3 to 2 and have encountered a lot of typescript compatibility issues. My problem is similar to this discussion. I found that the v1.1.3 can exclude these empty types, but the v2 can't. Is there any way now to exclude these empty types? Here is the code demonstration. Thank you very much for this tool. |
Beta Was this translation helpful? Give feedback.
I'm not sure that
homepage_homepage_Entry
is the only type possible. How doesentry
field is defined?Basically, codegen tried to reflect the whole possible response from the server, based on the schema. If there is a need for a fragment - it means that you might have interface/union, and your schema might return other types as well.