Go REST

REST vs GraphQL

When each makes sense, what they share, and why most teams should reach for REST first.

REST and GraphQL are both ways to expose a backend to a client over HTTP. The framing is often "GraphQL is better than REST" or vice versa, but the truth is more boring: they are different tools that fit different situations. Pick the one that solves your actual problems.

What they share

Both run over HTTP. Both use JSON for payloads. Both let a client read and write data. Both can be authenticated with bearer tokens, rate-limited, cached at the edge, monitored with the same tools. The differences are in theshape of the API, not the transport.

What REST looks like

A REST API exposes a fixed set of URLs, each returning a fixed shape. To assemble a complex view, you make several calls:

GET /users/123
-> { id, name, email }

GET /users/123/posts
-> [{ id, title }, ...]

GET /posts/456/comments
-> [{ id, body, author }, ...]

Three round-trips for "user with their posts and the comments on each post". Pros: each endpoint is simple and cacheable independently. Cons: you over-fetch (every endpoint returns its full shape, even fields you do not need) and under-fetch (you have to make multiple calls for one screen).

What GraphQL looks like

A GraphQL API exposes one URL (/graphql) and a schema. The client sends a query describing exactly the shape they want:

query {
  user(id: 123) {
    name
    posts {
      title
      comments {
        body
        author { name }
      }
    }
  }
}

One round-trip for the same data. Pros: client decides what fields come back, no over-fetch, related data nests naturally. Cons: caching is harder (every query is unique), the schema-first approach is heavier, error handling is fiddly (a query can partially succeed).

Where REST wins

Where GraphQL wins

The N+1 problem cuts both ways

REST encourages multiple calls; GraphQL hides them but the database may still get hit N+1 times if your resolvers are naive. Solving N+1 in GraphQL means using DataLoader (or equivalent) to batch within a single query. Solving it in REST means designing the right endpoints (/users?include=posts) or living with the round-trips.

How to choose

Three questions:

Tie-breaker: REST first. If you outgrow it, you can layer GraphQL in front of REST endpoints. The reverse migration is harder.

What about tRPC, gRPC, JSON-RPC?

Three more flavours worth knowing:

For typical product API work, the choice is REST or GraphQL. Pick REST unless you have a concrete reason to pick GraphQL.

Continue reading

More primers

All primers Glossary Integration guides