-
Notifications
You must be signed in to change notification settings - Fork 162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Codegen generates unused types from the schema #724
Comments
I have the same issue, and I found this discussion: dotansimha/graphql-code-generator#5567 Unfortunately it looks like this is considered a feature not a bug 😢 because "technically" a hypothetical server could return an empty object derived from any non-selected subtype of the type named in the schema. We know our actual server and client logic wouldn't allow this in real-life data, but the logic that prevents it exists outside of the schema and query that the generator can see. I feel like it's common enough for GraphQL servers and clients to be configured to only allow data linkages that match There aren't many good workarounds either. If TypeScript had anything like an |
I found a good-enough workaround. type IgnoreTypenameOnlyObjects<T> =
T extends { __typename?: Maybe<string>; } // If any key is __typename...
? keyof T extends "__typename" // ...and, EVERY key is __typename...
? never // ...discard this subtype.
: T // Keep any object with __typename and any other key
: T; // Keep everything else, e.g. non-objects or objects without __typename
export type DeepIgnoreTypenameOnlyObjects<Item> = {
[K in keyof Item]: DeepIgnoreTypenameOnlyObjects<
IgnoreTypenameOnlyObjects<Item[K]>
>;
}; Then wrap your complex union type in I recommend also wrapping it in a type resolver utility (like https://stackoverflow.com/a/69288824/568458) so the type is readable in IDEs else it becomes very unwieldy and indirect. |
Which packages are impacted by your issue?
No response
Describe the bug
I'm using codegen with typescript-operations and typescript-react-apollo to generate types for a graphql query. Here is the config:
I have the following query in a file:
The issue is that using the above config, I get following types for footernavigation_footerNavigation_Entry:
Where
FooterNavigationQuery_entries_about_about_Entry_descendants
is an array of all possible types here from schema (which is huge), but in the query used for this component, I'm interested only in the last one from the screenshot:FooterNavigationQuery_entries_about_about_Entry_descendants_footerNavigation_footerNavigation_Entry
I don't understand why I'm getting unused types generated here, even if I've tried with related plugin options. In order to use this, I must type thin in a component by asking for __typename and again checking the type:
Which seems unnecessary since the query itself is querying just the footer-related fields. If I remove __typename in a query, I'll still get multiple types, just without that field. By using typename I can at least target the correct type, but it seems like unnecessary work. Thanks!
Your Example Website or App
localhost
Steps to Reproduce the Bug or Issue
/
Expected behavior
I expect to get single type for the provided field in the query.
Screenshots or Videos
No response
Platform
graphql
v16.8.1@graphql-codegen/*
5.0.2Codegen Config File
/* eslint-disable import/no-extraneous-dependencies */
import { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
schema: {
'http://localhost:8080/api': {
headers: {
Authorization: 'Bearer xxx',
},
},
},
documents: ['./**/*.graphql'],
debug: true,
ignoreNoDocuments: true,
generates: {
'modules/gql/': {
plugins: ['typescript-operations', 'typescript-react-apollo'],
preset: 'near-operation-file',
presetConfig: {
baseTypesPath: 'types.ts',
importAllFragmentsFrom: 'types.ts',
},
config: {
useTypeImports: true,
avoidOptionals: true,
extractAllFieldsToTypes: true,
skipTypename: true,
mergeFragmentTypes: true,
withHooks: false,
flattenGeneratedTypes: true,
flattenGeneratedTypesIncludeFragments: true,
dedupeFragments: true,
},
},
},
};
export default config;
Additional context
No response
The text was updated successfully, but these errors were encountered: