wasAsked.

What is the difference between REST and GraphQL?

Frequently Asked (All Companies) · Role not specified · technical

Published

Year not specified · Software Engineering

Community answers

Posted by wasAsked editorial collection

REST and GraphQL are two approaches to designing APIs. REST exposes multiple resource-based endpoints; GraphQL exposes a single endpoint where clients describe exactly the data they need. REST - Resources at different URLs, accessed with HTTP methods: GET /users/42 GET /users/42/orders GET /orders/1001/items - The server decides the shape of each response. - Uses HTTP features naturally: status codes, caching (Cache-Control, ETags, CDNs), content negotiation. GraphQL - A query language and runtime developed by Facebook (open-sourced in 2015). - A single endpoint (usually POST /graphql) and a strongly typed schema describing types, queries, mutations and subscriptions. - Clients request exactly the fields they need, including related data, in one round trip: query { user(id: 42) { name orders(last: 3) { id total items { productName quantity } } } } Key differences 1. Over-fetching and under-fetching - REST endpoints often return more data than needed (over-fetching) or require several calls to assemble a screen (under-fetching). - GraphQL returns exactly the requested fields in one request. 2. Endpoints and versioning - REST: many endpoints; breaking changes often handled with versions (/v1, /v2). - GraphQL: one endpoint; schema evolves by adding fields and deprecating old ones (@deprecated), typically without versions. 3. Typing and introspection - REST: contracts documented separately (OpenAPI). - GraphQL: the schema is built in and introspectable, enabling powerful tooling (GraphiQL, code generation for typed clients). 4. Caching - REST: easy HTTP caching by URL at browsers, proxies and CDNs. - GraphQL: POST requests to one URL are harder to cache with HTTP; caching is done in clients (Apollo, Relay normalised caches), with persisted queries, or server-side. 5. Error handling - REST uses HTTP status codes. - GraphQL usually returns 200 with an errors array, and partial data is possible. 6. Performance and security concerns - GraphQL needs protection against expensive queries: depth and complexity limits, timeouts, query cost analysis, and persisted queries. - The N+1 problem in resolvers requires batching (DataLoader). - Authorization must be applied at field or resolver level. - REST endpoints are simpler to rate-limit and monitor individually. 7. Real-time - GraphQL subscriptions provide real-time updates over WebSockets; REST uses webhooks, SSE or separate WebSocket APIs. When to choose REST - Simple CRUD APIs, public APIs, file uploads and downloads, heavy reliance on HTTP caching and CDNs, and teams wanting simplicity. When to choose GraphQL - Complex frontends (web and mobile) that need data from many related entities, with different clients needing different shapes. - Aggregating several backend services behind one API (federation). - Rapidly evolving UIs where avoiding backend changes for every screen is valuable. In .NET, Hot Chocolate is the leading GraphQL server; in Java, Spring for GraphQL; in Node.js, Apollo Server. Interview summary: "REST uses multiple resource URLs with server-defined responses and strong HTTP caching. GraphQL uses one typed endpoint where clients request exactly the fields they need, reducing over- and under-fetching, at the cost of more complex caching, security and server-side optimisation."

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics