# GraphQL vs REST: why an API sends a whole post to show one title

URL: https://systemdesignsimple.com/graphql-vs-rest-overfetching-ask-for-fields/
Published: 2026-11-19
Category: System Design
Series: System Design, simply, Part 49: Ask for exactly the fields you want (GraphQL)
Publisher: System Design Simple (https://systemdesignsimple.com)

## Summary

In GraphQL vs REST, over-fetching is a common point of comparison: a fixed REST response can carry whole posts when a card shows one name and three titles. GraphQL lets the client select fields, and the result follows the query's shape. REST can trim too with a fields parameter. Our in-process lab: 21,404 bytes for plain REST, 227 for sparse REST, 219 for GraphQL.

## Key facts

- **Our lab:** sd-49, in-process simulation, seed 49, 500 runs: median response 21,404 bytes for plain REST, 227 for REST with ?fields=, 219 for GraphQL. No headers, compression or real network
- **Same shape:** graphql.org/learn/queries: the result has the same shape as the query
- **Null propagation:** graphql.org/learn/response: when a non-null field fails, null moves up to the nearest nullable parent, and data itself can become null
- **REST trim:** JSON:API 8.4 Sparse Fieldsets: a client MAY request only specific fields with fields[TYPE], if the server implements it
- **Caching:** graphql.org/learn/caching: exposing a globally unique id is a best practice that lets client caches merge the same object
- **Origin:** Engineering at Meta (14 Sep 2015): Facebook began rebuilding its native mobile apps in 2012 and built GraphQL that year

In GraphQL vs REST, over-fetching is a common point of comparison: a fixed REST response can send whole posts, bodies included, to a profile card that shows one name and three titles. GraphQL lets clients select fields and returns them in the same shape; REST responses can also be trimmed when the API supports it.

## What over-fetching costs one card {#over-fetching}

Take a profile card showing four values: your friend's name and the titles of their three newest posts. A classic REST design serves it with two fixed resources, `GET /users/42` and `GET /users/42/posts`, and each returns whatever fields the server put in that resource. (Part 48 covered how REST lays out addresses and verbs.) The client parses everything and keeps four values, which is over-fetching. The opposite problem, under-fetching, is needing a second or third call to fill the screen.

## GraphQL vs REST in our lab {#lab}

Our lab script `labs/sd-49/compare-rest-vs-graphql-payload.mjs` fills that card three ways for 100 seeded users with 12 to 40 posts each and post bodies of 150 to 400 words, over 500 runs (seed 49, Node 22). Byte counts are fixed by the seed. Plain REST returns its default page of 10 full posts; the other two return only the three posts the card shows, with only the fields it needs.

| Method | Requests | Median response bytes |
| --- | --- | --- |
| Plain REST, full objects (page size 10) | 2 | 21,404 |
| REST with the lab's own `?fields=` parameter and `limit=3` | 2 | 227 |
| GraphQL, one query | 1 | 219 |

The server runs in the same process, so there is no real network, no HTTP headers, no TLS and no gzip or brotli, and compression would shrink the repeated text in full posts a lot. The GraphQL side is a small engine written for the lab, so its timings say nothing about graphql-js. The `?fields=` parameter is the lab's own example and does not follow JSON:API.

Two requests also do not mean two waits. The user id is already known, so both REST calls can go out at once. The lab's network model (60 ms round trip, 10 Mbps) estimates median card times of 60.2 ms for sparse REST sent concurrently and 60.3 ms for GraphQL. REST pays a second round trip only when the second call has to wait for the first (an estimated 120.2 ms). The byte gap mixes two things, seven extra posts and the unused fields on every post, and this run does not split them apart.

## How a GraphQL query picks fields {#how-it-works}

The graphql.org home page sums it up as "Ask for what you need, get exactly that". You can only choose from fields the schema offers.

[Image: graphql.org home page, Precision section: Ask for what you need, get exactly that, with a query and its response side by side | graphql.org home page, Precision section. Captured in Chrome 2026-11-19. Source: [graphql.org](https://graphql.org/).]

The server first parses the query and checks every field against the schema, so a field that does not exist fails validation before any code runs. Then each selected field is resolved by a function called a resolver: `user(id: 42)` returns a user, `latestPosts(first: 3)` a list, `title` a string on each post. The executor groups selected fields by response key and writes the resolved values in a nested response. The Queries docs put it as: the result has the same shape as the query.

Omitted fields usually make the response smaller, though how much depends on the values and on fixed overhead such as headers. A resolver that loads the full post row and returns only `title` still reads the whole row: selection trims the response, and the database read shrinks only if the resolvers are written to load less. How rows sit on disk matters here too, as in [why 20 posts can touch 20 pages](/why-20-posts-cost-20-disk-pages/).

This file runs with `npm i graphql@16` and `node profile-card-query.mjs`. We ran it on 2026-11-19:

```js
// profile-card-query.mjs: an illustrative schema, resolvers and the card's query (graphql-js 16)
import { graphql, buildSchema } from 'graphql';

const schema = buildSchema(`
  type Post { title: String! body: String! }
  type User { name: String! email: String bio: String latestPosts(first: Int!): [Post!]! }
  type Query { user(id: ID!): User }
`);
const posts = [
  { title: 'Hello world', body: 'long text 1', at: 1 },
  { title: 'My first app', body: 'long text 2', at: 2 },
  { title: 'Trip photos', body: 'long text 3', at: 3 },
  { title: 'Older post', body: 'long text 0', at: 0 },
];
const rootValue = {
  user: ({ id }) => id === '42' ? {
    name: 'Your friend', email: 'friend@example.com', bio: 'a long bio',
    // newest first, at most `first` posts
    latestPosts: ({ first }) => [...posts].sort((a, b) => b.at - a.at).slice(0, first),
  } : null,
};
const source = '{ user(id: 42) { name latestPosts(first: 3) { title } } }';
console.log(JSON.stringify(await graphql({ schema, source, rootValue })));
```

`{"data":{"user":{"name":"Your friend","latestPosts":[{"title":"Trip photos"},{"title":"My first app"},{"title":"Hello world"}]}}}` `email`, `bio` and `body` exist on the objects but are not in the output. Add `body` to the selection and it appears. Ask for `likes`, which the schema lacks, and validation rejects the query. ## When one field fails, null moves up {#errors} The Response docs describe what happens when a resolver throws. The failed field becomes `null` and an entry goes into `errors`. If that field is declared non-null, it cannot hold `null`, so the null moves to the nearest parent that allows it. Follow that through the schema above. Say `title` fails on one post. `title` is `String!`, so that post becomes null. The list is `[Post!]!`, so neither the post nor the list may be null, and `latestPosts` on `User` is non-null too. The first nullable field is `Query.user`, so the response becomes `{"data":{"user":null}}` with one error, and the card loses the name it could have shown. If `user` had been `User!`, all of `data` would be null. The `!` marks therefore also decide how far one failure spreads. Mark a field non-null when the data really guarantees it, knowing that a failure there can null a parent. Clients should handle both `data` and `errors`. ## Can REST trim too? Sparse fieldsets {#rest-trim} Yes, when the server supports it. JSON:API section 8.4 says a client MAY request specific fields with a `fields[TYPE]` parameter, and an endpoint that receives one MUST NOT include other fields of that type. [Image: JSON:API specification section 8.4 Sparse Fieldsets: a client may request that an endpoint return only specific fields with a fields[TYPE] query parameter | JSON:API specification, 8.4 Sparse Fieldsets. Captured in Chrome 2026-11-19. Source: [jsonapi.org](https://jsonapi.org/format/#fetching-sparse-fieldsets).] In JSON:API, field selection is optional for the server. In GraphQL every query must list its fields, and one operation can fetch any nested data the schema exposes, such as a user and their posts. ## Why Facebook built it in 2012 {#why-graphql} Meta's engineering post of 14 September 2015 says Facebook began rebuilding its native mobile apps in 2012 and needed News Feed as data. The team found a gap between the data the apps wanted and the server queries they had to make, and GraphQL came out of that work the same year. The post gives no response-size figures. [Image: Engineering at Meta post: Back in 2012, we began an effort to rebuild Facebook's native mobile applications; the team was frustrated with differences between the data the apps wanted and the server queries | Engineering at Meta, "GraphQL: A data query language" (14 Sep 2015). Captured in Chrome 2026-11-19. Source: [engineering.fb.com](https://engineering.fb.com/2015/09/14/core-infra/graphql-a-data-query-language/).] ## What GraphQL costs you {#costs} Suppose the card also showed each post's author. If the `author` resolver looks up one user per post, three posts mean one query for the list plus three author queries, the N+1 problem. Whether it happens depends on how resolvers are written. The batching pattern in the Performance docs (DataLoader is the common one) collects those lookups during a request and sends them as one query. A full feed multiplies this across many posts and authors; [designing Instagram's feed](/design-instagram-feed/) shows the data model such a query would sit on. A REST client can treat the URL `/users/42` as the identity of the object. The Caching docs say GraphQL has no URL-like primitive for each object, and recommend that the API expose a globally unique id. A normalized client cache can then store user 42 once and merge it from different queries, if the client selects the id field or is told how to identify the type. Whether a changed name then updates every screen depends on the client library and how it is set up. For where else stale copies can hide, see [five places a cache can hide](/five-places-a-cache-can-hide/). Queries often go as POST to one URL, which CDNs do not cache by default. Persisted queries give each query a stable id, which makes stable cache keys practical when you pair them with GET requests or server and CDN cache rules. And since a client can nest `posts { comments { author { posts ... } } }`, servers set depth and cost limits and cap page sizes. | Option | Good for | Costs you | | --- | --- | --- | | REST, fixed responses | Simple, cacheable by URL | May send extra bytes or require additional calls when screens need different data | | REST with a fields parameter | Trimmed responses, still URL-based | Server must implement it per resource | | GraphQL | Each screen selects its own nested data | Schema, resolver batching, cost limits, client cache setup | For one or two clients with a handful of stable screens, we would add a fields parameter and a page limit to REST: in our lab that pair came within 8 bytes of GraphQL, and you keep plain URL caching. GraphQL pays off when many clients and screens pull different slices of the same connected data, and you are ready to own resolvers, error handling and query limits. For calls between services that need a deadline more than flexible fields, RPC with a deadline is often the better fit. The profile card, user 42 and the post titles are an illustrative example, not a real API. Lab numbers come from an in-process simulation, not a production service. GraphQL's history is from Meta's own public post; nothing here describes any company's private systems.

## Frequently asked questions

**What is over-fetching in an API?**
Over-fetching is when a response carries fields the screen never uses, for example whole posts with bodies when a profile card shows only a name and three titles. In our in-process lab that card took a median 21,404 bytes from plain REST and 219 from a GraphQL query.

**Does GraphQL always return the fields I ask for?**
When every resolver succeeds, the response follows the shape of the query. When one fails, its field becomes null and an entry is added to errors. If that field is non-null, the null moves up to the nearest nullable parent, which can blank a whole object or all of data.

**Can REST avoid over-fetching without GraphQL?**
Yes, if the server supports it. JSON:API defines sparse fieldsets: the client adds fields[articles]=title,body, where articles is a resource type, and the endpoint must not include other fields of articles resources. Many REST APIs offer a similar fields parameter of their own.

## Sources

- [Engineering at Meta: GraphQL: A data query language (14 Sep 2015)](https://engineering.fb.com/2015/09/14/core-infra/graphql-a-data-query-language/)
- [GraphQL official site, home page (Precision)](https://graphql.org/)
- [GraphQL docs: Queries](https://graphql.org/learn/queries/)
- [GraphQL docs: Response](https://graphql.org/learn/response/)
- [GraphQL docs: Caching](https://graphql.org/learn/caching/)
- [GraphQL docs: Performance (N+1, DataLoader, persisted queries)](https://graphql.org/learn/performance/)
- [GraphQL docs: Security (depth limiting, persisted queries)](https://graphql.org/learn/security/)
- [JSON:API specification, Sparse Fieldsets](https://jsonapi.org/format/#fetching-sparse-fieldsets)
