What are the main differences between GraphQL and REST?
Answer
REST uses multiple endpoints (one per resource), and the server controls the response shape — clients often receive too much data (over-fetching) or must make multiple requests for related data (under-fetching). GraphQL uses a single endpoint and lets the client request exactly the fields it needs. Key differences: (1) Over/under-fetching: GraphQL eliminates both. (2) Versioning: REST needs /v1, /v2 prefixes; GraphQL evolves the schema with deprecations. (3) Type system: GraphQL has a built-in strongly typed schema; REST relies on external documentation. (4) Discoverability: GraphQL has introspection; REST needs OpenAPI/Swagger. (5) Network requests: GraphQL can fetch nested data in one request; REST may require many. (6) Caching: REST has natural HTTP caching; GraphQL requires extra effort (persisted queries, CDN).