GraphQL vs. REST, Explained Using Pizza

Background photo credit:

Retrieving data from an API backend is a daily task for a lot of web developers. Using REST is a common way of accomplishing this. Recently an alternative has appeared in the form of GraphQL, developed by Facebook. But what is the main difference between the two? I will now attempt to show you using, of all things, pizza.

Meet the pizza people

Here are two competing pizza restaurants, Roger’s Pizza and George’s Pizza.

They both offer the same types of pizza, but the way you order is different for both.

Ordering from Roger’s Pizza

Roger is a big fan of fancy menus. You walk into his place and see this giant, beautiful menu…

Pretty simple. You realise that it’s not the most flexible of menus as the choice is limited. Luckily however your favourite pizza is Pepperoni/Mushroom and he has it! You order this pizza and leave happy with a full belly.

Ordering from George’s Pizza

George has spotted a competitive advantage over Roger. He offers a more flexible pizza ordering process. As you walk in you see the following setup…

You see four containers with a different topping in each. You point to the Pepperoni and Mushroom toppings. George makes your customised pizza. You leave happy with a full belly.

Pepperoni/Mushroom is boring

You are tired of having Pepperoni/Mushroom each day. It’s time to add some Olives!

You enter Roger’s Pizza. You look at the menu. Disaster, there is no Pepperoni/Mushroom/Olive option! Your initial concerns about this menu’s lack of flexibility have come true. You ask Roger to make this pizza and he says “Sure, but I need to redesign my fancy menu first. Come back tomorrow”. Roger just lost a customer today.

You head on over to George’s Pizza. You point to the Pepperoni, Mushroom and Olive toppings. In a few minutes your pizza is ready and you leave happy with a full belly.

Ok, how does this relate to REST and GraphQL?

In the above story Roger’s ordering system is like how we use REST. With REST, you look at your web app’s requirements and design individual endpoints. These REST endpoints will serve shapes of data to satisfy the requirements. In Roger’s case he decided what pizzas (requirements) his customers would like. Then he created his menu (REST endpoints) around them.

As long as his customers order the same choice of pizzas then his menu works well. But as soon as they try to order something different then the fancy menu needs to be redesigned to add the new choice.

This is like REST. If a new shape of data is required for your app and your current REST endpoints don’t serve it then you need to design and implement a new REST endpoint. This takes time and communication with your backend team before it is ready to use.

GraphQL is more flexible

When Pepperoni/Mushroom became boring George’s Pizza was able to accommodate your craving for Olives. This is because all the toppings were laid out in front of you and you picked the ones that you wanted. If tomorrow you wanted a new Ham/Pepperoni/Olive combo then George’s system would still be flexible enough to handle it for you.

George’s ordering system is like how GraphQL works. With GraphQL all the data available to you is documented via a schema. With this schema you can pick the data shape that you want. Should your web app add a new feature or a new screen then you can pick out the data shape that will best serve it. There is no need to communicate with your backend team to develop a new REST endpoint. This saves on time and development costs.

Mmm, Pepperoni/Mushroom/Olive goodness

The difference between GraphQL and REST

Flexibility is the arguably the biggest difference between the two technologies. There are also other differences like being able to retrieve all the data you need in one request using GraphQL, whereas it may take several requests with REST.

Finishing up

I hope you enjoyed this little pizza adventure and it helped to show the big difference between GraphQL and REST. Also did you know that George’s Pizza gets 33% more customers than Roger’s Pizza? I wonder why…

Relevant Links:

If you liked this article please consider dropping me a heart below and follow me for future articles like this!