REST vs GraphQL

Fixed endpoints vs flexible queries

REST and GraphQL are both approaches to building APIs, but they have fundamentally different philosophies about how clients should request data from servers.

REST

REST (Representational State Transfer) organizes APIs around resources with fixed endpoints. Each URL represents an entity (like /users/123), and HTTP methods (GET, POST, PUT, DELETE) define actions. The server decides what data each endpoint returns. REST has been the dominant API paradigm since Roy Fielding formalized it in 2000.

GraphQL

GraphQL is a query language for APIs created by Facebook in 2012 (open-sourced 2015). Instead of fixed endpoints, clients send queries specifying exactly what data they want. A single endpoint (/graphql) handles all requests. The client controls the shape and depth of the response.

Key Differences

- **Data fetching**: REST often requires multiple round trips. GraphQL can fetch everything in one request. - **Over/under-fetching**: REST endpoints return fixed data shapes — too much or too little. GraphQL returns exactly what's requested. - **Endpoints**: REST has many endpoints. GraphQL has one. - **Caching**: REST has built-in HTTP caching via URLs. GraphQL caching is more complex. - **Learning curve**: REST is simpler to start with. GraphQL requires learning a query language and type system. - **File uploads**: REST handles them natively. GraphQL needs workarounds.

When to Use Each

**Use REST** for simple CRUD APIs, public APIs (easier to understand and cache), server-to-server communication, and when your data model maps cleanly to resources. **Use GraphQL** when clients need flexibility in data fetching, your frontend is complex with varied data requirements, you want to reduce round trips, or you have a microservices backend you want to unify behind one API.

Analogy

**REST** is like a restaurant with a fixed menu — you order dish #14 and get exactly what's on the menu for that item, whether you wanted all the sides or not. **GraphQL** is like a build-your-own-bowl place — you specify exactly which ingredients and how much of each, getting precisely the meal you want in one order.