BlogTechnologyGraphQL Over REST

GraphQL Over REST

Synclovis Systems -
Web and Mobile (iOS and Android) application development company -
Blogs -
Graph over rest

Why GraphQL is dominating REST?

We have been using Rest APIs from a long time which gives us a lot of flexibility in terms of Data Formats, better support to web clients,speed with lesser bandwidth and many more. Most of the servers for making APIs for Mobile, Webpages now-a-days are built on REST protocol.

If everything works as intended while using REST then Why we should now consider using GraphQL?

The answer to this question is pretty simple. GraphQL gives a better platform for data synthesizing,better speed,better memory graphing etc.The APIs developed on REST protocol become quite inflexible when it comes to some sudden client end requirements.

One of the major reasons where GraphQL dominates the REST protocol is Data Handling. Let’s take an example where we have developed two REST API which contains data such as:-

  • id
  • name
  • author
  • author

Now, whenever this API is called it will return all the data it contains, which is quite useful in some cases. Now let’s think of the case where we are using Books API. But instead of using all the 4 data, we only need 3 data i:e; Name, Author & genre.In such cases,while using REST API, we will all the five fields irrespective of the payload we pass and the actual requirement of the client, which can be called Over Fetching of data which will cause a delay in data processing & more memory allocation.

This is the place where GraphQL APIs comes into play. With the help of GraphQL, we can query for only that fields which is required by the client-end.

Let’s take a dive into coding prospective for a better understanding:

.

CASE OF REST API :-

API ENDPOINT:- ‘/fetchBooks’
Requirement:- Name & Author Name

Once this REST API is called, then we will get the data in the following way:

			{
				"id":"1",
				"name":"Hamlet",
				"author":"William Shakespeare",
				"genre":"Drama",
			},
			{
				"id":"2",
				"name":"Odyssey",
				"author":"Homer",
				"genre":"Epic",	
			}

			

In the above case, the requirement was only Name & Author Name, but instead we got all the data some of which is completely useless.

CASE OF REST API :-

API ENDPOINT:- ‘/fetchBooks’
Requirement:- Name & Author Name

In case of GraphQL, we just need to pass the required fields name as arguments and then we will get a response in the following way:

			{
				"name":"Hamlet",
				"author":"William Shakespeare",
			},
			{
				"name":"Odyssey",
				"author":"Homer",
			}
			

In the above case, the response which we received is the exact fields which was the requirements.in this way we completely get rid of the useless which will further help in betterment memory allocation,data complexity.

Error Handling

In case of REST API the response code which we get on a successful API call is 200 OK. Similar way, in case of any kind of error we get responses such as 404,403,500,503 etc, which can be easily referenced to know which type of has occurred. Whereas in case of GraphQL APIs, On every API call, we get the status code of 200 OK and in case when any error occurs during the network call, a complete error message is sent from the server end to client side.

Example In Case of REST API:

			{
				"error":404
			}

			

Example in Case of GraphQL API:

			{
			  "errors": [
				{
				  "message": "Field \"some field" must not have a selection since type \"String\" has no subfields.",
				  "locations": [
					{
					  "line": 23,
					  "column": 45
					}
				  ]
				}
			  ]
			}
			

From the above case, We can clearly see the difference between the Error Handling Methodologies.

What do we conclude?

At the end, Both REST & GraphQL are just different ways of calling the API with their own Pros & Cons. Moreover, if you have used REST and build you API’s on this protocol, you won’t feel a lot of challenges in implementing GraphQL also.

Some of the key benefits of GraphQL over REST are:-

  • Non Repeating Network calls: We don’t need to do multiple round of networks calls for getting the data.What all data we require to use can be fetched in a single API call.
  • Reduced Data Complexity: As we can only fetch the required data using GraphQL, Data complexity get reduced comprehensively.
  • API Call Time gets reduced: As we are able to fetch only the required data, the time required to fetch the data or run a query gets minimized.
  • Better Memory Utilization: As we fetch only the required data, Memory usage get rectified to a better level when compared to REST.
  • Better Error Handling: Error handling in GraphQL get much easier and clear when compared to REST.

Lead Software Engineer



Leave a Reply

Your email address will not be published. Required fields are marked *

Creating digital solutions for your business

Subscribe

Subscribe to stay updated with our latest Tech News & Blogs

  • About Us
  • Services
  • Industries
  • Technologies
  • Portfolio
  • Blog
This is a staging environment