Skip to content

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.

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.

Fielding’s constraints, and why each exists:

  1. Client–server — UI and data store evolve independently.
  2. Stateless — each request carries everything needed; any request can go to any server.
  3. Cacheable — responses declare freshness, so intermediaries can serve them.
  4. Uniform interface — resources identified by URLs, manipulated through representations, self-descriptive messages, hypermedia as the engine of state (HATEOAS).
  5. Layered system — the client cannot tell whether it talks to the origin or a proxy.
  6. 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.

GET /v1/users list
POST /v1/users create
GET /v1/users/42 read
PATCH /v1/users/42 partial update
DELETE /v1/users/42 delete
GET /v1/users/42/orders sub-collection
POST /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 404 for “does not exist or you may not see it” on others’ resources, to avoid leaking existence — see Web Security.
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 (...):

loaders.js
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.

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.

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.

  1. How would you rate limit a GraphQL endpoint when one request can cost 1,000 field resolutions?
  2. Where does authorisation belong in a GraphQL server, and why is per-route middleware wrong?
  3. How do you cache a GraphQL response at the CDN?
  4. What breaks if you paginate with offset in a feed that receives new items constantly?
  5. How do you version a GraphQL schema without breaking clients?
  • “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.