Skip to content
tezvyn:
Menu

Apollo Client: GraphQL as Reactive State

MediumHow cards are made

Apollo Client turns your GraphQL API into reactive state that components read like a local cache. It shines when multiple views share overlapping data, but teams often footgun by sending one massive query instead of letting the cache normalize fragments.

Why it exists

REST endpoints often return either too much or too little data, forcing frontends to stitch multiple requests together and manage caching and deduplication manually. Apollo Client was built to let a UI ask for exactly the fields it needs in a single request, then keep that data alive as reactive state across the whole application. It solves the N-plus-one request problem and the stale UI problem in one layer, so engineers stop writing ad-hoc state synchronization code.

The mental model

Think of Apollo Client as a smart database that lives inside the browser. Your components do not fetch raw JSON from endpoints; they declare what data shapes they need, and Apollo handles the network, normalization, and broadcast. When one component updates a user name, every other component watching that same user object rerenders automatically, because the cache is a graph of typed objects rather than a pile of isolated endpoint responses.

How it works

You wrap your application in an ApolloProvider and define queries with the useQuery hook or the equivalent framework-specific binding. The client sends the query to your GraphQL server, then writes the returned objects into a flat, normalized cache keyed by type and ID. Subsequent reads hit the cache first, and the cache policy controls when to refetch from the network. Mutations return updated objects that the cache merges in place, triggering reactive updates across every subscribed view without manual event buses.

When to use it

Reach for Apollo Client when your frontend consumes a GraphQL API and multiple components need consistent views of the same entities. It is especially valuable in dashboards, social feeds, or e-commerce carts where a single piece of data like inventory count or a like status appears in many places at once. The normalized cache removes the need for manual store wiring or prop drilling, and built-in loading and error states reduce boilerplate.

When not to use it

Skip it if your API is strictly REST and you cannot add a GraphQL layer, because Apollo is designed around GraphQL documents and schema types. It is also overkill for simple read-only landing pages with no shared state, where a lightweight fetch wrapper adds less bundle weight and fewer concepts. If your team avoids GraphQL for backend reasons, forcing Apollo into the stack just adds indirection and complexity without payoff.

One canonical example

Imagine a project management app where a task card and a sidebar detail panel both display the same assignee avatar and name. With Apollo Client, both components query fragments of the User type. When the sidebar runs a mutation to update the user name, the cache normalizes the response by User ID, and the task card rerenders instantly with the new name without a second network request or any manual store updates.

Interview question

In an Apollo Client app, a sidebar mutation updates a user's name. A task card showing the same user re-renders instantly without a network request. What explains this behavior?

  • a.The mutation opens a WebSocket that pushes the updated name to every listening component
  • b.The task card is configured with refetchQueries to reload its data after any mutation
  • c.Both components share the same local useState hook via React Context
  • d.Apollo Client normalizes the response by User ID and merges it into the shared cache, triggering reactive updates across subscribersCorrect
Why?

Apollo Client stores objects in a flat, normalized cache keyed by type and ID, so when a mutation returns an updated User object the cache merges it in place and automatically re-renders every subscribed view. Distractor D is tempting because refetchQueries is a real Apollo pattern, but it would trigger a fresh network request rather than the instant reactive update described.

Just read this? Test yourself on what you have been reading.

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles