GraphQL over REST

REST and GraphQL are the ways of sending data over HTTP.

REST i.e., REpresentational State Transfer is a set of rules that developers follow when they create APIs and was used in many application stacks in the past few years.

REST is about creating representationof object’s current state and transferring that representation over network

GraphQL is a strongly-typed query language for APIs, developed by Facebook in 2012. Later in 2015, Facebook open-sourced it. It can overcome many shortcomings of REST. You will be going through them in this blog. It provides a new API standard that provides a flexible and efficient alternative to REST.

model your business domain as a graph by defining a schemanodes and how they relate to one another.

What makes GraphQL better than REST?

“When we built Facebook’s mobile applications, we needed a data-fetching API powerful enough to describe all of Facebook, yet simple enough to be easy to learn and use by our product developers.
— GraphQL co-creator and former Facebook engineer, Lee Byron.

Let us see the difference between REST and GraphQL.

Ask for what you need: Increased mobile usage creates the need for efficient data loading. With GraphQL, it becomes faster as it minimizes the amount of data sent over the network. In GraphQL, there is a single endpoint that serves all the required data. Whereas, in REST there are multiple endpoints to retrieve the data. This leads to unnecessary round-trips.

Fig. Example showing the difference between REST and GraphQL API calls

If you want to display the address of the user with user_id=1 using REST, then it will hit an endpoint /api/address?user_id=1, where the entire data consisting of stress and city details will be received as shown in the above example.

“The company chose to use GraphQL because of the limitations of REST. The amount of over-fetching we were doing was insane.”
— Adam Neary, a tech at Airbnb.

The major drawback of REST is that one can only download predefined bundles of information. If you want only some information about a bundle, you still have to download the entire bundle and throw out the parts that you don’t need. Also, if you want some part of every bundle then you have to download all the bundles and then reorganize it yourselves. Over-fetching and under-fetching faced in REST are overcomes by GraphQL.

In simple words, the scenario in REST is as shown in the image below.

“GraphQL makes orchestrating data fetching so much simpler and it pretty much functions as a perfect isolation point between the front end and the back end”
— Viktor Charypar, software engineer at Red Badger

The benefit of the Schema and typed system: Schema consists of all types that are exposed in an API. Once the schema is defined, the teams working on the frontend and backend can work independently as they are aware of the definite structure of data that would be sent over the network.
You can go through this link for more details about how actually a schema looks like and how you can create your GraphQL server using ExpressJS.

Real-time updates with Subscription: REST helps us in performing the GET, POST, PUT and DELETE operation. GraphQL comprises of queries, mutations, and subscriptions.GraphQL query is similar to the GET operation in REST. It is used by the client to request the data it needs from the server. Mutation gives us the capability to perform post, edit or delete operations. Real-time applications need to be immediately informed about the event. Hence they need a real-time connection to the server.GraphQL provides a concept of subscription. When a client subscribes to an event, it will hold a stable connection with the server. Whenever an event occurs, the server will prompt the corresponding data to the client.

Conclusion

I hope you have a clear picture of the main differences between these two concepts. REST requests always return bundles of data and hence cannot retrieve a subset of it. Also, it deals with a single resource at a time and hence multiple round trips to the server are required if you need data that comes from different resources. With GraphQL, you can predict the response and also you get what you have asked for. Hence, GraphQL is faster and flexible as it avoids additional round-trips to the server. This leads to overcome the major shortcoming of REST.

REST APIs are Rest-in-peace APIs. Long-live GraphQL.
Pradnya Dalvi

Pradnya Dalvi