GraphQL – the future of API

Tram Ho

GraphQL is a very hot keyword right now, people are talking about it. So, what is GraphQL? And why is it so popular?

What is GraphQL?

Define

In short, GraphQL is:

A query language for your API

GraphQL is one of the “most modern” ways to build and query APIs. It is simply a syntax describing how we query data, usually used to load data from server to client.

GraphQL has 3 main characteristics that make it different from the rest:

  • It allows the client to specify exactly what data to retrieve.
  • Allows easy aggregation of data from multiple sources.
  • Use a type system to define data.

Why is GraphQL considered the future of API?

Talk about REST

Since the olden days, we have moved from SOAP to REST because of the benefits it brings us. It’s undeniable that REST has done a good job, but then the applications are becoming more complex and sophisticated, which leads to the APIs also growing and replicating, when REST starts. The first step is to reveal the disadvantages that are difficult to overcome if we continue to use it.

Too many endpoints

For each resource in REST is represented by an endpoint. In fact, we will have a lot of endpoints for a lot of different resources, if you want to make a GET request, you need an endpoint for that particular request, with specific parameters, corresponding to the Other methods. With a giant application like Facebook or Github, they will face having a huge amount of API and a lot of effort to manage and maintain these APIs.

Over-fetching and under-fetching

Another annoying problem of REST is over-fetching and under-fetching information, for example, we only want to get the User’s name but with only one endpoint get all the User’s information, we still have to use it, and the accompanying trivial information becomes redundant and unnecessary. The essence of REST is always returning a hard-fix structure, we can’t get exactly what we need without creating a dedicated endpoint for that.

Or when we need to get information from many resources, we will have to call to many APIs corresponding to each of those resources, as the number of resources is increasing, the amount of code to implement is more and more complex, it is difficult to manage.

Versioning

To avoid conflicts or new code that doesn’t run with old clients, APIs when updating often keep the old versions and adding new versions. For GraphQL, we do not need to do this, when we want to develop any API, we just need to add new code without having to worry about compatibility issues with the old client.

GraphQL is the future

Back in 2012, when Facebook faced with the problems encountered with REST as mentioned above, they developed GraphQL to solve those problems.

Single enpoint

Creating multiple endpoints is no longer necessary, with GrapQL, we only need one endpoint, and with it, we can retrieve the data we need in a single request.

Take only what is needed

We will not have the problem of over-fetching or under-fetching as in REST anymore, just define the data we need, and we will get the data we want. This also increases performance because we do not need to retrieve redundant data anymore

Share the news now

Source : Viblo