GQL vs REST API - Do You Know Which One Is Better?
GQL vs REST API - Do You Know Which One Is Better?

In this post you will learn the differences between REST and GraphQL. First of all I want to compare them and after that share with you my way of using GraphQL that you might find appealing.

What is it?

The first question is what is REST and what is GraphQL?

REST is a representational state transfer

Which actually means that we define our requests on the server.

REST

Like for example with /artists we get a list of our artists, with /artists POST request we create an artists and we have specific URL /artists/:id for updating and deleting artists.

This is how REST API typically looks like. But what do we have in GraphQL?

GraphQL

As you can see here it looks like a tree of nodes that we can request. Essentially this is just a single endpoint where we provide a structure that we want to get back.

error

And the strongest point why all people like GraphQL so much is because it is strict and it is impossible to break. There is such tool which is called GraphQLI which is a visual UI for GraphQL where we can see all requests that are created inside GraphQL with every single field and all inputs that we can provide inside.

Which actually means when you use GraphQL you are getting out of the box the schema of all your requests.

swagger

You don't have something similar out of the box with REST. There are some third-party libraries which can do that but it is not an out of the box solution.

In REST realistically it's a wild west - you can change your API at any moment, sometimes it is not even RESTfull API. You can just create some URL, accept there any data and you are good to go.

GraphQL on another hand is a special layer between server and client which describes how client should request information through API

And inside this layer with schema we define all our requests.

Getting only needed data

Rest data

One more important difference is that inside REST we simply create all our endpoints on the backend and we can't change them from the client.

GraphQL data

Inside GraphQL is it different. Yes we create our endpoints on the backend but we can specify what data we want to get back from our API in this specific case. This is actually the first point when you need to use GraphQL.

If you want from the client to request only specific data that you need and not all data then GraphQL wins REST.

Combining data sources

The second point when you want to use GraphQL is when you have multiple data sources. Which actually means that you have several endpoints and you want to collect all these data or you have data with references.

query {
  users {
    _id
    name
    posts {
      _id
      title
      description
      category {
        _id
        name
      }
    }
  }
}

For example a list of users and every single user has relations to his posts where each post has a category. Inside REST API you have 2 ways. You either make a single ugly request or you have several different requests to get all this data. Inside GraphQL you can combine all these requests.

Bandwidth

The next point to use GraphQL is bandwidth problems. If we are talking about bad internet or mobile devices it makes a lot of sense to request only data that you need as with GraphQL you can specify what fields exactly you want to get from the backend.

Prototyping

Another point to use GraphQL is when you need rapid prototyping. When we use REST if we need an additional property on the entity which is not exposed yet we must change it not only on the client but first on the server. With GraphQL it is different. We can directly on client change what properties it can get from the entity. It means that in a lot of cases you don't need to tune your server side code but it is enough to only update your client.

Performance

Now let's talk when does it makes sense to use REST API. Previously I said lots of good things about GraphQL and you might think that GraphQL is awesome and there are no problems in GraphQL.

This is not true and the first problem in the list is performance. We already talked how we can combine a lot of requests together. But here is the problem.

Performance

Your request will be extremely slow because all these requests in the database must to joins or additional queries. It is extremely easy to make it slow. You must understand what you are doing from both sides to implement it correctly.

Do it simple

The second point is also related to complex queries. Sometimes it is much easier to create several queries to take information without combining them in a single request. Again you can do this in both REST and GraphQL but people with GraphQL tend to do complex queries and fetch all data in a single request. Sometimes it is just not the best idea.

My way

Now I want to share with you my thoughts regarding GraphQL. I personally prefer to use GraphQL over REST for all my projects. From my perspective GraphQL is amazing in comparison to REST API.

My way

I can see whole schema with all requests and all fields that I implemented inside GraphQL. It is similar to Redux Devtools where we see the whole state at once.

Inside REST you never know how you need to use specific endpoint. With GraphQL you never need to ask your backend developer what data and in what format you must deliver to get the correct response. It is all written inside UI.

The second point which is important for me is that you are not forced to use GraphQL like a huge super performant tool where you want to combine lots of entities in a single request.

You can still you GraphQL like REST API with different endpoints.

So in this ways you kind of use something similar to REST API but just with schema and it is much easier to read and support.

And actually if you want to improve your Javascript knowledge and prepare for the interview I highly recommend you to check my course Javascript Interview Questions.