Web6 min read10 interview questions
REST or GraphQL: which should you use
Two ways for apps to ask a server for data, and how to choose.
In one sentenceREST gives you fixed menus of data at different addresses. GraphQL lets you describe exactly the fields you want in one request.
Think of it like thisREST is ordering set meals from a menu. GraphQL is telling the kitchen exactly which ingredients you want on one plate.
The big picture
Tap any step to see where to use it, how to use it and an example result.
How REST works
Each kind of thing has an address such as /users/42. You use standard actions: GET to read, POST to create, PUT or PATCH to change, DELETE to remove. It is simple, easy to cache and easy to test with a browser.
How GraphQL works
There is usually one address. You send a query that lists the fields you want, even across related things, and the response has the same shape as the query. It saves extra trips when a screen needs data from many places.
Choosing
Pick REST when the data is simple, public or cached heavily. Pick GraphQL when many different screens need different slices of related data, such as a mobile app and a website sharing one backend.
Side by side
| Feature | REST | GraphQL |
|---|---|---|
| Addresses | Many, one per kind of thing | Usually one |
| Response shape | Decided by the server | Decided by the caller |
| Caching | Easy, built into the web | Needs extra work |
| Learning curve | Gentle | Steeper |
| Best for | Simple, public or cached data | Varied screens over related data |
Common mistakes
- Choosing GraphQL only because it is newer.
- Building a REST API with verbs in the address, like /getUsers. The method already says the action.
- Letting GraphQL queries run without limits. A single deep query can overload a server.
Quick recap
- REST: many addresses, fixed shapes
- GraphQL: one address, you pick the fields
- REST is easier to cache and learn
- GraphQL helps when screens need varied data
Try it yourself
List the data one screen of an app you know needs. Count how many REST calls it would take, then write the same request as one GraphQL query.
Top 10 interview questions
Ordered from basic to advanced. Try answering out loud first, then open the answer.
What is a REST API?Basic
A way of exposing data as resources with addresses, such as /users/42, and using standard HTTP methods like GET, POST, PUT, PATCH and DELETE to read and change them.
What is GraphQL?Basic
A query language for APIs where the client sends one request describing exactly which fields it wants, usually to a single address, and gets back a response in the same shape.
What are the main HTTP methods and what do they do?Basic
GET reads, POST creates or triggers an action, PUT replaces, PATCH changes part of a resource, and DELETE removes it.
What do over-fetching and under-fetching mean?Basic
Over-fetching is getting more data than you need, such as a whole user record when you only want the name. Under-fetching is needing several requests to assemble one screen. GraphQL was designed to reduce both.
What does it mean that REST is stateless?Intermediate
Each request contains everything the server needs to handle it. The server does not remember earlier requests, so any server can answer any request, which makes scaling and caching easier.
Which is easier to cache, and why?Intermediate
REST, because each resource has its own URL and GET responses can be cached by browsers and CDNs. GraphQL usually sends POST requests to one address, so you need extra tooling such as persisted queries.
What is the N+1 problem in GraphQL?Intermediate
Resolving a list of items and then running one extra database query per item, giving N+1 queries. It is usually fixed by batching with a tool such as DataLoader.
How do you version a REST API, and how does GraphQL differ?Intermediate
Common ways are a version in the path (/v2/users) or a header. GraphQL usually avoids versions by adding new fields and marking old ones deprecated, so existing clients keep working.
How do you protect a GraphQL server from expensive queries?Advanced
Limit query depth and complexity, set timeouts, use pagination, allow only approved persisted queries for public use, and rate limit by cost rather than by request count.
When would you choose REST, GraphQL or something else like gRPC?Advanced
REST for simple, public or heavily cached APIs. GraphQL when many different screens need different slices of related data. gRPC for fast service-to-service calls inside a system where both sides are under your control.
Guides are simplified on purpose. Tool names and details change, so check the official documentation before relying on a detail.