Overview
Our React app is running, but it can't talk to our GraphQL server yet. In this lesson, we will:
- Install Apollo Client 4 and its dependencies
- Configure
ApolloClientwithHttpLinkandInMemoryCache - Wrap the React app in
ApolloProvider
⌨️ Let's play with Apollo Client 4
Let's open up the root of our React app (client/src/index.js). It's time to use Apollo Client to send queries to our GraphQL server!
We first need to install three packages: graphql, @apollo/client, and rxjs.
graphqlprovides the core logic for parsing GraphQL queries.@apollo/clientcontains pretty much everything we need to build our client, including an in-memory cache, local state management, and error handling.rxjsis a peer dependency of Apollo Client 4 that powers its observable implementation.
From the client/ directory, run the following command:
npm install graphql @apollo/client@4 rxjs
With these installed, let's import the symbols we need. Core client APIs come from @apollo/client, while React-specific APIs come from @apollo/client/react:
import { ApolloClient, HttpLink, InMemoryCache } from "@apollo/client";import { ApolloProvider } from "@apollo/client/react";
Let's cover what each of these does!
The ApolloClient class
As you'd expect, ApolloClient is the class that represents Apollo Client itself. We create a new client instance like so:
const client = new ApolloClient({// options go here});
We need to provide a couple of options to the constructor. The first is the link option, which tells Apollo Client how to send GraphQL operations over the network. We'll create an HttpLink that points at our GraphQL server. Our server is running locally at localhost:4000, so the link looks like this:
link: new HttpLink({uri: "http://localhost:4000",}),
Second, every instance of ApolloClient uses an in-memory cache. This enables it to store and reuse query results so it doesn't have to make as many network requests. This makes our app's user experience feel much snappier.
We provide an InMemoryCache instance in the cache option, like so:
cache: new InMemoryCache(),
That's all we need to configure our client! Here's the full call:
const client = new ApolloClient({link: new HttpLink({uri: "http://localhost:4000",}),cache: new InMemoryCache(),});
Our client is ready to use, but how do we make it available to the components in our React app? That's where ApolloProvider component comes in!
The ApolloProvider component
The ApolloProvider component uses React's Context API to make a configured Apollo Client instance available throughout a React component tree. To use it, we wrap our app's top-level components in the ApolloProvider component and pass it our client instance as a prop:
root.render(<React.StrictMode><ApolloProvider client={client}><GlobalStyles /><Pages /></ApolloProvider></React.StrictMode>);
Now all of our pages, containers, and components can access the client via friendly React Hooks thanks to the context API.
Now we relaunch our app, aaaaand, drumroll... nothing changes! We still get our boring, mostly blank layout (well, at least there are little cats on rockets in the background).
Our client is configured and ready to use, but we aren't actually using it yet.
Code review
Practice
Create a new ApolloClient instance. Configure it with an HttpLink that connects to the endpoint https://graphql.org/swapi-graphql and an InMemoryCache. Assign the instance to a variable called client.
Up next
Next, we'll build the query we need to populate our homepage.
Share your questions and comments about this lesson
Your feedback helps us improve! If you're stuck or confused, let us know and we'll help you out. All comments are public and must follow the Apollo Code of Conduct. Note that comments that have been resolved or addressed may be removed.
You'll need a GitHub account to post below. Don't have one? Post in our Odyssey forum instead.