Habilelabs-Logo
Blog

Introduction to GraphQL and comparison between GraphQL and REST APIs

October 20th, 2018 . 8 minutes read
Blog featured image

GraphQL is another stuff in the technology stack which has great features. If you are building APIs then for sure you are going to love GraphQL. I know, for some, it might be just another name to a technology but after reading this blog, perceptions of many would change. I really hope so.

So, let me just tell you what you are going to get in this blog.
1. What is GraphQL? – GraphQL Introduction
2. Features of GraphQL.
3. GraphQL and REST similarities.
4. Comparison between GraphQL and REST.
5. Advantages of using GraphQL.
6. Who is using the GraphQL?

Let’s just begin the very small but informative journey to GraphQL.

What is GraphQL? – Introduction to GraphQL

GraphQL is a technology which is said to be the query language for the APIs. It evolved way back in 2012 by the global IT giant company Facebook. It was then used internally, but in 2015 Facebook released it publicly.

GraphQL is much efficient, powerful and flexible. It provides a complete and understandable API which has a nice description of the data.

In very common words if it has to described then it a set of pre-defined measures that help you get the data from the query in whatever quantity it is required without making requests to multiple points.

Now that you got some familiarity with GraphQL, let’s go through some of its essential features.

Top useful features of GraphQL

While you must be wondering why GraphQL and not REST, it is the time to first know some of the essential features of the GraphQL:

Top-useful-features-of-GraphQL

It is a query language

Yes, I know that I have already stated this in the introduction but as a feature, this makes GraphQL a stronger technology which sends queries to your APIs. This results in getting the optimized results.

Great ability to give what you want from the API

GraphQL has the ability to give you the exact results that you want with no less and no more data. It always helps in giving back the predictable results.

It gives many resources in a single request

GraphQL helps in accessing the properties of the resources and it also follows the references between them. GraphQL APIs get all the data that is needed by the app in a single request.

It is organized in terms of types and not the endpoints

GraphQL is useful in a manner that it has an organized structure of types and fields and not the endpoints. This helps in ensuring that apps ask what is possible and provide the proper error report. This feature also helps in avoiding the manual parsing of the code.

organized-in-term-of-types

Image Source: https://graphql.org/

It creates a uniform API across the entire application

This feature of GraphQL helps it to create the uniform API without limiting to a specific storage engine such as InnoDB of MySQL and WiredTiger of MongoDB.

These were some of the essential features of GraphQL which makes it an in-demand product. Now let’s know what is common between the GraphQL and REST.

GraphQL and REST similarities

Similarities-between-GraphQL-and-Rest-API

These are some of the similarities between the GraphQL and the REST approach in dealing with the APIs:
1. Both can be operated over the HTTP.
2. Both are specified for building and consuming the APIs.
3. Both are portable in nature.

Very fewer similarities. Right? Probably that is what the reason why you are here to find the differences. So, lets just quickly move towards the next section where you will get to know the comparative aspects of both.

Comparison between GraphQL and REST

Some of the comparison parameters or moreover the advantageous factors of GraphQL over REST are as follows:

GraphQL-Vs-REST

Calling and fetching of Data

As you were looking for the advantageous factor of GraphQL, this is the most appropriate one where the difference lies in the following way:

GraphQL has a unique feature of fetching the data. It has only one endpoint that helps in fetching the data over the server. REST API has multiple endpoints requests. An example to this will clear the meaning what I am trying to express.

You may also like: 4 Basic Rest API Design Guidelines

Suppose there is a student who has enrolled for a university program, so, using REST API student details can be fetched by student/:id and his student club data can be fetched through student/:id/club and his role in the clubs can be fetched using student/: id/club/:clubID/role. This approach has multiple endpoints every time when the details of the same student are fetched. But using GraphQL it can be fetched using single endpoint /student.

The query that would be formed will be like –

This will result in enough data in response that was required.

Over fetching and under fetching of data

REST APIs data are downloaded by hitting the endpoints that are meant to return a fixed data structure. So, while using the REST APIs this is the space of ambiguity that a lot of unnecessary data is fetched.

This creates a situation of over-fetching or under-fetching where either data comes in surplus or it doesn’t come as it was required respectively from the endpoints.

GraphQL helps in resolving this problem of an unnecessary load of data or avoid getting into the situation where data, as it was required, was not fetched. This also helps in making clear and fast execution of the queries.

Also Read: Top 11 Popular Web Development Trends to follow in 2018

Iterations in front-end

While REST APIs force the backend to be updated every time any change is done in the front-end (when needs change to get less or more data), GraphQL follow a flexible nature. Clients can specify the exact data requirements and this helps in making no adjustments and changes in the backend.

Error Handling

While REST APIs are very specific in sending the response status code on every request from the client (status code 404, 500, 503 etc) which easily tells what sort of error has come, GraphQL on the other hand when operated over HTTP always send status code 200 (OK) and in case of error the complete error message is sent to the client.

Network request handling

While REST APIs make multiple requests to the endpoints for the data, GraphQL reduces this burden over the network by allowing to fetch the data in a single query.

Analytics on the backend

While REST APIs need third-party library or extensions for analyzing the data, GraphQL uses the concept of resolver functions to collect the data that is actually requested by the client. These resolvers provide the essential details and insights about any bottlenecks in the system.

Now, that you have got the brief knowledge of comparative factors between REST and GraphQL, lets now focus on some advantages of using GraphQL.

Advantages of using GraphQL

By now you must have got a click in mind to simply know the advantages of GraphQL that really matter in API interactions.

Advantages-of-using-GraphQL

So, these are some of the advantages of using GraphQL:
1. Apps using GraphQL can be quick even on slow mobile network connections.
2. GraphQL has a clean layer of abstraction between servers and clients.
3. It has an ad-hoc approach (for a particular purpose) for dealing with the API data.
4. GraphQL has fewer roundtrips than REST.
5. It requires fewer resources in dealing with the APIs.
6. GraphQL provides a very easy deprecation process.
7. GraphQL rebounds the data for versioning. This makes versioning easy in comparison to REST APIs.

Although there are many useful features and advantages of GraphQL, it has few limitations which are listed below.

You should also Check: Why Most Of The Companies Use Node.Js?

Disadvantages of using GraphQL

1. Caching is not easy in GraphQL as it has no caching mechanism. Clients are responsible for taking care of caching.
2. Authorization problems still prevail in GraphQL.

Who is using GraphQL?

GraphQL has already gained a lot of popularity since its first public appearance.
Some of the major organizations which are using GraphQL are:
1. Coursera
2. Facebook
3. GitHub
4. Myntra
5. Paypal
6. Pinterest
7. Shopify
8. Stackshare
9. Twitter
10. UC Trend
11. Yelp

And there are many more who are using GraphQL. This is just a few.

Bottom Line:

There is a huge increase in the number of developers and companies using the GraphQL in the past few months. With no doubts, it is one of the fast-growing technology that organizations are opting. It has changed the methods of interaction with the APIs. REST is getting a tough competition from GraphQL now.

Habilelabs is a premier software development company that provides professional services including the latest technologies. It is rich with the resources to develop the feature-rich Angular, React and NodeJS applications using GraphQL and REST. Give us a call to find out more about our awesome services!

I hope this blog must have helped you in getting closer to GraphQL and its features. For any queries, you may feel free to comment in the comment section. You can also share your views on GraphQL in the comments section below.

Author: aishwarya
Share: