What is the difference between REST and GraphQL?
REST exposes many fixed endpoints that each return a set shape of data. GraphQL exposes one endpoint where the client asks for exactly the fields it needs, which avoids over-fetching but makes caching and rate limiting harder.
Explain it simply
REST is a fixed menu: /users/5, /users/5/orders, each returning whatever the server decided. GraphQL is a build-your-own thali: you list exactly which items you want in one request.
REST:
- Resource-based URLs and HTTP verbs (GET, POST, PUT, DELETE).
- Simple, cacheable with normal HTTP caching.
- Can over-fetch (extra fields) or under-fetch (need several calls).
GraphQL:
- One endpoint, a typed schema, and a query that names the fields.
- A mobile screen can fetch a user, their last 3 orders and order totals in one round trip.
- Needs more server work: resolvers can cause the N+1 query problem, caching is harder because all calls are POST to one URL, and expensive queries must be limited by depth and cost.
Choose REST for simple, public, cache-friendly APIs. Choose GraphQL when many clients (web, Android, iOS) need different shapes of the same data.
Example
REST (3 calls):
GET /users/5
GET /users/5/orders?limit=3
GET /orders/881/items
GraphQL (1 call):
{ user(id: 5) { name orders(last: 3) { id total } } }What to say in the interview
"REST is resource-oriented with many endpoints and easy HTTP caching; GraphQL gives clients precise queries from one endpoint, cutting over- and under-fetching. The costs are resolver N+1 issues, harder caching and the need for query cost limits."