Types generation
Last updated
Was this helpful?
Last updated
Was this helpful?
Types are only enforced between the backend and frontend with GraphQL at runtime (when sending the request). What happens if we change something in the backend and forget to reflect this change in the frontend? It will crash. Let's make sure this doesn't happen.
We are going to generate types from the GraphQL schema with . Then use them on the frontend. If something changes on the backend, Typescript will let us know.
Start the server:
Create a new package:
Generate the types:
To fix Apollo does not support anonymous operations
change:
to
You should now see types.d.ts
in packages/types
. Let's create a package.json
and index.d.ts
for this package:
and now use Lerna to add the types
to web
:
We can now import the variables:
and enforce types with queries and mutations:
yarn add <dependency>
won't work in anymore. Use lerna add dependency> --scope=web
instead.
Later, if we have a React Native app or a queue worker, we will be able to reuse the types from this package.
branch and available on GitHub.