REST API vs GraphQL Simulator
Compare REST and GraphQL side-by-side. Understand over-fetching, under-fetching, the N+1 problem, and when to use each approach with interactive scenarios.
Category: API Design
What You Will Learn
- Compare REST and GraphQL request patterns
- Understand over-fetching and under-fetching
- Learn when to choose REST vs GraphQL
Topics covered: api, rest, graphql, educational, interactive, backend
// simulator
REST API vs GraphQL Simulator
Compare REST and GraphQL side-by-side. Understand over-fetching, under-fetching, the N+1 problem, and when to use each approach with interactive scenarios.
Simple Fetch
Goal: Compare one user profile over REST and GraphQL. Keep Simple Fetch in the menu and press Run comparison.
Simple Fetch ready to run.
Get basic user profile information
Each REST request waits for the preceding resource in this scenario.
REST total
not run yet
GraphQL total
not run yet
Overview
REST and GraphQL are two ways to design an API, and the difference shows up most clearly when a screen needs data from several places. This simulator runs the same data needs against both styles and shows the requests that travel and the data that comes back. In a simple fetch the two look much alike. With nested data, such as a user and their posts, REST needs a second request. With related data, such as the comments on each post, the number of REST requests grows with every post, the N+1 problem, while one GraphQL query asks for exactly the nested fields. REST also tends to over-fetch, returning whole objects when the screen needs two fields. GraphQL avoids both by letting the client describe the shape of the response. The trade-offs run the other way too: GraphQL moves complexity to the server, makes HTTP caching harder and needs limits on query depth, while REST stays simple to cache and reason about. The goal is to pick the right tool for each API rather than a winner.
Try this
- Run "Simple Fetch" in both styles and compare the requests and payloads.
- Switch to "Related Data" and count the REST requests, one more for every post, compared with a single GraphQL query.
- Use "Multiple Resources" to see over-fetching in REST responses next to the fields GraphQL returns.
Frequently asked questions
- What is over-fetching?
- Getting more data than the client needs, such as a full user object when the screen only shows a name. It wastes bandwidth and is common with fixed REST endpoints.
- Is GraphQL faster than REST?
- Not inherently. GraphQL can reduce round trips and payload size, but a complex query can be expensive to resolve on the server, and REST responses are easier to cache over HTTP.
- When should I choose REST over GraphQL?
- When the API is resource oriented, clients are simple or public, and HTTP caching matters. GraphQL fits best when many clients need different shapes of the same related data.
Try next
// simulator
Idempotency and Retry Lab
Lose payment responses, replay idempotent requests, compare backoff with full jitter during an outage, and move exhausted jobs to a dead-letter queue.
// simulator
GraphQL N+1 and DataLoader Lab
Trace nested GraphQL resolvers, count N+1 SQL queries, batch users and comments with DataLoader, and discover why a loader cache must stay inside one request.
// simulator
Email Authentication Lab: SPF, DKIM and DMARC
Publish SPF, DKIM and DMARC records, send mail directly and through a forwarder or mailing list, and inspect the receiver’s authentication verdict, alignment and disposition.