GraphQL vs REST - a vending machine example
Tags: graphql, showdev, webdev
Published: 2019-12-18 10:14:09

---
title: GraphQL vs REST - a vending machine example
published: true
description: Do you remember the episode of The Office in which Dwight's stuff was placed in a vending machine? It's an awesome example of GraphQL vs REST issue!
tags: graphql,showdev,webdev
cover_image: https://thepracticaldev.s3.amazonaws.com/i/ddp6apn155ps5bqfykyh.png
canonical_url: https://blog.graphqleditor.com/graphql-vs-rest-vending-machine-example/
---

Any **"The Office"** fans here?

Do you remember the episode in which Jim put Dwight's stuff into a vending machine? This seems to an on-point example of the major difference between **GraphQL vs REST** according to one of the GraphQL’s creators - **Lee Byron**.

![The Office - vending machine prank](https://thepracticaldev.s3.amazonaws.com/i/uay03gc8fn4da5in35iv.png)  

## The Vending Machine Case

In that episode, Jim puts Dwight's belongings into a vending machine. Among them where coffee mug, bobble-head, nameplate, pencil cup ... and Dwight's wallet. Not to leave him empty-handed, Jim provided Dwight a bag of nickels. 

To retrieve his item from a vending machine Dwight should:
1. Use nickles to retrieve his wallet
2. Use quartes, halves to retrieve the first object
3. Repeat the procedure for every item he wants to retrieve

That's the way the vending machine operates. The pattern is similar to those we know from using the **REST API**. Imagine we want to retrieve posts from a company blog using rest:
![GET /api/posts](https://thepracticaldev.s3.amazonaws.com/i/5v9voyhjkimrbxxsvbtk.png)
and if you want to add authors you will need to fetch the from another resource (or modify your resource containing posts) you'll end up with two server requests instead of one (or extra work), and as you continue to scale, you may have even more requests to different endpoints in order to fetch all the needed data:
![GET /api/post/:id](https://thepracticaldev.s3.amazonaws.com/i/fyhr6zk4z1xg0k10oj42.png)
This is exactly our vending machine, where to get one item you need to press one button. If you want to get more items you need to get the one-by-one, repeating the procedure for different items.

## A vending machine with **special buttons**

Now imagine a vending machine with special buttons allowing you to get multiple things at once. This kind of super vending would help Dwight's get his stuff back super fast and it's exactly what **GraphQL API** does in terms of retrieving data.
![GraphQL Query](https://thepracticaldev.s3.amazonaws.com/i/apei50lhqosbif9kg1v6.png)
In GraphQL we have a single endpoint being able to process complex requests. You query the **GraphQL server** for specific data and it will respond with what was requested, which results in fewer bits transferred over the wire.

![GraphQL vs REST](https://thepracticaldev.s3.amazonaws.com/i/p39c43pwx8jt8h5jehc6.png)

## Speed up your GraphQL API development

[**GraphQL Editor**](https://graphqleditor.com/) is a supportive tool for both advanced **GraphQL users** as well as those taking their first steps with GraphQL APIs. Our all-in-one development environment for GraphQL will help you build, manage & deploy your **GraphQL API** much faster thanks to dozens of built-in micro features.

[![GraphQL Editor](https://thepracticaldev.s3.amazonaws.com/i/835ac1nun474qfgn1tfi.gif)](https://graphqleditor.com)