Loading, please wait...

A to Z Full Forms and Acronyms

Do you agree GraphQL is a replacement for REST API? Let's see why ?

Jan 18, 2020 Services, Webapi, REST api, GraphQL, 2373 Views
In this article, we will discuss how GraphQL is used over Rest API
Hi everyone, this article will be about GraphQL & am targetting to cover below topics in this article. 
  • What is GraphQL ?
  • How GraphQL Works ?
  • Why GraphQL is an alternative to Rest API?
  • Real time scenario using GraphQL.
  • Types of Operations in GraphQL .
 
Hope we are good to go.
 
What is GraphQL ?
Actually GraphQl is an replacement to a REST API, i assume you already know what REST api is. Rest api has couple of end points and it is stateless, client independent api for data exchange.
 
We use this Rest Api for single page applications or mobile applications where we dont return any views to renders browsers. we just return/exchange the data between frontend.
 
So, here we can question, when Rest api is doing all this, why GraphQL is a alternative to this Rest API.
 
And the answer is, GraphQL offers same features what Rest API is giving but additionally GraphQL is highly flexibile.
 
So how flexbile GraphQL is ? to make it clear let's see what are the limitations over Rest API.
 
Rest API work with end points like GET/POST. So we can fetch the data and return the data in JSON type as shown below
 
 
Now let say we have a requirement that to GET only Id & title. In Rest API we always send back all the data whether we needed it or not.
To acheive above requirement, we have different scenarios to do so
 
Scenario :1
We just create a new Rest API endpoints, in this we send back the data only id & title.
 
I personally don't think this is a good way to do. Why because at the end of the day we will end up with lot of API endpoints as projects evolves over time. Whenever we need an new different kind of data you need to add new endpoint at the back end, this will lead to slowdown the entire process in major projects.
 
Scenario :2
Another scenario would be using query parameters as below
eg: Get/post?data=abc

So on this Get/post route above, we check the value of query parameter. Based on the value we return a subset of data. It certainely better compared to scenario -1. But in complex API projects this scenario will make more complex to understand.
 
Scenario:3
Now let's understand how GraphQL will help in this requirement. Using GraphQL we can build the backend which exposes end points/single endpoint. GraphQL is very flexible in exposing query language to frontend.

So then frontend developers can request all kind of data and get what they need which is required.

Hope you understood now why we choose GraphQL over Rest API.
 
How GraphQL works at backend ?
Ok, we all know that with Rest api we will send Get/Post/Put request to server. But in GraphQL it works totally different.

Do you wonder if i say GraphQL always sends Post request even if it get the data from server.

Yeah, it's true. We have only one single endpoint i.e., Post/graphql.

Now question is why GraphQL uses only single end point, reason is that GraphQL basically exposes query language to the front-end similar like SQL/NOSQL etc.,
Then front end will send command to the backend with such post request to fetch the data.
 
Query expression looks like this, this is what we send as text in post body to graphQL end point.

 
  • Query: Operation Types
  • User: End Points(object)
  • Name,Age : Requested Fields
There are 3 different types of Operation types in GraphSQL listed below
 
  • Query : It is a Post request in GraphQL to retreive the data.
  • Mutations : It helps to change the data.
  • Subscription : This is implemented by web sockets, if we want to push updates from server to client, we need established websocket connection to do this.
 
Conclusion:
At the end of this article, we understood,
 
  • What is GraphQL ?
  • How GraphQL Works ?
  • Why GraphQL is an alternative to Rest API?
  • Real time scenario using GraphQL. 
  • Types of Operations in GraphQL .
Would you tell us how you feel about this article ?
 
LIKE || SHARE || COMMENT
 
Happy Learning 
A to Z Full Forms and Acronyms