Overview
GraphQL and REST are two different approaches to API design.
- REST is resource-oriented
- GraphQL is query-oriented
Data Fetching Model
- REST: Server defines fixed response structures per endpoint
- GraphQL: Client defines exactly which fields to retrieve
Endpoints
-
REST:
-
GraphQL:
-
Single endpoint
-
Example:
/graphql
Over-fetching and Under-fetching
- REST:
- Over-fetching: receiving unnecessary data
- Under-fetching: needing multiple requests to gather related data
- GraphQL:
- Client specifies fields → avoids both problems
Example Comparison
REST
GET /user/1
GET /user/1/appointments