REST vs GraphQL: When to Choose Each for Your API
REST fits clients that read or update resources through stable URLs. GraphQL fits clients that need different fields from related records in one request.
Which API fits your clients?
| Decision | REST | GraphQL |
|---|---|---|
| What does each screen need? | A profile page and an account page can reuse the same user representation. | A profile can request a name. An account page can also request the user's orders. |
| How do clients read related records? | The server can include related records or give them separate URLs. | The client selects related fields that the schema exposes. The server resolves them for each query. |
| Can clients reuse a response? | A public catalog at a stable URL suits shared HTTP caching when the response permits reuse. | A query over GET can use the same HTTP caches. Different query URLs have different cache keys. |
| What must the team maintain? | The team defines the resource representations and the HTTP methods each resource supports. | The team maintains a typed schema and the code for each field. It can limit the cost of nested queries. |
One example: a user and their orders
How a REST API handles the request
REST is an architectural style. In a REST API over HTTP, the URL identifies the resource and the HTTP method specifies the operation.
Suppose the API exposes a profile and an order history at separate URLs. The profile screen needs the first request alone. The account screen needs both.
GET /users/123
GET /users/123/orders
The API could also offer GET /users/123?include=orders to serve the account screen in one request. The server defines that option.
How a GraphQL API handles the same request
GraphQL is a query language for APIs. Its schema defines the fields, their types, and their relationships. The account screen sends this query to an endpoint such as /graphql.
query {
user(id: "123") {
name
email
orders {
id
total
}
}
}
The profile screen can omit orders from its query. Both screens use the same schema. A new field requires server support before a client can select it.
Where does the work happen?
One HTTP request can trigger several database queries. For example, a resolver that loads each order separately makes a database request per order. A batch read can retrieve those orders together.
For a fair comparison, measure the same screen with each design. Compare response bytes, HTTP requests, database queries, and time until the screen has its data. Test with both an empty cache and a warm cache.
HTTP caching, status codes, and API versioning
When can a cache reuse a response?
For either API, a shared cache can reuse a fresh GET response when the request matches and HTTP rules permit reuse. A public catalog might send Cache-Control: public, max-age=60. A personalized order history can use Cache-Control: private to prevent shared storage. RFC 9111 defines the rules for reuse.
The GraphQL over HTTP draft requires POST support and permits GET for queries. A server must reject a mutation sent via GET. The draft describes how to send a query in the URL.
An HTTP cache can store a POST response with explicit freshness information and a Content-Location equal to the target URI. It can reuse that response for a later GET or HEAD. A repeat POST still reaches the server. RFC 9110 specifies POST semantics.
What does the status code tell the client?
For a REST resource, a successful GET can return 200. If the server cannot find the resource, it can return 404. RFC 9110 defines HTTP status codes.
For GraphQL, the client checks the HTTP status, the response media type, and the result. With application/graphql-response+json, a malformed JSON request can produce a 400. If the server cannot resolve a nullable orders field, a 2xx response can contain both user data and an errors array. Legacy servers that use application/json can return 200 even for a GraphQL validation error. The GraphQL over HTTP draft explains status codes.
What happens when a field changes?
REST leaves API versioning to the API designer. A path such as /v2/users/123 is one option for an incompatible change. A compatible addition can keep the same URL. Fielding describes the REST constraints.
A GraphQL schema can add displayName and mark name with @deprecated. Clients can still select either field. If the team removes name, queries that select it fail validation. The GraphQL specification defines field deprecation.
Can REST and GraphQL share a backend?
A team can introduce GraphQL for the account screen while other clients continue to use the REST API. The resolver for orders can read from /users/123/orders for this user.
Client (web/mobile)
↓
GraphQL Gateway (aggregates, shapes data)
↙ ↓ ↘
REST REST REST
API1 API2 API3
(users) (orders) (products)
This design gives the client one request but adds a gateway to operate. The team can trace requests from one screen to check the cost before it moves other screens.
Where gRPC fits
If two internal services need a stream of order updates, gRPC offers typed service methods and bidirectional streaming. Teams can generate clients from a Protocol Buffers service definition. A REST or GraphQL API can call that service on behalf of a browser.
The gRPC vs REST guide compares service contracts, streaming, and browser support.
Frequently Asked Questions
Is GraphQL better than REST?
REST fits clients that share a stable representation of a resource. GraphQL fits clients that need different fields or relationships from the same schema.
Can a REST client request fewer fields?
Yes, if the API offers field selection. For example, an API can accept ?fields=name,email on a user URL. The API defines the parameter and its syntax.
Is REST faster than GraphQL?
A shared cache can shorten response time for repeated GET requests. A GraphQL query can save a network round trip when a screen needs related records. The result depends on cache reuse, response size, and database work.
Can I use both REST and GraphQL?
Yes. For example, a GraphQL resolver can call a REST orders endpoint. The account screen receives the order totals it selects, and other clients can still call the REST endpoint directly.
Related Topics
HTTP Methods
GET, POST, PUT, PATCH, DELETE — when to use each method in REST APIs.
REST API Design Guide
URL design, response structure, versioning, and production-ready patterns.
Best Practices
Design conventions for REST resources and responses.
Authentication
API keys, JWT, and OAuth 2.0 for REST APIs.