Topic 032 / Core
GraphQL
Chapter 3.4 · API Styles and Serialization
What and why
REST (→ 31) fixes the shape of each resource's response; a client that needs a different combination of fields for different screens either over-fetches unused data or makes several round trips to assemble one view. GraphQL is a query language and runtime where the client specifies exactly which fields it needs, across related resources, in a single request, and the server resolves that query against its data graph. Builds on: → 31.
Diagram
REST: client ──► GET /orders/{id} (fixed shape)
client ──► GET /orders/{id}/items (second round trip for related data)
GraphQL: client ──► one query naming exactly: order { id, total, items { name } }
server ──► resolves each named field, returns exactly that shape
How it works
- The server defines a schema: the types and the fields and relations available on each.
- The client sends a query naming exactly the fields and nested relations it wants.
- The server's resolvers fetch each requested field, potentially from different backing stores or services.
- The server assembles and returns exactly the shape the query asked for — no more, no less.
- A naive resolver that fetches a related field once per parent row, one row at a time, produces the N+1 query problem; it's usually fixed by batching those lookups.
Practical example
(illustrative). A feed-list screen asks only for {id, title}; a
detail screen on the same data asks for {id, title, body, comments{author, text}} — both
come from the same schema and the same endpoint, each getting exactly its own shape in one
request, where REST would need either an overly heavy single endpoint or extra round trips.
When to use it
- Use when: several different client shapes genuinely need overlapping but different slices of the same data (multiple platforms, bandwidth-constrained clients), or aggregating several backend resources into one response is a recurring pain point.
- Avoid when: there's one client shape and REST already fits — GraphQL adds server complexity (schema, resolvers, query-cost limits) and makes HTTP-level caching harder, since one endpoint now serves arbitrarily many different queries.
- Failure scenarios: an unbounded or deeply nested query fans a resolver chain out into an expensive, slow response (the N+1 problem above, or a deliberately adversarial deeply nested query); losing the simple HTTP caching a fixed-shape REST endpoint got for free.
- Simpler alternative: REST, or a backend-for-frontend (→ 148) that shapes one tailored response per client, instead of exposing a general query language to every client.
Practice
(decision kind — judge 3 scenarios; 15–20 min, no lab services). For each of these, decide REST or GraphQL and defend it in one sentence: (1) one client, one data shape; (2) three internal clients needing overlapping but different shapes of the same data; (3) a public API serving many unknown third-party integrators. Done when each scenario has a choice and a one-sentence defense naming the deciding factor.
Key takeaway
GraphQL trades a fixed response shape for a client-specified one, at the cost of server-side resolution complexity and harder HTTP caching. Done when you can: state, for a given set of client shapes, whether that cost is worth paying. Used next in → 148 (BFF as a REST-shaped alternative to the same client-shape problem).
References
None — this topic makes no external claims.
Source directory