Dev tools
REST vs GraphQL: Which API Style Should You Use?
REST vs GraphQL compared: how each works, one screen built both ways, performance, caching, errors and security, plus a checklist for which to choose.
In REST vs GraphQL, the core difference is who decides what a response contains. A REST API exposes many fixed URLs, one per resource, and each returns a shape the server chose. A GraphQL API exposes one endpoint and a typed schema, and the client sends a query naming exactly the fields it wants. REST is simpler, easier to cache and the safe default for public APIs; GraphQL pays off when several clients, such as web and mobile apps, need different slices of connected data and you want to get it in one round trip.
So is GraphQL better than REST? Not in general, and anyone who says so is selling something. This GraphQL vs REST API guide compares the two on how they work, builds one screen both ways, then looks at performance, caching, errors, versioning, security and tooling, and ends with a decision checklist. It's a companion to our broader gRPC vs REST guide, which has a short GraphQL section; this one goes deeper on the REST-versus-GraphQL choice.
REST vs GraphQL at a Glance
| REST | GraphQL | |
|---|---|---|
| What it is | An architectural style built on HTTP resources | A query language and runtime with a typed schema |
| Endpoints | Many, one per resource (/users/42, /orders) | Usually one (/graphql) |
| Who shapes the response | The server | The client, per query |
| HTTP methods | GET, POST, PUT, PATCH, DELETE map to actions | POST for everything; GET optional for queries |
| Typical problem | Over-fetching and several round trips | Server complexity and cost control |
| Caching | Native HTTP caching, CDNs | Harder; needs persisted queries or client caches |
| Errors | HTTP status codes | Often HTTP 200 with an errors array |
| Versioning | URL or header versions (/v2) | Evolve one schema, deprecate fields |
| Contract | Optional (OpenAPI) | Required, built in (the schema) |
| Real-time | SSE, WebSockets, webhooks | Subscriptions |
| Best for | Public APIs, simple CRUD, cacheable data | Many clients, nested data, fast-moving front ends |
What Is REST?
REST (Representational State Transfer) organises an API around resources: things such as users, orders and invoices, each with a URL. You act on them with standard HTTP methods: GET reads, POST creates, PUT or PATCH updates, DELETE removes. The server answers with a representation (usually JSON) and a status code (200, 201, 404 and so on). Because it uses ordinary HTTP, every browser, proxy, CDN and monitoring tool already understands it, and responses to GET requests can be cached with standard headers.
REST's weakness is rigidity. Each endpoint returns a fixed shape, so a client that needs only a user's name still gets the whole record (over-fetching), and a screen that needs a user, their orders and each order's items may need several requests in a row (under-fetching). We cover the classic comparison with an older style in REST vs SOAP.
What Is GraphQL?
What is GraphQL? It's a query language for APIs and a runtime for answering those queries. The specification was originally developed at Facebook and is now governed by the GraphQL Foundation, hosted under the Linux Foundation. A GraphQL service defines a schema: the types of data it offers and how they connect. Clients then write queries that follow those connections and list the fields they want, and the server returns JSON in exactly that shape.
The schema has three kinds of entry point:
- Query reads data.
- Mutation changes data.
- Subscription pushes updates, usually over WebSockets.
GraphQL is not a database language like SQL and doesn't replace your database or an ORM. It is agnostic about where the data comes from: resolver functions on the server fetch each field from a database, another API or a cache. That is also where its costs live, as the performance section shows.
REST vs GraphQL Example: One Screen, Two Ways
This GraphQL vs REST example uses a mobile dashboard that shows a customer's name, their three most recent orders and the items in each order. This is the most common situation where the two styles diverge.
With REST, a straightforward design needs a request for each resource:
GET /users/42
GET /users/42/orders?limit=3
GET /orders/101/items
GET /orders/102/items
GET /orders/103/itemsFive requests, the first two in sequence and the last three that can run in parallel once the orders are known. Each response also includes fields the screen doesn't use, such as the user's address and each order's shipping history. (The endpoint names here are illustrative; real APIs vary.)
With GraphQL, the client sends one request:
query Dashboard {
user(id: 42) {
name
orders(first: 3) {
id
total
items {
name
quantity
}
}
}
}The response mirrors the query: a user with a name and an orders list, each with exactly the requested fields and nothing else. One round trip, no over-fetching, and the same endpoint serves a different screen with a different query tomorrow without any server change.
| REST (basic) | GraphQL | |
|---|---|---|
| Requests for this screen | 5 | 1 |
| Sequential round trips | 2 | 1 |
| Unused fields returned | Many | None |
| Server changes to add a new screen | Possibly a new endpoint | Usually none |
Now the fairness check. REST can close most of this gap: add an ?include=orders.items option, or build a dedicated "dashboard" endpoint (often called a backend-for-frontend). Both are common, and both make REST competitive. And GraphQL moves the work rather than removing it: the server must still fetch the user, the orders and each order's items, and it can easily do that inefficiently, as the next section explains.
REST vs GraphQL Performance
Is GraphQL faster than REST? Sometimes, in one specific way: it cuts the number of network round trips and the bytes sent, which matters on slow mobile connections. It is not faster in general, and it can be slower on the server. Four things decide the real result.
Round trips and payload size. One query instead of five saves latency, especially when the client is far from the server. Smaller payloads help on mobile. This is GraphQL's real strength.
The N+1 problem. The GraphQL documentation describes it directly: to fetch a hero and their friends, the server makes one request for the hero, another for the friends and then a separate request for each friend's related data. A naive implementation turns one query into dozens of database calls. The standard fix is batching and caching with a tool such as DataLoader, which collects requests over a short period and sends them as one. A REST endpoint written with a proper join has no such problem, because the developer decides the query. Neither style is safe from slow database access; GraphQL just makes it easier to cause by accident.
Expensive queries. Because clients decide the shape, a client can ask for deeply nested, huge lists that would be costly to serve. REST limits this by design, since you control what each endpoint returns. GraphQL servers need explicit limits (see the security section).
Caching. Covered next, and it often decides the matter.
The honest summary: for a few simple resources, REST with HTTP caching is usually faster to build and to serve. For a complex screen over connected data, GraphQL can save a lot of waiting on the client side, provided the server is built carefully.
Caching: Where REST Has the Edge
REST uses plain HTTP, so a GET request for /products/7 can be cached by the browser, a CDN and any proxy using standard headers, and no special code is needed.
GraphQL typically sends every request as a POST to one URL, which standard HTTP caches treat as uncacheable by default. The GraphQL project's own guidance says the server may accept GET for queries, and that using GET makes responses cacheable and helps with CDNs. The catch is that long queries can exceed URL length limits, so the usual workaround is persisted queries (also called trusted documents or automatic persisted queries): the client sends a short identifier or hash and the server looks up the stored query. That works, but it's extra machinery that REST doesn't need.
On the client side, GraphQL libraries cache by object identifier, which gives a consistent UI, but that's a different thing from an edge cache. If most of your traffic is public, read-heavy and identical for many users, such as a product catalogue, REST's native caching is a major advantage.
Errors and Status Codes
REST uses HTTP status codes meaningfully: 404 for not found, 401 and 403 for authentication and permissions, 429 for rate limits, 500 for server errors. Monitoring and retries build on that.
GraphQL is different because one request can partly succeed. If a query asks for a user and their orders and the orders service fails, the response can contain the user data and an errors array for the missing part. The GraphQL-over-HTTP draft specification therefore says that if a response contains non-null data, the server should use a 2xx status code, "since HTTP does not yet have a status code representing partial success". Requests that fail validation before execution typically get a 400. With the newer application/graphql-response+json media type, a valid request that fails to execute can return 4xx or 5xx, but the older application/json style prefers 2xx.
The practical consequence is that a dashboard counting HTTP 5xx errors will miss many GraphQL failures. You need to log the errors array too.
Versioning and Evolving the API
REST APIs usually break compatibility by publishing a new version, such as /v2/users, and keeping the old one alive for a while. It's simple and explicit, but it means maintaining parallel versions.
GraphQL's approach is to evolve one schema without versions. New fields can be added without breaking existing queries, because clients only get what they ask for. Old fields are marked deprecated and removed once usage drops to zero, which is something the server can measure because it sees which fields clients request. That's a genuine advantage for APIs consumed by apps you can't force to update, such as mobile apps. It does need discipline: removing a field is a breaking change for anyone still querying it.
Security: Different Risks to Plan For
Both styles need authentication, authorisation and rate limits. GraphQL adds some risks because clients write the queries. The OWASP GraphQL Cheat Sheet recommends:
- Depth limits on incoming queries, so a client can't nest relationships endlessly.
- Amount limits and pagination, so one query can't request a million records.
- Timeouts at the application or infrastructure layer.
- Query cost analysis with a maximum cost per query, where it's needed.
- Disabling introspection in production or public environments, so outsiders can't browse your whole schema.
- Controls on batching, since many operations in one request can be used to brute-force data past per-request rate limits.
- Object-level authorisation: always check that the requester may see each object, on both the edges and the nodes of the graph, not just at the top of the query.
Rate limiting works differently too, because one GraphQL request can cost far more than another. GitHub's public GraphQL API is a good example: rather than counting requests, it gives each user 5,000 points per hour, calculates a query's cost from the number of requests needed to fill each connection (divided by 100, minimum 1 point), requires every connection to set a first or last between 1 and 100, and rejects any call that would need more than 500,000 nodes. REST APIs usually count requests per endpoint instead. For the general pattern, see API rate limiting.
Tooling, Real-Time and File Uploads
- Documentation and discovery. A GraphQL schema is self-describing: introspection lets tools such as GraphiQL list every type and field and autocomplete queries. REST gets similar results from an OpenAPI document, but it has to be written or generated and kept up to date.
- Type safety. Both can generate typed clients, from OpenAPI for REST and from the schema for GraphQL. GraphQL's schema is part of the protocol, so the contract can't drift from the implementation as easily.
- Real-time. GraphQL has subscriptions in the spec, usually delivered over WebSockets. REST has no standard answer and uses SSE, WebSockets or webhooks; our guide to server-sent events vs WebSockets covers the options.
- File uploads. REST handles them natively with multipart forms or direct-to-storage URLs. GraphQL has no built-in upload type, and most teams still use a REST endpoint or signed URL for files.
- Debugging. With REST you can read a request in a browser or curl. GraphQL needs a client or a request body, though its tools are excellent.
When to Use REST and When to Use GraphQL
Choose REST when:
- You're building a public or partner API that third parties will call. REST's familiarity and caching win.
- The data is simple and resource-shaped: CRUD on a handful of entities.
- Responses are cacheable and identical for many users.
- Your team is small and needs the shortest path to a working, easy-to-monitor API.
- You need file uploads, webhooks or integration with standard gateways without extra layers.
Choose GraphQL when:
- Several clients, such as web, iOS and Android, each need different fields from the same data.
- Screens combine data from many related objects or services and round-trip latency is hurting.
- Front-end teams need to ship new screens without waiting for backend changes.
- You can afford the server-side work: DataLoader-style batching, limits, cost control and good monitoring.
- You'd like an API gateway over several back ends, presenting one schema to clients.
Choose both when it fits: a public REST API for partners and a GraphQL API for your own apps is a common and sensible split. Another popular pattern is a GraphQL layer in front of existing REST services, which gives front ends flexibility without rewriting back ends.
Quick Decision Checklist
| Question | Lean REST if... | Lean GraphQL if... |
|---|---|---|
| Who calls the API? | Unknown outside developers | Your own apps |
| How many client types? | One or two with similar needs | Many with different needs |
| How connected is the data? | Flat resources | Deeply nested relationships |
| How cacheable is it? | Mostly public and repeated | Mostly personalised |
| How big is the team? | Small | Has capacity for platform work |
| Is mobile bandwidth a problem? | No | Yes |
Common Mistakes
- Choosing GraphQL because it's modern. If one client calls five endpoints, a REST API is fine.
- Shipping GraphQL without limits. No depth, cost or pagination limit means any client can run an expensive query.
- Ignoring the N+1 problem. One innocent-looking query can trigger hundreds of database calls without batching.
- Putting authorisation only at the top of the query. Check access on each object and field.
- Leaving introspection on in public production. It hands attackers a full map of your schema.
- Making REST chatty on purpose. If you see many sequential calls, add an include option or a purpose-built endpoint before reaching for a new technology.
- Monitoring GraphQL by HTTP status alone. Partial failures return 200, so track the errors array.
- Treating the choice as permanent. Many systems run both, and moving some screens is easier than a full rewrite.
FAQ
What is the main difference between REST and GraphQL?
REST has many endpoints returning fixed shapes decided by the server. GraphQL has one endpoint and a typed schema, and the client specifies exactly which fields to return in each query.
Is GraphQL better than REST?
Neither is better in general. GraphQL suits many clients with different needs and connected data, while REST is simpler, easier to cache and better for public APIs. Many teams use both.
Is GraphQL faster than REST?
It can reduce round trips and payload size, which helps on slow connections. But it can be slower on the server because of the N+1 problem and costly queries, and it loses easy HTTP caching. REST with caching is often faster for simple, repeated reads.
Is GraphQL harder to secure than REST?
It has extra risks because clients write queries, so you need depth and cost limits, pagination, timeouts, object-level authorisation and introspection turned off in production. These are well-documented, but they are work REST doesn't need as much.
Can I use REST and GraphQL together?
Yes. A common setup is a public REST API for partners and a GraphQL API for your own web and mobile apps, or a GraphQL gateway that calls your REST services behind it.