-
Notifications
You must be signed in to change notification settings - Fork 156
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 The closest I've found to a workaround is for cases where the "selected" subtypes that are valid for the query all have some known common property, like // in this example, lots of subtypes include an `id` property typed as `string | null` so we make it the default
type ExtractUsedTypes<Type, Key extends string = 'id', Matches = string | null> = Extract<NonNullable<TYpe>, { [key in Key]?: Matches }>
export type SomeType_fixed = Omit<SomeType, 'propertyWithTooManyTypes' | 'anotherOne'> & {
// this strips out all the junk empty types that don't include `id: string | null`
propertyWithTooManyTypes: ExtractUsedTypes<SomeType['propertyWithTooManyTypes']>
// this strips out all the junk empty types that don't include `numericId: number`
anotherOne: ExtractUsedTypes<SomeType['anotherOne'], 'numericId', number>
} Then your actual code can import the fixed version of the type and you don't need to fill the code with junk guards for scenarios that you know can't happen in your setup. Unfortunately, this adds a lot of brittle overhead to the type definitions, when half the point of using this library is to avoid hardcoding types and make the type follow changes to the schema and queries. |
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: