LearnThatStack Ace your next interview
API Design · question
Question 39 of 92

Compare REST vs GraphQL APIs.

intermediate Pro
← All API Design questions

The full answer covers the core mechanics, when this pattern wins, and the trade-offs interviewers most often probe. It includes a code sample. Read it once at your own pace, then try to recall the structure from memory before the interview. Pro also includes this question's interview lens - the likely follow-up probes and what you can say in the room.

Full answer + code samples + AI explanations

Unlock to read the complete answer for this premium question.

Guided practice for API Design One question at a time. Answer out loud, get graded, see what you missed.
Related concept

Who controls the response shape

In REST the server decides the response shape, in GraphQL the client builds it, and in gRPC a compiled contract sets it. The trade-offs come from who decides the shape.

For this question · chapters 1 and 2 of 3

1/3 Over-fetching and under-fetching
the problemOver-fetching and under-fetchingone shape, too much, too littletrips1on the wire600byteselapsed250msprofile screennamePriya Ramandrawn at 250 msorder 8842Ceramic mug, 24.00order 8839Desk lamp, 62.00order 8801Notebook pack, 18.00the wirefields returned, lit is drawnGET /screens/profile600 bytes8 of 8 fieldswho decides the response shapeserver decidesREST, this chapterclient composesGraphQL, chapter 2contract compilesgRPC, chapter 3one endpoint per screen, and a deploy for every new oneserver decides, client composes, contract compiles
the problemOver-fetching and under-fetchingtrips1on the wire600byteselapsed250msprofile screendrawn at 250 msnamePriya Raman8842Ceramic mug, 24.008839Desk lamp, 62.008801Notebook pack, 18.00the wirewho decides the response shapeserver decidesREST, hereclient composesGraphQL, nextcontract compilesgRPC, lasta deploy for every new screenthree answers to one question

Over-fetching and under-fetching

  1. A profile screen needs its data in one exact shape. The screen has four rows: the name with its avatar, then the last three orders, each with a product name and a total. But the server that answers the request has never seen this screen. Every problem in this concept comes from the server not knowing that shape.
  2. The first request shows over-fetching: the response is bigger than the screen needs. The call to GET /users/123 returns 24 fields in 2400 bytes, but the screen shows only two of them. The mobile radio still turns on to download the other 22 fields, which the screen never shows. The endpoint is not badly written: it is written once for every caller, and this screen is only one of those callers.
  3. Under-fetching is the same mistake in the opposite direction. This time the fixed response shape is too small, because the user record holds order_ids, not the orders themselves. So the client has to send a second request. The second request cannot start until the first one has finished.
  4. On a phone, 610 ms means most of a second of blank screen. The screen waits for a third request to turn the product ids into names. The three requests could not run at the same time, because each request needed ids from the one before it. The three requests also moved 6900 bytes so the screen could show 8 of the 99 fields that came back.
  5. REST has a reasonable fix: GET /screens/profile, an endpoint that returns exactly the eight fields the profile screen shows. So the profile call takes one trip, 600 bytes and 250 ms. But the order screen and the settings screen each need their own endpoint, so every new screen means a deploy. A ?fields= parameter on the old endpoint does the same job with less setup, but the server still decides which fields exist.
  6. Speed was never the point, because the purpose-built endpoint was as fast as anything that comes after it. The real question is who decides the response shape. In REST, the server decides the shape, one endpoint at a time. The next two chapters give the other two answers.
Chapter 1 · step 1 of 6

A profile screen needs its data in one exact shape. The screen has four rows: the name with its avatar, then the last three orders, each with a product name and a total. But the server that answers the request has never seen this screen. Every problem in this concept comes from the server not knowing that shape.

1 of 6 Use ← → or swipe See the concept: all 3 chapters in it

© LearnThatStack - diagrams may not be republished without permission.

Want a quick review of the fundamentals? See the API Design cheatsheet.

← Back to all API Design questions
Pro · $10/mo

74 of 92 API Design answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime