GraphQL vs REST

REST has many endpoints with a response shape set by the server. GraphQL has one endpoint and a response shape set by the client's query. Measured on the…

In the last lesson, the book detail page went from 2 REST requests to 1 GraphQL query. That sounds like a clear win for GraphQL. Is it always? Here we measure both at the Bookshop, including the part where REST comes out ahead.

Both are ways for a client to ask a server for data over HTTP. REST has many addresses whose contents the server decides, GraphQL has one address and the client writes what goes in. Like two ways of shopping. REST is a shelf of ready-made parcels: each shelf holds one parcel with fixed contents, and the same parcel can be stacked at the front of the shop in advance (that's the cache). GraphQL is a made-to-order counter: you say exactly what you want and queue once, but your order is made every time you come.

The difference in one table

REST GraphQL
Address one URL per resource: /books/1, /authors/1 one endpoint: /graphql
Response shape decided by the server decided by the client's query
HTTP method GET, POST, PUT, PATCH, DELETE by action usually POST for everything; queries may use GET
HTTP cache (browser, CDN) GET per URL can be cached as it is POST is not cached
Status code tells the outcome: 200, 404, 422 often 200 even with errors
List of fields read from the docs written in the schema

The example is still the Bookshop from the previous lesson: REST at https://api.bookshop.test/books/1, GraphQL at https://api.bookshop.test/graphql. Every byte count below is the JSON body only, without HTTP headers. RTT (round-trip time) is the time of one trip to the server and back. We use 100 ms here, a fair number on a mobile network.

Measured: the book detail page

The detail page needs the title, price and author name of The Rainbow Troops.

REST GraphQL
Requests 2, one after the other (/books/1 then /authors/1) 1
Body sent 0 bytes (a GET has no body) 59 bytes
Body received 76 + 31 = 107 bytes 96 bytes
Network wait (RTT 100 ms) 200 ms 100 ms

To be honest: for one book the total bytes barely differ, and GraphQL is even bigger if you count the body it sends (59 + 96 = 155 bytes against 107 bytes). What surely wins is the number of round trips: one, not two.

Measured: the book list page

The list page shows the title and author name of three books.

REST has to fetch the book list first, and only then knows which authors to ask for:

GET /books       → 3 books, 234 bytes
GET /authors/1   → 31 bytes
GET /authors/2   → 39 bytes
GET /authors/3   → 31 bytes

GraphQL needs one query, with a 47-byte body:

{ books { title author { name } } }
REST GraphQL
Requests 4 1
Round trips 2 (the 3 author requests go out together) 1
Body received 234 + 31 + 39 + 31 = 335 bytes 225 bytes
Network wait (RTT 100 ms) 200 ms 100 ms

So REST makes 4 requests, GraphQL 1, and GraphQL receives 110 bytes less, because /books also sends id, author_id, price and stock, which the page doesn't use. The gap grows with the data: 20 books by 20 different authors means 21 REST requests, and still 1 for GraphQL.

HTTP caching: where REST wins

The response to GET /books/1 can carry the header Cache-Control: max-age=60. For the next 60 seconds the browser answers from its own cache: a second visit to that detail page is 0 requests. A CDN can also keep the same URL for every visitor.

GraphQL over POST doesn't get that. In practice browsers and CDNs don't store POST responses, so the second visit is still 1 request to the server. There are two ways out:

Status codes: 404 versus 200

In REST, GET /books/99 is answered with 404 Not Found. The status code tells the story. In GraphQL, book(id: 99) gives data: { book: null } plus errors, and the HTTP status depends on the server:

Case Apollo Server GraphQL over HTTP draft
Full data, no errors 200 200
book(id: 99): both data and errors 200 294
Fails to parse: { book(id: 1) { 400 400
Fails validation: the isbn field 400 422
Mutation sent with GET 405 405

The GraphQL over HTTP draft is the specification being written for the HTTP rules. Its code 294 (Partial Success) is a new code for "there is data, but there are errors too". It isn't officially registered yet, so a client that doesn't know it treats it like 200. Many servers in use today answer 200 for that case. The takeaway: in GraphQL, status 200 doesn't always mean success. Read errors in the body.

When REST, when GraphQL

REST fits better when HTTP caching matters and the API is fairly simple: a public catalogue read millions of times through a CDN, CRUD over clear resources, a public API people try out with curl, or uploading and downloading big files.

GraphQL fits better when many screens need different fields from the same data (web, mobile app), the data is nested (book → author → other books), the RTT is high so every round trip hurts, or one entry point sits in front of many services.

Many companies use both: REST for their public API, GraphQL for their own apps.

⚠️ Assumptions that often miss

🤔 What about API versions?

REST usually makes a new version in the URL (/v1/books, /v2/books) when a response's shape changes. GraphQL usually keeps one schema that keeps growing: new fields can be added at any time without breaking old clients, because a client only receives the fields it asks for. Old fields are marked @deprecated, then removed once no client uses them.

TL;DR: REST has many endpoints with a response shape set by the server. GraphQL has one endpoint and a response shape set by the client's query. Measured on the Bookshop: the detail page is 2 REST requests (107 bytes) against 1 query (96 bytes), the list page 4 requests (335 bytes) against 1 query (225 bytes); what surely wins is the number of round trips, not always the bytes. REST wins at HTTP caching: a GET per URL can be kept by the browser and CDN, while GraphQL over POST is still 1 request every time. GraphQL's status codes depend on the server (Apollo: 200 for data with errors, 400 for a query that fails to parse or validate; the GraphQL over HTTP draft: 294 and 422), so always read errors. Pick REST for a simple API that needs HTTP caching, GraphQL for nested data and clients that need different fields.

⚡ The list page: 4 requests against 1

Write:

{ books { title author { name } } }
Guess first: the list page shows three books with their authors' names. How many requests does REST need?

Four: GET /books first, then /authors/1, /authors/2 and /authors/3 sent together. That's 2 round trips, 200 ms. GraphQL needs 1 query, 100 ms.

Meaning: What GraphQL surely wins is the number of round trips. Bytes aren't a sure win, because the query text is sent in the body too.

⚡ HTTP caching: here REST wins

Guess first: the detail page is opened again 20 seconds later, and its REST response carries Cache-Control: max-age=60. How many requests over REST, how many over GraphQL?

REST makes 0 requests: the browser answers from its own cache. GraphQL over POST is still 1 request, because browsers and CDNs don't store POST responses.

Meaning: A GET per URL can be kept by the browser and a CDN. That's a strength of REST that GraphQL doesn't get for free.

⚡ Pick by what you need

Meaning: REST fits better when HTTP caching matters and the API is fairly simple. GraphQL fits better for nested data and screens that need different fields. One more thing: in GraphQL, status 200 doesn't always mean success, so always read errors in the body.

What you will learn