REST vs GraphQL
REST and GraphQL answer the same question: how does a client ask a server for data? REST exposes a fixed set of resource URLs with server-chosen response shapes. GraphQL exposes one endpoint and a typed graph the client queries. Every trade-off follows from who controls the response shape.
The 60-second answer
Section titled “The 60-second answer”REST models the domain as resources addressed by URLs, uses HTTP methods for intent, and leans on HTTP caching. GraphQL exposes a single endpoint with a typed schema; clients get exactly the fields they ask for, which suits heterogeneous clients but makes caching and authorisation harder and introduces the N+1 problem. I would choose REST for public, cacheable, resource-shaped APIs and GraphQL when many clients need different shapes over the same graph and over-fetching is a real cost.
REST constraints
Section titled “REST constraints”Fielding’s constraints, and why each exists:
- Client–server — UI and data store evolve independently.
- Stateless — each request carries everything needed; any request can go to any server.
- Cacheable — responses declare freshness, so intermediaries can serve them.
- Uniform interface — resources identified by URLs, manipulated through representations, self-descriptive messages, hypermedia as the engine of state (HATEOAS).
- Layered system — the client cannot tell whether it talks to the origin or a proxy.
- Code on demand (optional) — servers may ship executable code.
Nobody implements HATEOAS. Saying “we are not fully RESTful, we are resource-oriented HTTP APIs” is more accurate than pretending, and it is fine as long as you know why: HATEOAS decouples clients from URL structure, at the cost of payload size and client complexity.
Resource design
Section titled “Resource design”GET /v1/users listPOST /v1/users createGET /v1/users/42 readPATCH /v1/users/42 partial updateDELETE /v1/users/42 deleteGET /v1/users/42/orders sub-collectionPOST /v1/orders/8891/cancel action that is not CRUD- Nouns, plural, lowercase, hyphenated:
/payment-methods, not/getPaymentMethod. - Filter, sort and paginate with query parameters, not path segments.
- Nest at most one level; beyond that, promote to a top-level resource with a filter.
- Non-CRUD actions become a sub-resource (
/cancel,/publish), not a verb on the noun. - Return the created or updated representation, not just an id, to save a round trip.
- Return
404for “does not exist or you may not see it” on others’ resources, to avoid leaking existence — see Web Security.
GraphQL in practice
Section titled “GraphQL in practice”type Query { user(id: ID!): User }
type User { id: ID! name: String! orders(first: Int = 20, after: String): OrderConnection!}
type Order { id: ID! totalCents: Int! placedAt: DateTime! }A resolver backs each field, and the client picks the fields:
query { user(id: "42") { name orders(first: 5) { nodes { id totalCents } } } }Three structural problems appear immediately.
N+1. Resolving orders per user issues one query per user; 100 users becomes 101 queries. Fix
with a batching loader that collects keys within a tick and issues one IN (...):
const orderLoader = new DataLoader(async (userIds) => { const rows = await db.query( 'SELECT id, user_id, total_cents FROM orders WHERE user_id = ANY($1)', [userIds]); const byUser = new Map(); for (const row of rows) { if (!byUser.has(row.user_id)) byUser.set(row.user_id, []); byUser.get(row.user_id).push(row); } return userIds.map((id) => byUser.get(id) ?? []);});The same problem exists in REST when a client loops over a list endpoint — it is just visible in the client’s network tab rather than the server’s logs.
Authorisation per field. REST authorises per route; GraphQL must authorise per field and per node, because any field is reachable from any entry point. Check ownership in the resolver or a policy layer, never in transport middleware.
Cost control. A client can request a deep, wide query. Defend with depth limits, complexity scoring, mandatory pagination on list fields, timeouts, and persisted queries (only pre-approved documents) in production.
Caching. HTTP caching does not work for POST /graphql. You get it through persisted queries
(GET with a hash), CDN caching of those, and client-side normalised caches keyed by entity id.
When each fits
Section titled “When each fits”| Situation | Better fit | Why |
|---|---|---|
| Public API, third parties, cacheable reads | REST | HTTP caching, simple tooling, predictable URLs |
| Many clients needing different fields | GraphQL | One schema, no over-fetching |
| Rapid UI iteration over a stable graph | GraphQL | Client changes need no server change |
| File uploads, binary, streaming | REST | GraphQL needs a separate upload spec |
| Service-to-service, strict contracts | gRPC | Binary protobuf, HTTP/2, generated clients |
| Simple CRUD, small team | REST | Least machinery |
The honest arguments for GraphQL are over-fetching and under-fetching: mobile clients save bytes and frontends stop needing three sequential round trips. The honest arguments against are operational: caching, rate limiting per query cost rather than per request, monitoring one endpoint (per-operation metrics become mandatory), and N+1 risk.
gRPC, briefly
Section titled “gRPC, briefly”Protocol Buffers over HTTP/2 with code generation from a .proto file, giving typed clients in many
languages. Unary plus three streaming modes (client, server, bidirectional).
- Smaller and faster to serialise than JSON.
- Field numbers are the contract; never reuse one.
- Not browser-native without a proxy (gRPC-Web), so usually internal.
- Errors are a fixed set of status codes, not free-form JSON.
Choose it for internal service-to-service calls where latency and schema discipline matter, not for
a public API that should be callable with curl.
Follow-up questions
Section titled “Follow-up questions”- How would you rate limit a GraphQL endpoint when one request can cost 1,000 field resolutions?
- Where does authorisation belong in a GraphQL server, and why is per-route middleware wrong?
- How do you cache a GraphQL response at the CDN?
- What breaks if you paginate with
offsetin a feed that receives new items constantly? - How do you version a GraphQL schema without breaking clients?
Common traps
Section titled “Common traps”- “GraphQL replaces REST.” It replaces shape negotiation. You still need HTTP, pagination, auth and caching.
- “GraphQL has no N+1 because it is one request.” One request can be hundreds of queries; the N+1 moves to the resolver layer.
- “GraphQL is always faster.” It reduces payload and round trips, and can increase server CPU.
- “REST means JSON over HTTP.” REST is an architectural style; JSON is a representation.
- “Versioning in the URL is bad practice.” It is the most operable option for public APIs.