Skip to content
Technology

Blog: Generating Code From Your GraphQL Schemas

07/17/2020

Generating code from your GraphQL schemas can help improve your development workflow. By doing so, there's less amount of code you'd need to write, which in turn saves you time and is easier to maintain.

Why should you generate code from your schemas?

Generating code from your GraphQL schemas can help improve your development workflow. By doing so, there's less amount of code you'd need to write, which in turn saves you time and is easier to maintain. You can detect errors when there are changes to the schema, making the codebase less error prone.
 

How can you use your schemas to generate code?

Initial Setup

Start off with creating a basic React app using typescript.
 
Screen Shot 2022-02-16 at 11.34.10 AM
Generating code from your GraphQL schemas can be done using graphql-code-generator. Using this tools, you're able to configure it in many different ways to fit your needs and workflow.
 
For this example, I’ll be using it to generate code for React JS, Typescript, and Apollo.
 
Add graphql and graphql-code-generator packages:
 
Screen Shot 2022-02-16 at 11.35.07 AM
 
Update package.json:
 
Screen Shot 2022-02-16 at 11.35.47 AM
 

Using Github GraphQL API

We’ll need a GraphQL backend server to query against. I’ll be using Github’s GraphQL API. Download Github's schema.docs.graphql file and place it at the root of the project to be used with code generation.
 
Add codegen.yml to the root of the project.
 
Screen Shot 2022-02-16 at 11.37.12 AM
  • schema: can be set to point to wherever you have your GraphQL schema located. It can be set to point to a backend GraphQL server so that whenever you generate code they’re in sync with the server. Here it's set to read locally from our schema file.
  • generates: is set to output code to ./src/generated/graphql.tsx.
Generate code from the Github schema by running:
 
Screen Shot 2022-02-16 at 11.41.35 AM
 
Inspect ./src/generated/graphql.tsx, you’ll see all the types that were created from the schema. In a matter of a few seconds, all of it was created for you saving you time from having to manually create them. You don't have to maintain any typings you'd have to write yourself. Any changes to the schema can be regenerated and reflected back in the code. It also gives you the benefit of detecting errors early on if there was a change to the schema and a mismatch in typings when generating code from the schema.
 

Adding Apollo Client

Now we’ll need a GraphQL client to make queries to the server. We'll use Apollo client for that and need to configure it to generate code to use Apollo.
 
All apollo client and typescript-react-apollo:
 
Screen Shot 2022-02-16 at 11.42.33 AM
 
Update codegen.yml:
 
Screen Shot 2022-02-16 at 11.43.33 AM
  • documents: is set to look for any GraphLQ files within the directory to be used for code generation.
  • withHooks: we’ll also set the configuration to only generate hooks that we can use.
Additional configurations can be found here.
 

Query Github API

Create a graphql file src/queries/CurrentUser.graphql:
 
Screen Shot 2022-02-16 at 11.44.24 AM

 

Then we’ll run yarn generate to generate the hook for the query. Inspect src/generated/graphql.tsx and you should see that it generated this portion of code you can use.

 

Screen Shot 2022-02-16 at 11.45.12 AM

 

Next we’ll need to setup the apollo client.
 
Add src/apolloClient.ts
 
Screen Shot 2022-02-16 at 11.46.15 AM
 
You’ll need a personal access token from GitHub that can be obtained by follow these instructions. Once you have your token, replace <GITHUBACCESSTOKEN> with it.
 
Next we’ll add ApolloProvider in src/index.tsx.
 
Screen Shot 2022-02-16 at 11.47.04 AM
 
That’s it for setting up Apollo. Next we'll update src/App.tsx to use the hook to query our data.
 
Import useCurrentUserQuery from ./generated/graphql and display the data.

 

Screen Shot 2022-02-16 at 11.48.08 AM

Viewed in the browser results in:
 Screen Shot 2022-02-16 at 12.01.17 PM
Screen Shot 2022-02-16 at 12.02.21 PM
 
Next we’ll update src/queries/CurrentUser.graphql with some additional data.
 
Screen Shot 2022-02-16 at 11.51.54 AM
 
Then run yarn generate and refresh the page. The code will reflect the new schema changes and the page should display the new information.
 
Screen Shot 2022-02-16 at 11.54.07 AM

Screen Shot 2022-02-16 at 11.55.39 AM

Screen Shot 2022-02-16 at 11.55.51 AM

Conclusion

Consider generating code from your GraphQL schemas in your next project. It can improve your development workflow by having less code you’d need to write which is less code you’d need to maintain and in turn would be less error prone. You can checkout the full source code here.