REST vs. GraphQL: Ordering at a Restaurant vs. Building Your Own Pizza

10 Minby Muhammad Fahid Sarker
RESTGraphQLAPIWeb DevelopmentProgrammingNode.jsJavaScriptData FetchingAPI DesignBeginner Guide
REST vs. GraphQL: Ordering at a Restaurant vs. Building Your Own Pizza

Hey there, fellow coder! So you're building this awesome new app. You've got the frontend looking slick, the backend logic is... well, it's getting there. But now you need the two to talk to each other. You need to fetch data. You've heard the cool kids whispering two names in the hallways: REST and GraphQL.

They sound like rival Transformer factions, but what are they really? And which one should you choose?

Fear not! Let's break this down using the most universal language known to developers: food.

The Old-School Diner (Meet REST)

Imagine you walk into a classic American diner. The menu is on the wall, and your options are fixed.

  • Combo #1: The User Special (gets you a user's name, email, address, date of birth, and their dog's middle name)
  • Combo #2: The Posts Platter (gets you all posts by a user, including the title, content, and a list of comments)
  • Combo #3: The Comments Combo (gets you all comments for a specific post)

This is REST (REpresentational State Transfer) in a nutshell. It's an architectural style where you have different endpoints (URLs) for different resources. Each endpoint is like a fixed combo meal on the menu.

https://my-awesome-api.com/users/123 -> Gets you the User Special for user #123. https://my-awesome-api.com/users/123/posts -> Gets you the Posts Platter for user #123. https://my-awesome-api.com/posts/456/comments -> Gets you the Comments Combo for post #456.

It's structured, predictable, and has been the standard for years. But... what if you just want the user's name and the titles of their posts? Nothing else.

The Problem with the Diner

  1. Over-fetching (Getting Too Much Food): To get the user's name, you have to order the User Special. The kitchen sends you everything: their address, their dog's name... all you wanted was the name! You're throwing away perfectly good data, and it wasted the waiter's time (and your network bandwidth).

    javascript
    // I just want the name! fetch('https://my-awesome-api.com/users/123') .then(res => res.json()) .then(user => { console.log(user.name); // Got it! // But I also got all this other stuff I don't need... // user.address, user.email, user.dog.middleName... });
  2. Under-fetching (Making Multiple Trips): To get the user's name AND the titles of their posts, you have to make two separate trips to the counter. First, you order the User Special. Then, you go back and order the Posts Platter.

    javascript
    // Trip 1: Get the user fetch('https://my-awesome-api.com/users/123') .then(/* ... */); // Trip 2: Get the user's posts fetch('https://my-awesome-api.com/users/123/posts') .then(/* ... */);

This is inefficient. You're making your app wait for two round trips to the server. It's like your waiter has to run back and forth to the kitchen twice.

The Fancy Build-Your-Own-Pizza Place (Enter GraphQL)

Now, imagine you walk into a modern, chic, build-your-own-pizza place. There's no fixed menu. Instead, there's just one counter and a list of all the fresh ingredients they have available: different doughs, sauces, cheeses, and toppings.

You walk up to the counter (a single endpoint) and give them a very specific order:

"Hi, I'd like a pizza. I want the user with an ID of 123, but I only want their name. And while you're at it, grab their posts, but for each post, I only want the title."

And in one trip, the chef hands you a beautiful, custom-made pizza with exactly what you asked for. Nothing more, nothing less.

That is GraphQL.

GraphQL is a query language for your API. Instead of multiple endpoints that return fixed data structures, you usually have a single endpoint. You send a POST request to this endpoint with a "query" that describes exactly the data you want.

Here’s what that pizza order looks like in code:

javascript
// The one and only endpoint const GRAPHQL_ENDPOINT = 'https://my-awesome-api.com/graphql'; // Your very specific order (the query) const myQuery = ` query GetUserAndPostTitles { user(id: "123") { name posts { title } } } `; // Sending the order to the kitchen fetch(GRAPHQL_ENDPOINT, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ query: myQuery }) }) .then(res => res.json()) .then(data => { console.log(data); });

The server's response will be a JSON object that mirrors the exact shape of your query:

json
{ "data": { "user": { "name": "Captain Coder", "posts": [ { "title": "Why I Love GraphQL" }, { "title": "My Top 5 Keyboards" } ] } } }

Look at that! In one request, we got exactly what we needed. We solved both over-fetching and under-fetching at the same time. The power is now in the hands of the client (the frontend app).

The Head-to-Head Showdown

Let's put them side-by-side in a simple table.

FeatureREST (The Diner)GraphQL (The Pizza Place)
How you get dataServer tells you what you get from each URL.You tell the server exactly what data you want.
EndpointsMany endpoints (/users, /posts, etc.).Typically one endpoint (/graphql).
Data FetchingProne to over/under-fetching.Gets exactly what you ask for in a single request.
Client WorkSimpler requests, but might need to make many.More complex query, but only needs to make one.
API EvolutionOften requires versioning (v1, v2, v3).No versioning needed. Just add new fields.

So, Which One Should I Use?

This isn't a battle where one hero vanquishes the other. They are different tools for different jobs.

Choose REST when:

  • You have a very simple API with clearly defined resources (e.g., a simple blog).
  • You want to leverage built-in HTTP caching mechanisms, which work beautifully with REST's URL structure.
  • Your team is already comfortable with REST, and you need to build something quickly.

Choose GraphQL when:

  • Your application is complex and has interconnected data (e.g., a social network where you need users, their posts, their friends, and their friends' posts).
  • You're building for multiple clients (web, iOS, Android) that all have different data requirements.
  • You want to minimize the number of network requests, which is super important for mobile apps on slow connections.
  • You want to give your frontend developers more power and flexibility to build features without waiting for the backend team to create new endpoints.

The Final Slice

Think of REST as a reliable, established restaurant that serves great combo meals. It's perfect for many occasions.

Think of GraphQL as the trendy, new spot that lets you build your own perfect meal from a huge list of ingredients. It's incredibly flexible and efficient, especially when your appetite is very specific.

Neither is inherently "better"—they just solve the problem of getting data in different ways. Now you know the difference, you can make the right choice for your next project.

Happy coding, and may your data always be exactly what you asked for!

Related Articles