Machine Room

REST vs GraphQL

One URL per resource against one endpoint where your app asks for the fields, one profile page both ways, what happens when a resource gets big, the price of GraphQL, and which one to pick. The one-page notes from the reel.

Download the PDF

One-page notes, REST vs GraphQL. Both let your app get data from a server. Each one has its own use case. 1, REST: one URL per resource. Three cards: user /users/1, post /posts/7, comment /comments/3. A phone, your app, sends GET /users/1 to a server and gets back {"id": 1, "name": "Maya Chen"}. Every resource gets its own URL. You ask with plain HTTP verbs, like GET and POST. The server decides what each URL sends back. 2, GraphQL: one endpoint, you ask for the fields. A query, { user(id: 1) { name posts { title } } }, goes as POST /graphql to a server; the reply: {"data": {"user": {"name": "Maya Chen", "posts": [{"title": "Trail run"}, {"title": "Sourdough"}, {"title": "Bike ride"}]}}}. A query language: your app names exactly the fields it wants. The reply holds exactly those fields, and no more. Every query goes to one URL, usually /graphql. 3, One profile page, both ways. A phone shows the profile page: Maya Chen, Trail run, Sourdough, Bike ride. Requests: REST 2, GET /users/1 and GET /users/1/posts; GraphQL 1, POST /graphql. Who picks the fields: REST the server, GraphQL your app. GitHub's own example: 10 followers, plus 10 followers of each = 11 REST requests, or 1 GraphQL request. 4, When the resource gets big. GET /users/1 serves the profile page, the settings page, checkout and the admin panel, and its reply grows to 15 fields: id, name, email, phone, address, city, role, created_at, last_login, avatar_url, plan, status, timezone, language, notes; only name is highlighted. Every caller gets all 15 fields, even the ones it doesn't need. Roy Fielding, who defined REST: "information is transferred in a standardized form rather than one which is specific to an application's needs." On the GraphQL side, an iOS phone and an Android phone both send to the same POST /graphql: iOS asks { user(id: 1) { name avatarUrl } } and gets its name and avatarUrl; Android asks { user(id: 1) { posts { title } } } and gets the three post titles. Each app gets only its own fields. Nobody's reply gets bigger. 5, The price. Caching: REST, a GET to a URL, browsers and CDNs can cache it by default; GraphQL, usually a POST to one URL, a POST is not answered from a cache. Rate limits at GitHub: 5,000 requests an hour against 5,000 points an hour, each query costs points. Over the limit at GitHub: 403 or 429 against 200 + an error message. GraphQL can still be cached: servers may also accept GET for queries, and with caching headers HTTP and CDN caches can help. A GraphQL reply can carry partial data and a list of errors together; when there is data, the status should still be 2xx. The work moves to the server. One query can ask for a lot: a naive server loads 10 users, then each user's posts one by one, 1 + 10 = 11 database queries. That's the N+1 problem. The usual fix: batching. 6, Which one? REST: simple and straightforward, you can build any API with it, HTTP caching for free. GraphQL: many apps need different data from the same API; fewer requests and less data, why mobile developers love it. You don't have to pick one. GitHub runs both, and graphql.org says: "GraphQL and REST can actually co-exist in your stack." REST: the server decides what each URL returns. GraphQL: your app asks one endpoint for exactly the fields it needs.

On a phone, press and hold the image to save it. The reel: REST vs GraphQL: Which One Should You Use?.