In recent weeks and months, we’ve been migrating many of our clients codebases, many at very large scale (over thousand developers on a single codebase), from Apollo Client 2 to Apollo Client 3.
While doing all that work, we’ve improved many of the toolings we are maintaining and created a bunch of new ones.
A lot of those improvements were fed back into GraphQL Codegen, and we are happy to share all those new learnings and features with everyone in the community.
We’ve also found and fixed a lot of memory leaks in upstream Apollo Client, thanks @benjamn for the great corporation!
We hope you would use those new features and improvements to quickly improve your workflow, type-safety and make your migrations easier.
And as usual, we would love to hear your feedback and ideas on how we can further improve the experience of using GraphQL and Typescript in general!
If you are already familiar with GraphQL-Codegen and the plugins it offers, you probably know the
fragment-matcher plugin.
In Apollo-Client v3,
the structure for fragment matcher has been changed, and now it’s called possibleTypes.
The @graphql-codegen/fragment-matcher@2.0.0 now supports Apollo-Client v3 by default, and it
generates type signature and the possibleTypes object automatically based on your GraphQL schema.
Here’s an example of using it with a codegen.yml file:
Then, when you create your Apollo Client cache instance, use the generated variable:
Without this, you’ll have to define and maintain the possibleTypes object manually, which might
lead to an incorrect or invalid setup that might effect Apollo-Client runtime.
Type Policies
If you are using an advanced configuration for your Apollo-Client cache, you can customize the behaviour of your cache.
The configuration you pass to Apollo depends on your GraphQL types and their fields, and instead of having an arbitrary object, you can have a fully-typed signature generated based on your GraphQL schema. That would make it much easier to customize, and you will catch errors in advance! (during build-time, instead during runtime)
You can use @graphql-codegen/typescript-apollo-client-helpers plugin to generate that.
Then, use the generated TypedTypePolicies to type your object:
TypedDocumentNode
Apollo-Client also supports TypedDocumentNode now natively (since v3.2,
you can read more about it here).
You can use it to generate a fully-typed DocumentNode you can use with Apollo-Client, and it will
automatically type your variables and responses.
You can use @graphql-codegen/typed-document-node with the following setup to get that:
Later, in your code, you can just import the generated TypedDocumentNode objects from
typed-document-nodes (based on your GraphQL operations), and it will be automatically typed:
Ready-To-Use Hooks / HOC / Components
One of the most powerful features of GraphQL-Codegen is the ability to generate flexible code based on your GraphQL schema and your GraphQL operations.
We generate TypeScript types, but that’s not all - we can also generate code for you.
You can generate a fully-typed React Hooks:
Then, just use it directly in your components:
Note: This is an alternative for
TypedDocumentNode.
More!
You can also generate Svelte-Apollo, apollo-angular types, Vue-Apollo, Stencil-Apollo and other view layers working with Apollo Client 3…
You can find a list of all available plugins here, and here you can find a list of tips for integrating codegen with your frontend applications.
