How-To Guide

How to Choose Between REST and GraphQL

Pick REST or GraphQL from the client needs: weigh over-fetching, caching, versioning, and tooling, and match the choice to the workload.

On this page

  1. Introduction
  2. Start from the client’s needs
  3. Weigh over-fetching and round trips
  4. Weigh caching and HTTP semantics
  5. Weigh the tooling and the team
  6. Test the workload against the choice
  7. Common pitfalls
  8. Conclusion

Introduction

REST and GraphQL are two ways to expose an API, and the choice is a trade, not a winner. REST is resource-oriented, uses the HTTP methods and status codes, and caches naturally. GraphQL exposes a schema a client queries for exactly the fields it needs, which helps a client with varied needs and related resources, at the cost of a single endpoint, application-level caching, and more server complexity. The right choice depends on the clients and the workload.

This tutorial lays out the trade-offs: start from the client’s needs, weigh over-fetching and round trips, weigh caching and HTTP semantics, and test the main workload against the choice. The examples compare the two styles, and the method applies to any API decision.

Start from the client’s needs

The decision starts with the clients. If the clients are homogeneous, such as one web app that needs the resources as the server defines them, REST’s fixed shapes fit and the HTTP caching and status codes help. If the clients are varied, such as several apps that each need a different subset of a graph of related data, GraphQL’s flexible selection removes the over-fetching and the round trips.

Ask how many clients, how different their needs, and how related the resources are. One client that uses most fields of each resource points to REST. Many clients that each use a different slice of a connected graph, such as a mobile app that needs few fields and a dashboard that needs many, points to GraphQL. The client’s needs, not the technology fashion, drive the choice.

# GraphQL: the client asks for exactly the fields it needs, in one request
query {
  order(id: 42) {
    id
    total
    customer { name }
    items { sku qty }
  }
}

Weigh over-fetching and round trips

A REST endpoint returns a fixed shape, so a client that needs a subset over-fetches, and a client that needs related resources makes several calls. GraphQL lets the client ask for the exact fields and the related resources in one request, so it neither over-fetches nor makes the extra round trips. For a client on a slow link or a mobile connection, avoiding the over-fetch and the extra round trips is a real gain.

The cost of the flexibility is the server complexity. GraphQL needs a schema, resolvers, a query planner that does not blow up on a nested query, and a defence against an expensive query, such as a depth limit and a query cost. REST needs none of that, because the shape is fixed. The choice weighs the client’s benefit against the server’s cost.

Weigh caching and HTTP semantics

REST uses the HTTP methods and status codes directly, so a proxy or a CDN can cache a GET by its URL and the client can revalidate with an ETag. GraphQL uses one endpoint and usually POST, so the responses are not cached by URL, and the caching moves to the application, with a persisted query or a client-side cache. A read-heavy API with cacheable resources fits REST’s caching better.

The status codes are also clearer in REST: a 404 for a missing resource and a 201 for a created one map to the operations. GraphQL returns 200 for a query that failed, with the error in the response body, so the client must inspect the body for the error. Both work, but the HTTP semantics a proxy and a client understand natively are REST’s territory.

# REST: cacheable by URL, status codes map to the operation
GET /orders/42          -> 200, cacheable, ETag for revalidation
POST /orders            -> 201 with Location
GET /orders/9999        -> 404

# GraphQL: one endpoint, POST, errors in the body
POST /graphql { order(id: 9999) { id } } -> 200 { "errors": [ ... ] }

Weigh the tooling and the team

Both styles have mature tooling. REST has OpenAPI for the schema and code generation, and a wide set of clients and proxies. GraphQL has a schema and code generation from it, a rich client cache in Apollo or Relay, and a playground. Choose the tooling the team knows, because the maturity of the ecosystem matters less than the team’s ability to operate it.

The operational side differs. A REST API is a set of endpoints that are easy to route, cache, and monitor per route. A GraphQL endpoint is one route, so the monitoring is per field or per query, and the protection against an expensive query is server-side. Both are operable, but the team must know which the operation suits.

Test the workload against the choice

Prototype the main client use case in the chosen style and measure the round trips and the payload. A mobile screen that needs a few fields across three resources is a few REST calls or one GraphQL query; measure the bytes and the round trips and see whether the difference matters. For a cacheable reference API, measure the cache hit rate under REST and under GraphQL.

Decide on the data, not on the theory. A workload that shows over-fetching and many round trips favours GraphQL; one that is resource-oriented and cacheable favours REST. A mixed system can use both, with REST for the resource-oriented part and GraphQL for the graph, which is a pragmatic answer when the workloads differ.

Common pitfalls

Choosing GraphQL for a homogeneous client that needs the same resources, so the flexibility is unused, choosing REST for varied clients that over-fetch and make many round trips, ignoring that GraphQL loses URL caching and HTTP status semantics, not protecting a GraphQL endpoint against an expensive query, and picking by fashion rather than by the workload are the recurring mistakes. The client-needs first choice, a measured trade-off, an appreciation of the caching cost, and a workload test address them.

Conclusion

REST and GraphQL are a trade, not a winner. Start from the client’s needs, weigh the over-fetching and round trips against the server complexity, weigh REST’s caching and HTTP semantics against GraphQL’s single endpoint, and prototype the main workload to decide on data. Self-check by measuring the round trips and the payload for the main use case in both styles. Then the choice fits the workload instead of the fashion.

Sources: GraphQL: Introduction and comparison, MDN: HTTP caching, AWS: GraphQL vs REST.

FAQ

Is GraphQL better than REST?
Neither is universally better. GraphQL fits clients with varied data needs and a graph of related resources, where over-fetching and round trips hurt. REST fits resource-oriented APIs with homogeneous clients and cacheable responses, where the HTTP semantics help. Choose by the workload.
Does GraphQL replace API versioning?
Not entirely. GraphQL's deprecation of fields softens the versioning, so a field can be deprecated without a new version, but a breaking change to a field's type still needs care and a schema check. REST versions at the URL or the header, which is a coarser but clearer boundary.

Related Examples