Skip to content

How to understand a Apollo Client (GraphQL) codebase

Apollo Client fetches GraphQL data and keeps a normalised cache keyed by type and ID. Components declare the data they need in `gql` operations, and updates to the cache flow to every component showing that object.

How to tell it's Apollo Client (GraphQL)

  • @apollo/client and graphql in package.json
  • new ApolloClient({ cache: new InMemoryCache() }) and ApolloProvider
  • gql tagged operations next to components

Where things usually live

src/apollo/client.ts
Client creation, links (auth, error handling), cache config.
src/**/*.graphql or gql blocks
Queries, mutations and fragments.
src/gql/ or __generated__/
Types and hooks from GraphQL Code Generator.
typePolicies in the cache config
How objects are identified and how lists merge.

Conventions, not rules. Real projects rename and move things, so confirm against the repository.

Where to start reading

  1. The client setup and its links
  2. The operation used by the component you care about
  3. Cache typePolicies

How a request moves through the code

  1. 1A component runs useQuery(QUERY, { variables }).
  2. 2Apollo checks the cache according to the fetch policy.
  3. 3If needed, the request goes through links (adding auth headers, handling errors) to the server.
  4. 4The response is normalised into the cache by __typename and ID.
  5. 5Mutations update the cache, and every component reading those objects re-renders.

Questions to ask about the repository

  • List the operations each page runs.
  • How do mutations update the cache: automatically, with update, or by refetching?
  • Which types have custom cache keys or merge functions?

What trips people up

  • Objects without an id or custom key aren't normalised, so updates may not show up elsewhere.
  • Paginated lists need merge functions; without them, new pages replace old ones.