TypeScript

REST vs GraphQL

I've built APIs with both REST and GraphQL in production. Neither is universally better. Here's what actually differs.

14 Oct 2023

REST vs GraphQL

I've built APIs with both REST and GraphQL in production. Neither is universally better. Here's what actually differs.

What is GraphQL?

A query language for APIs. Facebook created it in 2012, open-sourced it in 2015. Instead of multiple endpoints, you have one endpoint and the client describes exactly what data it wants.

Where GraphQL wins

Data fetching. REST often requires multiple round trips. Get the user from /users/1, then their posts from /users/1/posts, then comments from /posts/5/comments. GraphQL fetches all of that in a single request.

No over-fetching or under-fetching. With REST, endpoints return fixed shapes. You get fields you don't need, or you don't get fields you do. GraphQL lets the client specify exactly which fields it wants.

No versioning. Add new fields without breaking existing clients. Old queries keep working. REST usually needs /v1/, /v2/ when the shape changes.

Where REST wins

Caching. REST works naturally with HTTP caching — CDNs, browser caches, ETags. GraphQL sends POST requests to a single endpoint, which makes HTTP-level caching much harder. You need a separate caching layer (Apollo Cache, for example).

Error handling. REST uses HTTP status codes. A 404 means "not found." A 500 means "server error." GraphQL always returns 200 OK, even when things go wrong. Errors are buried in the response body. This makes monitoring and alerting more complex.

Simplicity. REST is easier to understand, easier to debug with curl, and most teams already know it. GraphQL has a steeper learning curve and requires more tooling.

The trade-off

GraphQL is powerful for complex, data-rich frontends with many different views — dashboards, mobile apps, SPAs. REST is simpler and better for straightforward CRUD APIs, public APIs, and teams that don't want to maintain a schema.

Pick based on your problem, not the hype.

Keep reading