Backend and APIs · Question 15 of 50

REST or GraphQL: when would you pick each?

What they're checking

Trade-offs rather than a favourite.

The answer

REST exposes resources at URLs (GET /users/42/orders) with HTTP methods and status codes. It is simple, works with HTTP caching, and suits most CRUD APIs.

GraphQL has one endpoint where the client asks for exactly the fields it needs, across related data, in one round trip. It suits several different clients over rich, connected data, at a cost: HTTP caching gets harder, you need limits on query cost, and resolvers can cause N+1 queries (batch them with a DataLoader). REST by default; GraphQL when over-fetching and round trips are hurting real clients.

The picture

REST:     GET /users/42 ─▶ GET /users/42/orders ─▶ GET /orders/7/items   3 trips
GraphQL:  POST /graphql
          { user(id: 42) { name orders { items { name } } } }           1 trip
The follow-up they'll ask

How do you version each?

Show the answer

REST: a /v2 path or a header. GraphQL usually evolves without versions: add fields, deprecate old ones.

Your progress stays on this device.