I get asked a lot about why I feel like GraphQL is generally a better solution than REST and one of my main reasons is the benefits you gain by fetching data declaratively instead of imperatively.

To give an example, let's look at grocery shopping. 🧵
Traditionally when we go grocery shopping, we walk the isles looking for the right products. We have a general sense about where those products are because signs at the end of each isle will direct us. This is a lot like how REST APIs work.
We are guided by endpoints and then find the data we need from each endpoint and bring it together into a data set. Much like filling your cart with groceries and then going to check out. It's imperative and requires knowledge about where groceries are.
When the store decides to update the layout and move isles around, we have to adapt to that knowledge and change where we search for certain products. This is true for each shopper (or client in the case of APIs).
On the other hand, GraphQL is more akin to having a personal shopper or using instacart. Instead of going to the grocery store and picking these items ourselves, we instead provide a list of the items we want and it is fetched for us from the locations in the store by someone.
It doesn't require me to know where in the store the item is, only to know what item I'm after. This is the declarative approach. When the store changes it's layout again it doesn't matter to me, as I still provide the same list. This is true for each shopper (or client).
https://twitter.com/undef_obj/status/1346900935176454152
You can follow @theworstdev.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled:

By continuing to use the site, you are consenting to the use of cookies as explained in our Cookie Policy to improve your experience.