189
Top Answer
REST
- Multiple endpoints (
/users,/posts) - Fixed data structure per endpoint
- Over-fetching or under-fetching common
- HTTP methods define operations (GET, POST, PUT, DELETE)
GraphQL
- Single endpoint (
/graphql) - Client specifies exactly what data it needs
- No over-fetching or under-fetching
- All requests are POST with a query
When to use REST:
- Simple CRUD applications
- Caching is important (HTTP caching works well)
- Team is familiar with REST
- Public APIs with many consumers
When to use GraphQL:
- Complex data requirements
- Mobile apps (minimize data transfer)
- Rapidly evolving frontend needs
- Multiple clients with different data needs
Example
# GraphQL - get exactly what you need
query {
user(id: "1") {
name
posts {
title
}
}
}
vs REST: 2 requests to /users/1 and /users/1/posts
APIArchitect