GraphQL vs REST
Compare GraphQL and REST on fetching, versioning, caching, errors and tooling, and learn when each one is the better fit rather than picking a side.
GraphQL vs REST
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- REST exposes many resource URLs and the server fixes each response's shape; GraphQL exposes one endpoint and the client chooses the shape.
- GraphQL removes over-fetching and many chained requests for screens that need data from several resources, at the cost of more work on the server to resolve arbitrary queries.
- HTTP caching works naturally with REST because each resource has a URL; GraphQL usually sends POST to one URL, so caching moves into the client and server layers.
- GraphQL evolves by adding fields and deprecating old ones in a single schema, where REST often versions the URL.
- Neither is better in general; simple resource-style CRUD and public cacheable APIs favour REST, while varied clients with nested data favour GraphQL.
The same screen, two ways
Take the profile page from the last lesson: a book, its author, and the author’s other titles. Here is what a typical REST layout asks of a client. These URLs are illustrative, not a real API.
GET /books/10 -> book fields, plus authorId: 1
GET /authors/1 -> author fields
GET /authors/1/books -> list of the author's books
Three round trips, and the second waits on the first because the client does not know the author id until the book arrives. Each response also carries every field the server chose to include.
In GraphQL it is one request:
{
book(id: "10") {
title
author { name books { title } }
}
}
I ran this against a graphql-js 17.1.0 schema with books and authors (the one the later lessons use). It returned the title, the author’s name, and the list of that author’s books in a single response. The graph does the walking that the client did by hand.
Where they differ
Who shapes the response. In REST the server does, per endpoint. In GraphQL the client does, per query, inside what the schema allows. That helps when many clients (web, iOS, a partner) want different slices of the same data.
How many endpoints. REST has one URL per resource and uses HTTP verbs for intent. GraphQL typically has one URL; the intent lives inside the request. Rules for those requests are in GraphQL over HTTP, and the resource-style alternative is in the API design lesson on REST resource modeling and URI design.
Types. A GraphQL schema is mandatory and machine-readable: every field has a type, and clients can ask the server to describe it (introspection). REST gets this from OpenAPI, which is excellent but separate from the server’s actual behaviour and optional.
Evolution. REST commonly adds /v2/ when a change would break a client. GraphQL’s habit is to never remove fields abruptly: add new ones, mark the old one @deprecated, watch usage, then remove. This works because a client only receives what it names.
Errors. A REST API signals failure with status codes. A GraphQL response can contain both data and errors and often still returns HTTP 200, because one field failing should not discard the rest. You will see how this plays out in errors and partial responses.
What it costs
Flexibility moves work to the server. With REST you know what each endpoint does and can tune it. With GraphQL, a client can send a query shaped in a way you never expected, and the server has to answer it without falling over. That is why the security group has lessons on depth and complexity limits and on caching.
The honest list of friction points:
- Caching. A
GET /books/10can be cached by a browser, a proxy, and a CDN with no extra work. A POST with a query in the body cannot. GraphQL clients cache normalised objects instead, and servers can use GET with persisted queries. - N+1 queries. A nested query naturally resolves one field at a time, which can mean one database call per row unless you batch. That is its own lesson, the N+1 problem.
- File uploads and streaming. Not part of the core language. Possible, but with conventions rather than built-in.
- Simplicity. For three endpoints and one client, a schema, resolvers and tooling are more machinery than the problem needs.
Choosing
A reasonable rule of thumb:
- Reach for REST when the API is mostly create/read/update/delete on well-defined resources, when public caching matters, or when you want the lowest-effort server.
- Reach for GraphQL when you have several clients with different needs, deeply connected data, or a front-end team that keeps asking for “one more field”.
- It is also common to run both. A GraphQL layer in front of existing REST services is an ordinary way to adopt it.
What the video covers
ByteByteGo’s “What Is GraphQL? REST vs. GraphQL” runs a bit over five minutes and was published in November 2022. I could only read its title and the channel’s boilerplate description, no transcript or chapters, so this lesson does not describe its examples. Its title tells you it sets GraphQL beside REST, and at five minutes it is an orientation rather than a deep comparison. IBM Technology’s seven-minute video on the same question is linked as a second view; note that IBM’s channel promotes its own API product in the description.
Where this fits
Now that you know where GraphQL sits, it is time to use it. Next: your first query in GraphiQL.
Check yourself
Why can one GraphQL query replace several REST calls?
Because the query can traverse relationships in the schema, so a book and its author and the author's other books come back in one response instead of three endpoint calls.
Why is HTTP caching harder with GraphQL?
Requests normally go as POST to one URL with the query in the body, so shared caches and CDNs have no distinct URL to key on unless you use GET with persisted queries.
What does GraphQL do instead of versioning URLs?
It adds new fields freely, marks old ones with the deprecated directive, and lets clients move at their own pace, since no client is forced to receive fields it did not ask for.
When is REST the simpler choice?
A small resource-oriented API, such as file storage or webhooks, where responses are uniform, publicly cacheable, and clients have the same needs.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.