Making GraphQL Codegen Work For You: GraphQL Integration with React and TypeScript
In this guide, we’ll be showing you how to use GraphQL alongside React and GraphQL Codegen to create a simple page that can pull data from an API and send emails. We’ll be using Novu as an open source notification system for developers that can send our emails after being passed through a form created within React.
Intro
We’ve all been there: you’ve been coding away in React with Typescript enjoying type-checking until it’s time to time to integrate your application with an API. And then, all of your data types are wrong, ending up with a ton of errors, or just not working at all. Thankfully, integrating APIs doesn’t need to be complicated with GraphQL. It enables you to quickly get an API integrated without the hassle of a standard REST API, thanks to tools like Codegen.
In this article, I will demonstrate the problem that many developers face and how to improve the workflow when integrating your react app with a GraphQL API.
This tutorial assumes that you have been working with React before and want to improve, so you already have your workstation set up.
What We Will Do:
- Bootstrap a new Next.js TypeScript project
- Install Apollo GraphQL and fetch data from a public API
- Showcase why using GraphQL Codegen is a good idea
- Create a Next.js API endpoint for sending emails with data
- Use the Novu.co platform for setting up and sending emails to a given email
If you want to skip the writeup, you can go to GitHub directly and look at the code: https://github.com/ugglr/next-typescript-graphql-integration
Creating an Example Project with React and Next.js
Learning is faster when you get your hands dirty, so let’s jump straight in. We’ll bootstrap a new React project using Next.js with Typescript. Next.js is a full-stack framework for React that allows us to quickly start up a web app.
Run the following command in your terminal to scaffold a new Next.js project with Typescript:
1yarn create next-app --typescript
It will then prompt you for the name of the project. Let’s name it next-typescript-graphql-integration
so we know what we’re working with, and we’ll leave anything else to default values.
While we’re still in the terminal, navigate to our newly created project and run it to make sure that the installation was successful.
1cd next-typescript-graphql-integration
2
3yarn dev
The output will be similar to this:
1yarn dev
2yarn run v1.22.15
3$ next dev
4ready - started server on 0.0.0.0:3000, url: http://localhost:3000
5event - compiled client and server successfully in 1457 ms (165 modules)
From this, we can determine that the page is available at localhost:3000. Opening the browser and checking the page will show the default Next.js screen.
Install Dependencies & Initialize Apollo Client
We are now ready to install some packages. We start by installing one of the most popular GraphQL clients, Apollo Client. Apollo is a tool that enables devs to utilize GraphQL APIs within almost any tech stack and integrates it within your UI. Then, we’ll start doing some fetching from this public Rick & Morty GraphQL API.
1yarn add @apollo/client graphql
When that’s done, let’s open up our code editor and initialize the Apollo Client. When inspecting the folder you can see that it’s a standard bare-bones Next project with the following folder structure:
1root
2--- .next
3--- pages
4--- public
5--- styles
Inside of _app.tsc
we can start initializing our client by following the steps described by the official Apollo Client React docs.
We include the necessary imports and create the client, then include it in our React app via ApolloProvider
. In the end, the file should look something like this:
1import "@/styles/globals.css";
2import type { AppProps } from "next/app";
3
4import { ApolloClient, InMemoryCache, ApolloProvider } from "@apollo/client";
5
6const client = new ApolloClient({
7 uri: "https://rickandmortyapi.com/graphql",
8 cache: new InMemoryCache(),
9});
10
11export default function App({ Component, pageProps }: AppProps) {
12 return (
13 <ApolloProvider {...{ client }}>
14 <Component {...pageProps} />
15 </ApolloProvider>
16 );
17}
This is everything we need to initialize our brand new GraphQL client on the client side. 🚀 Let’s continue with fetching some data the un-safe way.
Fetching Data without Type Checking
Now that we have our project up and running, it’s time to add some data from our API. If we want to get a list of characters from the API we need to run a query like so:
1query {
2 characters {
3 results {
4 id
5 name
6 species
7 }
8 }
9}
The Apollo documentation will tell us to create the query document and then use it together with the useQuery