What are the main differences and trade-offs between REST and GraphQL?
❓ Interview Prompt: Compare REST APIs (multiple endpoints, fixed schemas, HTTP caching) vs GraphQL (single endpoint, flexible queries, over/under-fetching fixes).
REST and GraphQL represent fundamental web API paradigm choices:
| Feature | REST APIs | GraphQL |
|---|---|---|
| Endpoints | Multiple URIs ('/users', '/orders') | Single endpoint ('/graphql') |
| Data Fetching | Server determines fixed JSON payload shape | Client queries exact required fields |
| Over/Under-Fetching | Common issue requiring multiple API calls | Solved completely by client query schemas |
| Caching | Native HTTP/CDN URL caching out of the box | Complex client-side normalized caching |
| Tooling & Standards | Universal HTTP verbs, status codes, OpenAPI | Strongly-typed schema definition language (SDL) |
Code Implementation Example
// GraphQL Client Query requesting exact fields:
// query { user(id: 42) { name email orders { id amount } } }
// Returns exactly what was requested (No over-fetching!):
{
"data": {
"user": {
"name": "Alice",
"email": "alice@example.com",
"orders": [{ "id": 101, "amount": 49.99 }]
}
}
}REST leverages native HTTP caching and standard URIs; GraphQL eliminates over/under-fetching via single-endpoint query schemas.