GraphQL vs. REST
Compare GraphQL and REST on data fetching, caching, errors, security and effort, and learn which fits your project.
Key takeaways
- In REST the server fixes the response shape; in GraphQL the client chooses fields within a schema the server defines.
- GraphQL reduces over-fetching and chains of requests, but moves work and risk onto the server.
- REST benefits from standard HTTP caching with little effort, while GraphQL usually needs extra caching design.
- GraphQL servers must limit query depth and cost and check permissions on every field that needs them.
- For a small service with one client, REST is often the cheaper choice.
REST gives you many addresses, each returning a shape the server decided in advance. GraphQL usually gives you one address, and the client sends a query that names exactly which fields it wants. GraphQL tends to help when many different screens need different mixtures of related data. REST tends to be simpler when your resources are straightforward and you want ordinary web caching and tooling.
Neither replaces the other. Plenty of organizations run both: REST for public integrations and simple services, GraphQL as a layer that gathers data for their own apps.
How each approach looks
With REST, described fully in REST APIs explained, each resource has its own URL. To show an author's page with their recent posts and comment counts, a client might call:
GET /authors/12
GET /authors/12/posts?limit=3
GET /posts/301/comments
GET /posts/302/comments
GET /posts/303/comments With GraphQL, the client sends one query describing the tree of data:
query {
author(id: 12) {
name
posts(first: 3) {
title
commentCount
}
}
} The response mirrors the query's shape, under a top-level data key:
{"data": {"author": {"name": "R. Okafor", "posts": [
{"title": "Cable basics", "commentCount": 4}
]}}} GraphQL was developed at Facebook and released publicly in 2015. It is defined by a specification, and server and client libraries exist for most popular languages.
The schema is the contract
Every GraphQL API has a typed schema that lists object types, their fields and the operations available. There are three kinds of operation: queries read data, mutations change it, and subscriptions deliver a stream of updates, commonly over a long-lived connection.
Behind each field is a function called a resolver that knows how to fetch or compute that value. One query can therefore draw on several databases or services, and the client never sees the seams.
Because the schema is typed, tools can check a query before it runs, autocomplete field names in an editor and generate client code. Many servers let clients ask the schema to describe itself, which is called introspection. Some teams disable it on public production systems.
REST has no built-in schema, but many REST APIs publish an OpenAPI description that gives similar documentation and code generation. The difference is that it's optional.
Fetching too much and too little
Over-fetching means receiving fields you don't need, such as a full user record when you only wanted a name. Under-fetching means one response isn't enough, so you make follow-up calls. On a slow mobile link, a chain of dependent requests adds noticeable delay because each waits for the one before it.
GraphQL addresses both directly. REST can address them too, with query parameters that select fields or expand related objects, or with purpose-built endpoints for a particular screen. Those work well when you have few clients. They get harder to manage when a web app, two mobile apps and several partners each want different combinations.
HTTP/2 and HTTP/3 also let a client run many requests at once over one connection, which reduces the cost of several parallel REST calls. It doesn't remove the wait when one call depends on another's answer.
Caching
HTTP caches understand URLs and headers. A GET /products/9 response with sensible cache headers can be reused by the browser, a proxy or a content delivery network with no special code, as explained in browser caching and Cache-Control.
GraphQL requests are often sent with POST to one URL, with the query in the body, so ordinary caches can't tell them apart. The common answers are client libraries that cache objects by identifier, server-side caching inside resolvers, and persisted queries, where the client sends a short ID for a pre-registered query, often via GET, so an edge cache can work again.
If your traffic is mostly public, repeatable reads, REST's caching advantage can matter more than any query flexibility.
Errors and monitoring
REST failures usually show up in the status code. GraphQL can return partial success: some fields filled in, others null, with an errors list explaining why. Many GraphQL servers answer such requests with 200, so clients must read the body, and dashboards that only count status codes will miss problems.
Logging needs similar care. "A request to the single endpoint was slow" says little. Useful monitoring records the operation name and timing for individual resolvers.
Performance and security on the server
Flexibility for the client becomes responsibility for the server.
- The N+1 problem. Asking for 50 posts and each post's author can trigger one database query for the list and then one per post. Batching tools, often called data loaders, combine those lookups.
- Expensive queries. A client can nest relationships deeply or request huge lists. Set limits on depth, list size and estimated cost, plus timeouts.
- Authorization. There's no separate URL to protect, so permission checks belong in the business logic that resolvers call, applied wherever sensitive data can be reached.
- Rate limits. Counting requests is a weak measure when one request can be a thousand times heavier than another, so many providers budget by query cost.
REST has authorization mistakes of its own, especially forgetting to check that the caller may access the particular object whose ID they supplied. Every API style needs that check.
Changing the API over time
REST APIs commonly publish numbered versions when they must break compatibility. GraphQL practice favors evolving one schema: add new fields freely, mark old ones with the @deprecated directive, watch which clients still request them, and remove them once usage stops. Because clients name every field, you can measure that usage precisely. It still takes discipline, since removing a field or changing its type breaks anyone who asks for it.
Which should you choose?
Team experience counts too. A group that knows how to secure and observe HTTP endpoints will need time to learn schema design, resolver performance and query cost controls. Adopt GraphQL to solve a fetching problem you can describe, not because it appears on job listings.
Files, uploads and real-time updates
Some jobs fit one style more naturally than the other. Downloading a large file is easy with a plain URL: the browser can stream it, resume it with range requests and cache it. GraphQL deals in structured fields, so most teams have a query return a link to the file and let ordinary HTTP carry the bytes. Uploads follow the same logic, often with a mutation that hands back a temporary upload address.
For live updates, GraphQL has subscriptions built into the language, though the specification leaves the transport to implementations. A REST service usually adds a separate mechanism: server-sent events, a WebSocket or webhooks that notify another server. Either way, keeping thousands of connections open is an operational job of its own, and it's worth asking whether checking every minute would serve the user just as well.
What it costs to run each
The first working version is quick in both. The differences show up later.
A REST team gets a lot for free from existing infrastructure. Access logs list which paths are slow, gateways can apply limits per route, and a new developer can test an endpoint with a browser or a terminal. The recurring cost is design churn: new screens ask for new combinations, and endpoints multiply with names like "summary" and "details".
A GraphQL team spends less effort negotiating response shapes with front-end developers, because the client can ask for what the schema already offers. The recurring cost moves to the platform: reviewing schema changes, watching resolver performance, tuning batching and keeping cost limits realistic. Someone has to own the schema, or it grows into a tangle of overlapping types.
Ask who will do that work. If nobody can, the flexibility will be paid for later in slow queries and surprising permissions.
Using both together
You don't have to rewrite anything to try GraphQL. A common pattern puts a GraphQL server in front of existing REST services: resolvers call the old endpoints and assemble the result. This helps front-end developers quickly, at the price of another component to run and another place where failures and delays can start.
Whichever you pick, send everything over HTTPS, authenticate each request, paginate large lists and document the behavior. Those habits, along with the fundamentals in what an API is, matter more to the people calling your API than the style's name. For debugging either one, curl and a JSON formatter go a long way.