Open In App

Why Use GraphQL?

Last Updated : 26 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

GraphQL is the query language for the Application Programming Interfaces (APIs). It provides a complete description in your API so that the necessary information will get fetched. It provides power to clients to ask for exactly what they need and nothing more.

In this article, we are going to look at how GraphQL is different from others such as REST, SOAP, and GRPC. These are some well-known and most-used API developing frameworks that most developers use. We will compare and look at why GraphQL stands above these frameworks and why GraphQL is the future for developing APIs.

Why Use GraphQL?

There are many ways to develop APIs like SOAP, gRPC, and REST. The two most popular approaches are using GraphQL and REST to develop APIs. GraphQL is developed to tackle the issues that we face when working with other API-developing frameworks like REST, SOAP, etc.

The GraphQL provides the following features and advantages over its competitors:

1. Fast Development

GraphQL is client-driven and provides power to clients to ask and get only the required information. The client-driven is the architecture in which the client is responsible for making requests to the server. And, with the help of GraphQL, the client will receive the required information that can be used to update the interface.

Points to notice

  • Client defines GraphQL queries to get the exact data
  • Then, GraphQL resolvers resolves query and gather or fetch the required data from sources
  • Here, Over-fetching and under-fetching are minimized because client is getting only the required data

This is why GraphQL development is fast and efficient compare to REST as REST is one of the most used for developing API.

Features

GraphQL

REST

gRPC

SOAP

Data retrieval

Client driven – client specifies the exact data needed

Server driven

Client driven, protocol buffers

Server driven structure

2. Contract Based Schema

In GraphQL Schema is like a blueprint that specifies how your data will be and what operations the client can perform. It defines what actions and operations a client can perform to fetch particular data.

A contract in GraphQL is like an agreement between the Client and the server. The client asks for data and the server provides the data.

Now, when developing an app it is important to tell other developers or let other developers know what data your GraphQL server is going to provide and what kind of queries it will accept. This will help you and other developers to make queries specifying what kind of data they need. This is called contract-based approach.

Here’s an example of contract based schema:

type book {
id: ID!
title: String!
author: User!
}

type ReaderID {
id: ID!
name: String!
email: String!
}

type Query {
getAllBooks: [book!]!
getBookById(id: ID!): book!
}

In the above schema we have defined three main types :

  • Book
  • ReaderID
  • Query

Book Type :

In the schema we have defined what fields does the Book type have. It has id, title, and author.

ReaderID Type :

ReaderID type represents ReaderID object that have three fields – id, name, and string.

Query Type :

Query type represents query objects and have two fields – getAllBooks, and getBooksById.

3. No Under-Fetching and Over-Fetching

What if you get unncessary data than the data you actually needs?. And, what if sometimes you don’t get the enough data from the sources?
Quite a headache to handle while developing an app and improving user’s experience. This is actually what happens while working with REST.

GraphQL is the solution for the problem, remember about contract based schema where the client defined the schema of what data can be provided by GraphQL server. Here the GraphQL queries will help to get the required data no more no less.

Look at the illustration :

UnderFeteching

In the above illustration, we wrote a GraphQL query asking only for the necessary information

  • Id of the user
  • Articles written by the user
  • Followers : But here, we have defined how many followers data we want, if this calling of API have been done using REST we will get all the followers details unnecessarily.

Under-fetching represents the scenario when the client asks for the data but did not receives the enough data to fulfill the client’s need. Thus, resulting in creating multiple requests effecting over all performance of the application.

Over-fetching represents the scenario when the client asks for the data but receives more data than the client need i.e unnecessary data. This can leads to longer response times, large data transfer and impacting overall performance.

GraphQL resolves the issues of over-fetching and under-fetching by allowing clients to ask what exactly data they need and fetching the exact data no more, no less just the required data.

4. Real-Time Data With Subscription

Real-Time data with subscription is one of the most powerful feature of GraphQL. GraphQL subscription allows client get real-time updates when specific event occurs on the server. This features is crucial if you are developing a chat application, and any other application that requires real-time updates.

What is Subscription?

Subscription is the special type in GraphQL that defines stream of data. Subscription are defined in the schema alongside queries and mutation. This powerful features is one of the crucial feature that shows the potential of GraphQL. With the help of subscription a developer can implement real-time updates in the application while maintaining the performance.

Example of writing subscription :

type Subscription {
currentUpdate(Id: ID!): MessageAlert
}

It starts with Subscription type and have a object that have one field currentUpdate(id).

GraphQL Over REST

As rest becomes popular among developers to develop APIs. REST offers some great features such as stateless servers etc. But the inflexibility of REST somehow becomes it’s down point.

Now, when it comes to GraphQL it climbs over the REST down-points like inflexibility, under-fetching, and over-fetching (explained later in this article). Providing necessary information, removing the need for multiple endpoints, and many more features resulting in overall improved performance of an application.

Fetching of Data Using REST

We gather the data by accessing the multiple endpoints when we use REST API. In the below example, we initially have/users/<id> endpoint to fetch user data.

REST_Fetching01

There is a second endpoint /users/<id>/articles to fetch articles written by the user, and a third endpoint that is /users/<id>/followers to fetch the followers of the given user.REST_Fetching02

In the above examples, we can see we have to request multiple endpoints to access the data and additionally, we are receiving data that we don’t need. Thus, the above illustrations show the inflexibility of REST when the client is accessing data.

Fetching of Data Using GraphQL

The above illustrations show the problem with REST, but GraphQL handles that problem efficiently. Look at the below illustration to understand it better.

GraphQL_Fetching

As we can see we have only made one request with GraphQL and with REST we have to make three requests to access data. Furthermore, we are only getting the data that we need with GraphQL, and with REST we are getting access and unnecessary information that is not the case with GraphQL.

Benefits of GraphQL Over REST

Below are the benefits of GraphQL over REST that describes the benefits GraphQL has over REST and thus, summarizing the sentence ” GraphQL is the better version of REST”

Features

GraphQL

REST

Flexibility

Clients can request only the data they need, reducing over-fetching and under-fetching issues.

Clients receive a fixed set of data determined by the server. Over-fetching and under-fetching may occur.

Single Request over Multiple Request

Clients can make a single request to get all the data they need as this has been already shown in the above illustration, reducing the number of requests.

Rest requires many and multiple requests to different endpoints to get related data as explained in the above diagram. However, this leads to decreased performance.

Versioning

As GraphQL has flexible nature and because of this feature it is easier to introduce changes and resolve bugs.

In REST it can break the structure if versioning has not been done carefully.

Real-time Data

Real time updates with the use of subscriptions that enables instant data changes and helps to get real time data.

REST requires other mechanisms like polling for getting real time updates, thus making it less efficient.

Is it easy or difficult to learn

A little bit difficult to learn but if have mastered DSA with C++ then not a big deal for you, but offers more power and flexibility.

Easy to learn compared to learning GraphQL.

Network Efficiency and Network Load

As it has flexibility and reduces over-fetching and under-fetching that leads to increased network efficiency.

REST has to make multiple fetching requests to gather data leading to increased network load.

Conclusion

As all the above data shows and in fact, describes Why to use GraphQL and start learning GraphQL for developing API but it depends on your requirements whether to use GraphQL or Rest or any other API developing framework.

Suppose flexible data requirements, want to avoid over-fetching and under-fetching, a single request for multiple resources, efficient caching strategies, and many more solutions GraphQL provides. In that case, GraphQL is good to go. However, if you want to learn GraphQL check out their official docs and you can learn GraphQL using any language eg C++, JS, etc.

Companies like Meta, Starbucks, X, etc use GraphQL over any other API developing tool. So, it is a good time to start learning GraphQL and adding it to your skills inventory.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads