What is the difference between REST and GraphQL?
Compare REST and GraphQL: endpoints, over-fetching, round trips, caching and typed schemas, with examples and interview questions to guide your choice.
Expected Interview Answer
REST exposes many fixed endpoints that each return a predefined data shape, while GraphQL exposes a single endpoint where the client sends a query describing exactly the fields it needs and gets back precisely that shape.
With REST you often make several round trips and receive over- or under-fetched data, because each resource lives at its own URL with a server-defined response. GraphQL lets the client compose one request that traverses related data in a single call, using a strongly typed schema, resolvers, and introspection. REST leans on HTTP verbs, status codes, and caching, whereas GraphQL usually sends POST queries to one endpoint and handles caching at the client or field level.
- GraphQL avoids over-fetching and under-fetching
- GraphQL reduces round trips by combining related data in one request
- REST benefits from simple, mature HTTP caching
- REST is easier to debug with standard URLs and verbs
- GraphQL's typed schema enables strong tooling and introspection
AI Mentor Explanation
REST is like asking the scoreboard operator separate questions — one call for the batter's runs, another for the bowler's figures, another for the field placement — and each time you get a fixed printed card. GraphQL is like telling a single analyst 'give me only this batter's strike rate and these two bowlers' economy' and receiving exactly that in one tidy reply, nothing more.
Step-by-Step Explanation
Step 1
Compare the endpoint model
REST spreads resources across many URLs; GraphQL routes everything through one endpoint driven by a typed schema.
Step 2
Look at data fetching
REST returns server-defined shapes causing over/under-fetching; GraphQL returns exactly the fields the query names.
Step 3
Count the round trips
REST often needs several calls for related data; GraphQL can traverse relationships in a single request.
Step 4
Consider caching
REST leans on HTTP/CDN caching by URL and verb; GraphQL usually caches at the client or per-field level.
Step 5
Weigh tooling and typing
GraphQL's schema and introspection power strong tooling; REST relies on conventions and OpenAPI specs.
What Interviewer Expects
- Clear grasp of endpoint versus single-schema design
- Understanding of over-fetching and under-fetching
- Awareness of caching trade-offs
- Knowledge of when each approach fits a use case
- Ability to mention typed schema and resolvers
Common Mistakes
- Claiming GraphQL replaces REST in every scenario
- Ignoring REST's strong HTTP caching advantage
- Saying GraphQL has no downsides like query complexity
- Confusing GraphQL with a database or ORM
Best Answer (HR Friendly)
“REST gives you many fixed web addresses that each return a set chunk of data, while GraphQL gives you one address where you ask for exactly the fields you want. GraphQL cuts wasted data and extra trips, but REST is simpler and caches more easily.”
Code Example
// REST: two requests, fixed shapes
GET /users/42
GET /users/42/posts
// GraphQL: one request, exact fields
query {
user(id: 42) {
name
posts { title }
}
}Follow-up Questions
- How does GraphQL handle over-fetching that REST struggles with?
- What are the caching challenges of GraphQL compared to REST?
- When would you still choose REST over GraphQL?
- How do resolvers work in a GraphQL server?
- What is the N+1 problem in GraphQL and how do you solve it?
MCQ Practice
1. How many endpoints does a typical GraphQL API expose?
GraphQL routes all queries and mutations through a single endpoint backed by a typed schema.
2. Which problem does GraphQL specifically aim to reduce?
Clients request exactly the fields they need, so GraphQL avoids returning too much or too little data.
3. Which approach benefits most naturally from HTTP and CDN caching?
REST's unique URLs and GET verbs map cleanly onto standard HTTP and CDN caching.
Flash Cards
REST endpoint model? — Many URLs, each returning a server-defined resource shape.
GraphQL endpoint model? — A single endpoint driven by a typed schema; clients request exact fields.
Over-fetching? — Receiving more data than needed — common in REST, avoided by GraphQL queries.
Caching edge? — REST caches easily via HTTP/CDN; GraphQL caches at client or field level.