4 reasons why you should use GraphQL over REST APIs
Tags: webdev, javascript, graphql, react
Published: 2021-01-05 05:38:12

REST has been preferred by many developers to send data over `HTTP` because they didn't need to install additional software or libraries when creating an `API` though `GraphQL` is ordinarily introduced as a technology to replace the legacy of `REST APIs`. In this article, I’ll be explaining the **benefits, limitations, and differences** between these two, which will help you decide what to chose for your next project. So without further ado let's dive right into it.


**What is REST?**

**REST(Representational state transfer)** is an architectural style for providing standards between computer systems on the web, making it easier for systems to communicate with each other. With `REST` you separate the implementation of client and server, to achieve this we use stateless operations including (`GET`, `POST`, `PUT`, and `DELETE`) to send and receive resources.

The idea behind this `REST` architecture is that you would retrieve a resource by putting through a request to the resource’s URL and get a response (usually `JSON`, but it depends on the `API`).

**Benefits of REST**

* Rest is **scalable** as it separates the client from the server and gives you ability to scale your application with ease.

* **Flexibility** is another advantage of REST as `Data` is not tied to resources or methods, so **REST** can handle different types of calls and return different data formats.

**Limitations of REST**

**Over fetching:** This is when the API endpoint provides way more information than required by the client.

**Under fetching:** This is when the API endpoint doesn’t provide all of the required information. So, the client has to make multiple requests to get everything the application needs.

_We'll use an example to understand well the above concepts_


**What is GraphQL?**

**GraphQL** is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more. In addition to this, it lets you combine different entities into a single query.

**Benefits of GraphQL**

* **Retrieve precise data, and nothing extra.** In GraphQL, you get what you request and nothing more, which is good.

* **Faster development in the Client.** Usually, when there are changes in the data requirements, you would just need to modify the query and there isn’t much change required, thus allowing rapid product iterations. Both the client and server development teams can work independently, as long as both the teams know the structure of the data. **i.e** client and server implementations are independent to each other.

**Example comparing both of them**

Let’s suppose, for example, we are displaying a user’s feed with a list of the user’s post and his/her followers. In our case, we have to display the author of the post, the posts as well as the followers for that user.

If we were to use `REST`, we would have made at least 2 or 3 requests, similar to this:

* `/user/<id>` to get the User(Author) details likely the username.
* `/user/<id>/posts` to get the list of posts posted by that user.
* `/user/<id>/followers` to get the list of followers for that specific user.

But in all these cases we are over-fetching the data. For example, in the first request, we need only the name, but we get all the details related to the user when we use this approach.

This is when `GraphQL` shows it’s potential. We need to specify the query and we can get the desired output. To achieve the same using `GraphQL`, we can use a query similar to this:

```javascript
query {
  User(id: '123') {
    name
    posts {
      title
    }
    followers {
      name
    }
  }
}
```
By using such a query we will be able to get a JSON response with the following properties. Clean and Simple, right?

**GraphQL vs REST**
To sum up, here are some couple of standout differences between `GraphQL` and `REST`:

**1. Data fetching**

`REST` causes **over-fetching** or **under-fetching**, whereas this isn’t the case with `GraphQL`. In `GraphQL`, _What you ask for is what you get_.

**2. Object definition (JSON response)**
In `REST` you can define `the request object` on the `Backend` and in `GraphQL` you define the object on the `Frontend`.

**3. Automatic caching**

`REST` automatically puts caching into effect whereas `GraphQL` has no automatic caching system, but using clients such as Apollo Client, Relay, etc. will make caching possible. **Caching** enables your client to respond to future queries for the same data without sending unnecessary network requests

**4. Error Handling**

Error handling in `REST` is much simpler as compared to `GraphQL`, which typically gives you a `200 OK` `status code`, even if there’s an `error`. But, when using clients such as `Apollo Client, Relay, etc`, it is very much possible to handle errors easily.

**GraphQL works best for the following scenarios**
* Apps for devices such as mobile phones, smartwatches, and IoT devices, where bandwidth usage matters.

* Applications where nested data needs to be fetched in a single call.

* A composite pattern, where an application retrieves data from multiple, different storage APIs.

**Conclusion**
`GraphQL` certainly has many advantages over `REST`, but it might not always be the **best** implementation. Like I said earlier, the choice depends on your application, whether to choose `REST` or `GraphQL`.


I hope this might help you make decisions in your future projects. If you like to share your experiences about `GraphQL` or `REST`, drop them in the comments section. Don't forget to connect with me on  [Twitter](https://twitter.com/graphicblessing) and 
[Linkedin](https://www.linkedin.com/in/hirwa-blessing/). Thank you for reading 😊!