top of page
  • Writer's picturePriyanka Palshetkar

Exploring GraphQL as an alternative to REST API - Part 2

To compare GraphQL directly with REST API, let’s go back to our restaurant example.


Say, we are hungry and we walk into the first place we see on the side of the road, The Under-fetching Cafe. At this place, we sit down and we are given a rather interesting menu - it just has the names of the food categories on it like appetizers, entres, drinks, etc. No details of any dishes. You think that this must be some new trend that has come up since you have not dined out for a long time due to Covid. So you follow along and call the waiter asking for more details on appetizers, entres and drinks. The waiter then goes back to the kitchen and gets a separate appetizer menu and then again back to the kitchen and then comes back with the entres menu. Now this back and forth is making you crazy because you are hungry, so you think this is a lost cause and while the waiter is away getting the drinks menu, you leave the place.

You then enter the place next to the one you were in - The Over-fetching Cafe. Here you sit down and call the waiter ad ask for the menu. The waiter then disappears into the kitchen and doesn’t turn up for a long long time. It’s been almost 15-20 mins now and finally you see the waiter come with your menu. You are already questioning the service of the place. But you are really hungry and you take the menu only to find out that it’s not a menu but a 120 pages book with all this unnecessary information about the dishes, their origin, the recipes and god knows what not. You are tired looking at the menu and you decide that it’s better to leave this place because you are not going to be fed any time soon.

You need food now and this next restaurant is your only hope. You enter this place called the GraphQL Cafe. Here you sit down and something different happens. Instead of giving you the menu, the waiter first asks you what would you like to eat? Since you have already spent so much time deciding what you eat, you ask him about Italian appetizers and Mock-tail drinks. The waiter then goes back and comes back with what you asked - Italian appetizers and Mock-tail drinks. The menu is simple with just the needed information. You happily order and are quickly fed. The world is rosy again :)

With REST API, we commonly face the problem of Under-fetching and Over-fetching data. And this is because, our endpoints with REST API are fixed. Every endpoint is configured to send back fixed data. But with GraphQL, we move the Data Requirements from the server side to the client side, meaning we give the client the control to ask for exactly what they want and get back just that data. And we achieve this by configuring a single smart endpoint rather than having multiple dumb endpoints.



Continued in Part 3

16 views0 comments

Comments


bottom of page