What is the difference between REST and GraphQL?
Answer
REST (Representational State Transfer) exposes data as resources with fixed URLs and HTTP methods. Each endpoint returns a predetermined data structure. Simple to understand, widely supported, good HTTP caching. Problems: over-fetching (endpoint returns more data than needed), under-fetching (multiple requests needed for related data), versioning complexity (/v1/, /v2/), rigid endpoint structure. GraphQL exposes a single endpoint where clients specify exactly what data they need via a query language. The server returns exactly what was requested — no more, no less. Key differences: Fetching: REST returns fixed data; GraphQL returns only requested fields; Multiple resources: REST needs multiple requests; GraphQL fetches all related data in one request; Versioning: REST often needs /v1/, /v2/; GraphQL adds fields and deprecates old ones without versioning; Type system: GraphQL has a strongly typed schema; REST has no built-in type system; Caching: REST is naturally cacheable via HTTP; GraphQL queries (POST) require custom caching (persisted queries, CDN with cache headers); Tooling: GraphQL has GraphiQL, introspection; REST has Swagger/OpenAPI. When to use GraphQL: complex data graphs, multiple client types (mobile/web) needing different data shapes, rapid product iteration. When to use REST: simple CRUD, public APIs, heavy caching requirements, limited client diversity. Many companies use both.