Software Studio
Back to Articles

GraphQL vs REST: When Each Makes Sense

GraphQLRESTAPI DesignArchitecture
Cover illustration for GraphQL vs REST: When Each Makes Sense

The GraphQL vs REST debate generates more heat than light. Both are tools with trade-offs, and the right choice depends on your specific context. Here's what I've learned from building production APIs with both.

GraphQL vs REST at a glance

  • REST is simple, cacheable and understood by every piece of infrastructure.
  • GraphQL lets each client ask for exactly the data it needs.
  • Caching is easier with REST.
  • Client diversity is handled better by GraphQL.
  • My default is REST, with GraphQL when the clients or the data justify it.

REST's strength is simplicity

REST's greatest strength is simplicity. A GET to /api/articles returns articles. A POST to /api/articles creates one.

The HTTP semantics, meaning methods, status codes and caching headers, are well-understood by every developer and every piece of infrastructure. Load balancers, CDNs and API gateways all understand REST natively. You can debug it with curl, and a new team member can guess most of the API from two examples.

GraphQL's strength is query flexibility

GraphQL's greatest strength is query flexibility. The client specifies exactly what data it needs.

A mobile app fetching a user profile might request name, avatar and recent posts in a single query:

query {
  user(id: "42") {
    name
    avatarUrl
    recentPosts(first: 3) {
      title
    }
  }
}

A web dashboard might request name, email, role, permissions and activity log. Same schema, different queries, no endpoint proliferation. Nobody has to add a new endpoint, or a new field to an old one, each time a screen changes.

The N+1 problem exists in both

The N+1 problem exists in both, but shows up differently.

  • In REST, it's the client making N+1 API calls: fetch the articles, then fetch the author for each.
  • In GraphQL, it's the server making N+1 database queries: resolve the articles, then resolve the author for each.

DataLoader solves this in GraphQL by batching and caching database queries within a single request.

const authorLoader = new DataLoader(async (ids: readonly string[]) => {
  const authors = await db.author.findMany({ where: { id: { in: [...ids] } } });
  const byId = new Map(authors.map((author) => [author.id, author]));
  return ids.map((id) => byId.get(id) ?? null);
});

A hundred author lookups become one query. The database side of this is covered in PostgreSQL Performance: Queries That Scale.

Caching: REST has a clear advantage

Caching is where REST has a clear advantage. HTTP caching headers such as Cache-Control and ETag work out of the box. CDNs cache REST responses transparently.

GraphQL responses are POST requests to a single endpoint, which HTTP caches can't meaningfully cache. Client-side caching in GraphQL, with Apollo Client or urql, is powerful but requires more setup.

If most of your traffic is public, read-heavy content, this point alone can settle the decision.

GraphQL schemas need safeguards

Schema design in GraphQL requires discipline. The flexibility that makes GraphQL powerful also makes it easy to create expensive queries. A malicious or naive client could request deeply nested data that generates hundreds of database queries.

These safeguards are essential:

  • Rate limiting
  • Query complexity analysis
  • Depth limiting

With REST, each endpoint has a known cost. With GraphQL, the client writes the query, so you have to put limits on what a query may cost.

When REST is the better fit

For backend-for-frontend patterns, where your API serves a specific frontend you control, REST is usually simpler.

  • You know exactly what data each page needs.
  • Custom endpoints that return exactly that data are straightforward to build and cache.
  • GraphQL's flexibility is less valuable when you control both sides.

When GraphQL shines

  • Public APIs consumed by diverse clients.
  • Applications with complex data relationships.
  • Teams where frontend and backend iterate independently.

GitHub's API v4 is GraphQL because their API serves thousands of different applications, each needing different data shapes. No fixed set of endpoints could suit all of them.

My default

REST for most applications, especially those serving a single frontend. GraphQL when the client diversity or data complexity justifies the additional tooling and operational overhead.

And increasingly, tRPC for TypeScript full-stack applications, where end-to-end type safety eliminates the need for a formal API layer entirely. When the same team owns the client and the server in one language, calling a typed function is hard to beat.

Three questions usually settle it:

  1. How many different clients will consume this API, and do you control them?
  2. How much of the traffic could be served from an HTTP cache?
  3. Is the team ready to run the extra tooling GraphQL needs?

Answer those honestly and the choice tends to make itself. For structuring the REST side, see TypeScript Patterns for NestJS APIs.