REST vs. GraphQL
Over-Fetching, Under-Fetching und Endpunkt-Wildwuchs - die Probleme, die GraphQL adressiert, indem der Client die Antwort formt, samt der Abwägungen, die es mitbringt.
Deutsche Übersetzung in Arbeit
Diese Lektion ist noch nicht ins Deutsche übersetzt und wird daher auf Englisch angezeigt. Der Rest der Seite ist vollständig lokalisiert.
Auf dieser Seite
REST serves fixed responses from fixed endpoints. That works beautifully until a mobile screen needs less data than the endpoint returns, a detail page needs more than one endpoint provides, and you end up with a dozen bespoke endpoints to paper over the mismatch. GraphQL flips the control: the client sends a query describing exactly the shape it wants, and the server returns precisely that - no more, no less. Before you reach for it on BookVault, understand the problems it solves and the costs it adds.
Three REST pain points
Over-fetching - BookVault's GET /api/books/42 returns the full book: title, author, ISBN, description,
publisher, page count, reviews. A "recently viewed" widget needs only the title and cover. It downloads
kilobytes to use two fields.
Under-fetching (the N+1 round-trip) - a book detail page wants the book and its author's bio and the member's loan status. That's three endpoints, three round-trips, and glue code to stitch them. On a list of 20 books, showing each author means 1 + 20 calls.
Endpoint sprawl - to fix the above, teams add /books/42/summary, /books/42/with-author,
/books/mobile/42... each a new endpoint to build, document, and version. The API surface balloons.
What GraphQL does instead
One endpoint (POST /graphql), and the client writes the query:
query {
book(id: 42) {
title
author { name } # traverse into the related author
availableCopies # only the fields this screen needs
}
}The response mirrors the query exactly:
{ "data": { "book": {
"title": "Effective Java",
"author": { "name": "Joshua Bloch" },
"availableCopies": 3
} } }No over-fetching (you named three fields, you got three), no under-fetching (author came along in the same
request), no new endpoint (the graph already exposed author off book). The client composes what it needs
from a typed graph of connected data.
GraphQL is a spec, not a Spring thing
GraphQL is a query-language specification (from Meta, now community-governed) with implementations in every language. Spring for GraphQL is the Spring integration built on the graphql-java engine. So the schema, query syntax, and concepts here are portable - what Spring adds is the wiring to your beans, security, and data layer.
The costs GraphQL adds
It is not free. Be honest about the trade-offs before adopting it:
- HTTP caching gets harder - REST's
GET /books/42caches trivially in a CDN or browser. GraphQL's singlePOST /graphqlwith a body does not, so you lean on application-level caching instead. - The N+1 problem moves server-side - the client's one tidy query can trigger many database queries as the server resolves each field (a whole lesson later).
- Query complexity is a security surface - a malicious client can ask for deeply nested data; you need depth/complexity limits.
- More upfront design - a schema, resolvers, and tooling versus a quick
@GetMapping.
REST is a diner with numbered combos: order #3 and you get the burger, fries, and the coleslaw you didn't want (over-fetching), and if you also want a milkshake that's a second trip to a different counter (under-fetching). GraphQL is a build-your-own bowl: you point at exactly the ingredients you want and get a single bowl with precisely those - but someone had to design the ingredient stations (the schema), and if you pile on twenty toppings the kitchen has to work harder to assemble it (server-side complexity).
BookVault's iOS app makes six REST calls to render one book detail screen: the book, its author, its publisher, its average rating, the current user's loan status, and similar titles. Battery and latency complaints are rising. Would GraphQL address this, and what new concern does it introduce that you'd need to plan for?
What core problem does GraphQL solve that REST commonly suffers from?
Key takeaways
- REST returns fixed responses from fixed endpoints, causing over-fetching, under-fetching (extra round-trips), and endpoint sprawl.
- GraphQL exposes one endpoint and lets the client write a query describing exactly the fields and related data it wants; the response mirrors the query.
- GraphQL is a portable specification; Spring for GraphQL wires the graphql-java engine to your Spring beans and data layer.
- Costs: HTTP caching is harder, the N+1 problem moves server-side, query complexity is a security surface, and there's more upfront schema/resolver design.
- GraphQL shines for mobile and rich detail screens that stitch many related resources; weigh it against REST's caching simplicity per use case.