Is GraphQL better than REST?

Reading Time: 4 minutes

In this blog, we will be going to explain a brief difference between GraphQL and REST APIs, Why GraphQL comes into the picture, and give some benefits over REST APIs.

So without any further delay let us begin with an understanding of brief about GraphQL and REST APIs.

GraphQL is a new API standard that provides a more efficient, powerful, and flexible alternative to REST. It was developed and open-sourced by Facebook and is now maintained by a large community of companies and individuals from all over the world.

In other words,  GraphQL is a technology that can be used everywhere a client communicates with an API. With the help of this, you can make changes to your request to match your exact requirements using this query language. In addition to this, it lets you combine different entities into a single query.

Today, GraphQL is used by a lot of popular companies such as GitHub, Twitter, and many more.

graphql-users

REST (REpresentational State Transfer) APIs and web services provide an easy way to communicate with a backend architecture without the need to understand or mess with the architecture itself. In other words, you will interact with one or more endpoints (i.e. URIs) that are part of an API that resides on a server or backend.

It allows systems to access and manipulate the textual representations of web resources using a predefined set of stateless operations (including GET, POST, PUT, and DELETE).

Now we will be going to analyze and try to figure out that GraphQL is better than REST or not on the basis of multiple scenarios?

Data Fetching

This is one major scenario in terms of accessing an API endpoint to get the data. So in the REST, we may have to hit multiple endpoints. Like if consider the Example1 given below in which we have student details and we want to fetch the address of a particular student. Then, in this case, we could be needed to hit this API /student/id to fetch the initial student data. And, then to get the list of assignments done by the student we are likely to hit another API like /student/id/assignments shown in Example2

Example1:

{
“student”: {
“id”: “e4fg33d”,
“name”: “Harry”,
“age”: “15”,
“email”: “harry15@gmail.com”
}
}

Example2:

{
“assignments”: [{
“id”: “1”,
“name”: “Assignment1”
},{
“id”: “2”,
“name”: “Assignment2”
}]
}

On the other hand, with the help of GraphQL, we just simply need to send a single request to access the GraphQL server that includes concrete requirements.  Then the server responds with the help of sending JSON as per the query requirements shown the Example below.

Example:

query {
Student(id: “e4fg33d”) {
name
age
email
assignments {
name
}
}
}

And, with the help of single query by leveraging the capability of GraphQL we will be able to get all the data for which we need to hit 2 API endpoints using REST rather.

Overfetching

While using REST we always need to face one problem which is Overfetching which means we will get more data than required. Also, We can’t make any changes or manipulate the design we required as per our need using REST due to its rigid nature. And, the only way we will get the data is by hitting endpoints.

But, with the help of GraphQL, suppose we need to display the information of a student which should only include the name and age of a student (refer Example1 given above), in this case, the GraphQL become handy because we can make changes to our query as per our need which shouldn’t be possible using RESTit will give u all the data including email which is not at all required in this case. 

Underfetching

There might be a problem where an endpoint using REST doesn’t provide enough information you need to display on the screen for which we have to hit and additional  APIs to fulfill our requirement. So, in this case, GraphQL plays a vital role in which it does the needful in only a single request.

Iterations

With REST is handy when we need to get all the information required to create a view. And, REST follows a pattern in which we need to design APIs according to the view required to display. But with this, there is no place for the frontend iterations so any change in the UI will make a backend to change according to which it was not the case with GraphQL.

GraphQL’s flexible nature is the solution to this problem. As changes in the UI can be made without disturbing the backend solutions. i.e, we don’t need to make any changes to the server if we have some change to be done at the client-side.

Type System and Schema

As we know, GraphQL uses a strong type system to define the capabilities of an API. All the types which are to be used in the API are written in a schema using GraphQL schema. It serves as a contract between the client and the server.

Both frontend and backend can start with their work once the schema is defined as they all are aware of the structure od the data which is sent. Also, the frontend team can start their work with the mock data as the have a proper understanding of structure they are gonna get to complete their work.

Conclusion: I think after reading this blog, you will get an understanding of why GraphQL is better than REST and you surely start implementing it in your new projects or replace the REST from the existing one.

Thanks For Reading!!!

image-1-1

Written by 

Nitin Arora is a Software Consultant at Knoldus Software LLP. He has done MCA from the Banarsidas Chandiwala Institute of Information technology, Delhi(GGSIPU). He has a graduation degree in BCA from Jamia Hamdard. He has a sound knowledge of various programming languages like C, C++, Java. Also has a deep interest in frontend development like Html, CSS, Angular, Javascript, ionic, react with redux, bootstrap. He is currently working in frontend technologies like React, Html, SCSS, Bootstrap, and Typescript. He is a focused, hardworking, team-oriented member and always exploring new Technologies, His hobbies are to play cricket, volleyball, and do coding.