If you are using GraphQL in your frontend(or even backend) applications I hope you are not creating all the types yourself. Thanks to the built-in schema introspection functionality, tooling can do all the heavy-lifting for you.
For frontend apps I typically use the GraphQL Code Generator created and maintained by The Guild. I don’t want to use this post to explain how to use the GraphQL Code Generator; there are multiple guides available. For example here is the one for Angular. Instead I want to focus on how you can manipulate the code generation.
By default, the GraphQL Code Generator will generate types for your complete schema AND specific operation types based on the provided GraphQL queries/mutations/subscriptions.
How does this looks like?
Here is the codegen.ts file we are using:
And here is on the GraphQL query files we are using:
If we execute the codegeneration, we get types for the full schema:
AND types for the specific operation:
Generate only operation types
If you want to change this and only generate operation types, you can use the onlyOperationTypes
config option:
This will omit the type files and only generate the specific types used in the query: