GraphQL Over REST

Awadhesh
3 min readJan 31, 2021

Do you really know when to choose GraphQL over REST? Here I will try to explain which scenario GraphQL is a better option than REST.

REST is a very much popular way to write HTTP API. But REST Architecture has few limitations like below.

  1. Over-Fetching and Under fetching of data

REST API always returns a complete set of any attribute in the API response. Let's consider we have an API for fetching User data. So our API will look like below

v1/api/user/id/123

The response will be like below :

{
“results”: {
“userId”: 123,
“username”: “ABC”,
“userAddress”: {
“id”: 12,
“name”: “XYZ”
}
}
}

But in case if any client like mobile client needs only 2 attributes like userId and userName for their use case, but still they will get complete data (including user address attribute). So this is called Over-fetching problems.

what is the solutions to this problem (over-fetching )in REST implementation?

We can either develop another endpoint (API) that will return only 2 attributes like userID and UserName for example. But this will lead to problems of many API(The number of API will increase).

We can think of another solution like pass attribute name in Query param, whatever field is required in Response. But to achieve this we need to write so many if-else conditions and API also will be like a mess (because of passing attributes Name in Query params.)

Big problem ????????????

We have solutions to these problems. The solution is GraphQL.

How GrapQL solves data over-fetching and under-fetching problems.

GraphQL has built-in solutions for this. Since GraphQL is a query-based language so it handles this well. It lets you fetch only all relevant data in a single query

2. Save Network Bandwith for the client.:

Since the client can fetch only relevant attributes in their Query unlike REST send all attributes in the response.it saves a lot of Bandwith of client and application load Time will improve.

3. Save Memory also on the client-side :

Since the client can fetch only relevant attributes in their Query unlike REST send all attributes in the response.it saves a lot of memory usage and CPU processing of client-side application and application load Time will improve.

4. Prevent Multiple API calls :

lets consider a scenario we need to fetch data of users, their post and their followers.

if we have to implement the above use-case then. need to make 3 separate API call like :

v1/user/id/123

v1/user/id/123/posts/

v1/user/id/123/followers /

But using GraphQL all related data can be fetched in Single Query like.

query {
user(id :123) {
id
name
post {
id
name
}
followers{

}
}
}

5. Versioning is Not Required

In REST architecture, we create new versions due to changes in resources or the request and response structure of the resources over time. Hence, maintaining versions is a common practice. With GraphQL, there is no need to maintain versions. The resource URL or address remains the same. You can add new fields and deprecate older fields. This approach is intuitive as the client receives a deprecation warning when querying a deprecated field

I tried my best to give some idea when to choose over REST. I will explain the complete GraphQL Basics in the next article.

Happy Learning !!!!!!!!!!!!!!!

--

--