GraphQL vs REST - a 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 be one of the best examples of the major difference between GraphQL vs REST according to, one of the GraphQL’s creators, Lee Byron.
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:
- Use nickles to retrieve his wallet
- Use quartes, halves to retrieve the first object
- 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:
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.