Setting Up Apollo Client in React
Configure ApolloProvider, create the Apollo client with cache and link chain, and connect to a GraphQL API.
Installing Apollo Client
Install @apollo/client and graphql. The graphql package is a peer dependency that Apollo Client requires for parsing query documents with the gql template tag.
For TypeScript projects, both packages ship their own type definitions, so no @types packages are needed.
Creating the ApolloClient Instance
Instantiate ApolloClient with a cache and a link. The simplest setup: new ApolloClient({ cache: new InMemoryCache(), uri: '/graphql' }). The uri shorthand creates an HttpLink internally.
For more control, build the link chain manually and pass it to the link option instead of uri.
All lessons in this course
- GraphQL Fundamentals for React Developers
- Setting Up Apollo Client in React
- useQuery and useMutation Hooks
- Apollo Cache: Normalization and Updates