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/clientandgraphqlinpackage.jsonnew ApolloClient({ cache: new InMemoryCache() })andApolloProvidergqltagged 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
- The client setup and its links
- The operation used by the component you care about
- Cache
typePolicies
How a request moves through the code
- A component runs
useQuery(QUERY, { variables }). - Apollo checks the cache according to the fetch policy.
- If needed, the request goes through links (adding auth headers, handling errors) to the server.
- The response is normalised into the cache by
__typenameand ID. - Mutations 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
idor custom key aren't normalised, so updates may not show up elsewhere. - Paginated lists need merge functions; without them, new pages replace old ones.