Setting up Apollo Client and making our first query
Before we can get started making GraphQL queries on the frontend, we first need to set up Apollo Client. Apollo Client is the frontend counterpart to Apollo Server, which we have already been using on the backend. While it is not required to use Apollo Client (we could also simply make a POST
request to the /graphql
endpoint), Apollo Client makes interacting with GraphQL much easier and more convenient. It also includes additional features, such as caching, out of the box.
Follow these steps to set up Apollo Client:
- Copy the existing
ch11
folder to a newch12
folder, as follows:$ cp -R ch11 ch12
- Open the
ch12
folder in VS Code. - Install the
@apollo/client
andgraphql
dependencies:$ npm install @apollo/client@3.9.5 graphql@16.8.1
- Edit
.env
and add a new environment variable, pointing to the endpoint for our GraphQL server:VITE_GRAPHQL_URL="http://localhost:3001/graphql"
- Edit
src/App.jsx
and importApolloClient...