REST vs GraphQL
REST uses HTTP verbs on resources; GraphQL uses a query language to fetch exactly what you need. REST is simpler and cacheable; GraphQL is flexible and avoids over-fetching. Choose REST for simple CRUD, GraphQL for complex data requirements.
- REST: simple CRUD, public APIs, when caching matters.
- GraphQL: complex frontends, mobile (bandwidth-sensitive), when clients need flexible data.
- REST: complex data requirements (GraphQL better).
- GraphQL: simple CRUD (REST simpler).
Definition
REST: endpoints per resource (/users/1), HTTP verbs, fixed responses. GraphQL: single endpoint, query language describes exactly what data you want, typed schema, resolvers fetch fields. GraphQL avoids over/under-fetching but adds complexity.
Why it exists
REST is simple but inflexible (fixed response shapes). GraphQL was created to let clients specify exactly what data they need — solving over-fetching (too much data) and under-fetching (multiple round trips).
How it works
REST: GET /users/1 returns a fixed shape. GraphQL: query { user(id: 1) { name, posts { title } } } returns exactly what you asked. Schema defines types and fields. Resolvers fetch data. Single endpoint (POST /graphql).
Scaling limits
REST: over/under-fetching, versioning. GraphQL: complexity, caching is harder, N+1 queries, DoS surface (arbitrary queries).
Cost shape
Both free. GraphQL needs a server library (Apollo, Yoga). REST needs nothing extra.
Alternatives
- REST — Simpler, standard, less flexible.
- gRPC — Internal APIs, typed, efficient, not for browsers.
- tRPC — TypeScript-only, no schema needed, internal APIs.