Is GraphQL better than RESTful API?

I am writing this article because a few months ago I worked on a scraping application where I needed to scrape overwhelmed data per request using RESTful API. In this application my involvement was mainly in backend side, so on that time I took a lot of unspoken burden based on consumer demand, I need to write the same APIs multiple times for different data sets.

So, in this article I will try to focus on some situations and try to get out some points regarding, why GraphQL API is more flexible than REST API.

Let’s start,

What is GraphQL API?

GraphQL is a data query language developed internally by Facebook in 2012 before being publicly released in 2015.

Mainly GraphQL gives a complete and understandable description of the data in your API. By using GraphQL API, we can think completely new way about our API architecture, that is I think efficient and more flexible.

Send a GraphQL query to your API and get the response exactly what you need, nothing more and nothing less.

What is REST API?

One of the most popular type of API name is REST. It is a very traditional approach and commonly known as RESTful API.

REST allows easy and rapid evolution of an API design. REST demands the use of hypertext, which scales very well since the client and server are very loosely coupled.

RESTful APIs have four very important data transactions that uses HTTP requests to GET(READ), PUT(Edit), POST(Create) and DELETE data.

REST VS GraphQL API

Suppose, we need to scrape  a news portal website where everyday thousands of news are published publicly and every news contains lots of information and overwhelmed user comments. If we use REST API for this system we have to make almost ten different API calls to get different information and each API call gives us in return a lots of information. Most of the time we just need one data suppose just ‘title’ of the news but our API response request contains hundreds of data records that’s very useless and we really don’t need. So REST gives us lots of excess which is very unnecessary.

But in GraphQL API If we want to developed same system we just need to make one API call and return It will gives us one response. GraphQL API is very specific and return It gives us what we exactly need.

So mainly GraphQL solve the over fetching and under fetching problem of REST API. That means, GraphQL things make more easier which retrieve and manipulate data more efficiency based on client request where, REST has no way to limit the response of data and always It’s contains subset of fields.

Using REST API, we need to make different request APIs for different endpoints to fetch required data.

For example, we need to get post informations of single post Id so typically we call /posts/1 endpoint and for the same post If we need with comments then we will use /posts/1/comments endpoint. So basically we need to make call two different API endpoints.

GET   /posts/1
GET   /posts/1/comments

And each response return lots of data with over fetching additional information.

REST API Sample Data Example

On the other hand, In GraphQL we just need to make one request call to get the post title and comments. It is so simple and flexible way to control your API any time as per your required data.

GraphQL API Example

But in real scenario everything has some limitation GraphQL also have. Like, It is very large learning curve, caching is not available but for the cache we can use extension and finally lack of versioning difficult to manage code for migrations etc. Elsewhere, in REST, It is very time consuming and easy to integrate, we can send multiple request to get the data multiple levels and caching also available.

In conclusion, GraphQL is a new way to interact with a server and REST used to calling a specific URL in order to get some list of data from server. But GraphQL things make more easier, It’s retrieving data and manipulate more efficient because It’s encourage using only the data needed based on client request.

Additionally I wrote some article based on GraphQL, please check this article at — https://blog.cloudboost.io/a-crud-app-with-apollo-graphql-nodejs-express-mongodb-angular5-2874111cd6a5

I am available at twitter, knock me If you think we can discuss further in short.