Coursify

System Design for Software Engineers

REST vs GraphQL

When designing public-facing APIs, the two most popular architectural styles are REST and GraphQL. Each has its own strengths and weaknesses.

1. REST (Representational State Transfer)

REST is an architectural style that uses HTTP methods to manipulate resources identified by URLs.

  • Resource-Based: Everything is a resource (e.g., /users, /posts).
  • Standardized: Uses standard HTTP methods (GET, POST, PUT, DELETE).
  • Cacheable: Standard HTTP caching mechanisms work well with REST.
  • Problem: Over-fetching & Under-fetching:
    • Over-fetching: Getting more data than you need (e.g., getting the whole user object just for the username).
    • Under-fetching: Not getting enough data in one request, leading to multiple requests (N+1 problem).

2. GraphQL

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data.

  • Client-Specified: The client defines exactly what data it needs.
  • Single Endpoint: Usually just one /graphql endpoint for all queries and mutations.
  • Strongly Typed: Uses a schema to define types and relationships.
  • Solves Over/Under-fetching: Clients get exactly what they ask for in a single request.
  • Complexity: Server-side implementation can be more complex (caching, rate limiting, N+1 query problems).
FeatureRESTGraphQL
Data FetchingFixed (Server-defined)Flexible (Client-defined)
EndpointsMultiple (one per resource)Single endpoint
VersioningUsually via URL (e.g., /v1/)Versionless (evolutionary schema)
CachingEasy (HTTP/Browser level)Harder (Requires client-side cache like Apollo)

Knowledge Check

Question 1 of 3
Q1Single choice

What is 'over-fetching' in the context of REST APIs?