GraphQL vs REST: why an API sends a whole post to show one title
GraphQL vs REST on over-fetching: GraphQL lets the client name the fields it needs. In our lab a profile card took 21,404 bytes via plain REST, 219 via GraphQL.
In this lesson you will learn
- What over-fetching costs one profile card, with our lab's byte counts
- How a GraphQL query is validated and resolved field by field, and why the reply has the same shape
- How a failed non-null field can null its parent, all the way up to the user
- When sparse REST fieldsets are enough, and what GraphQL costs you in N+1 loads and caching
Builds on Lesson 45: What your browser really sends in one HTTP request 5 min read Next: Security monitoring: login anomaly detection

Drafted with AI assistance, then checked and edited by a human engineer · How we write
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
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
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
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.

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.
This file runs with npm i graphql@16 and node profile-card-query.mjs. We ran it on 2026-11-19:
// 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
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
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.
![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](/img/posts/graphql-vs-rest-overfetching-ask-for-fields/jsonapi-sparse-fieldsets.webp)
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
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.

What GraphQL costs you
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 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.
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)engineering.fb.com
- GraphQL official site, home page (Precision)graphql.org
- GraphQL docs: Queriesgraphql.org
- GraphQL docs: Responsegraphql.org
- GraphQL docs: Cachinggraphql.org
- GraphQL docs: Performance (N+1, DataLoader, persisted queries)graphql.org
- GraphQL docs: Security (depth limiting, persisted queries)graphql.org
- JSON:API specification, Sparse Fieldsetsjsonapi.org