[Sketchnote] Why Use GraphQL?
December 13th, 2020
I made a sketchnote based on my current understanding of their video: GraphQL is the better REST. The rest of this post describes the content of the sketchnote.
- An API standard created by Facebook.
- An alternative to the REST API standard.
API stands for "application programming interface." It's an agreement for how one program talks to another.
One program (the client) sends a request to another application (the server) to ask for information. The server sends a response with the requested data back to the client.
A GraphQL server exposes a single endpoint that handles all incoming requests, regardless of the data being requested.
- REST is like shopping at multiple, smaller retailers. You have to make multiple trips to different stores to buy all the things you need.
- GraphQL is like doing all your shopping at one big-box retailer (like Costco). You can make a single trip to buy everything you're looking for in one place.
Underfetching: When the response from the first endpoint doesn't have all the information you need, so you have to make another requst to a different endpoint.
- REST is like solving a crime by following a trail of clues. Each clue on its own isn't enough to solve the mystery: you have to put them all together to get the final answer you're looking for.
- GraphQL is like identifying a thief directly by playing back security camera footage. You can get the information you're looking for in one step.
Overfetching: When the response from an endpoint includes more data than you actually need for your use case.
- REST is like asking a chatty person for information. You ask them, "When is your birthday?" and they start telling you their entire life story.
- GraphQL is like asking a terse person for information. You ask them, "When is your birthday?" and they tell you a date. They give you only as much information as you ask for.