Dev. to/blessingart

By Hirwa Blessing

Many developers prefer to send data over hypertext Transfer protocol because they don’t need to install additional software or libraries to create application programming interfaces, although GraphQL was generally introduced as a technology to replace traditional REST application programming interfaces. In this article, I’ll explain the benefits, limitations, and differences between the two, which will help you decide what to choose for your next project. So without further ado, let’s get right into it.

What is REST?

REST (stands for State transition) is an architectural style used to provide standards between computer systems on a network, making it easier for systems to communicate with each other. With REST, you can separate the client and server implementations, and to do this, we use stateless operations including (GET, POST, P, and DELETE) to send and receive resources. The idea behind this REST architecture is that you can retrieve a resource by making a request to its URL and get a response (usually JSON, but this depends on the APPLICATION programming interface).

The benefits of REST

  • Rest is extensible because it separates the client from the server and enables you to easily extend your application.
  • Flexibility is another advantage of REST, because data is not tied to resources or methods, so REST can handle different types of calls and return different data formats.

Limitations of REST

Overfetching: This is when an API endpoint provides far more information than the client needs.

Extracting: This means that the APPLICATION programming interface endpoint does not provide all the required information. As a result, the client must make multiple requests to get everything the application needs.

We will use an example to better understand the above concepts

What is GraphQL?

GraphQL is the API’s query language and the runtime that implements these queries using existing data. GraphQL provides a complete and easy-to-understand description of the data in your API, giving customers the ability to ask for exactly what they want, and nothing more. Among other things, it allows you to combine different entities into a single query.

The benefits of GraphQL

  • Retrieve accurate data without anything extra. In GraphQL, you get what you’re asking for, and that’s it, which is great.
  • Faster development of the client side. Typically, when the data requirements change, you only need to modify the query without much change, allowing for rapid product iterations. Both the client and server development teams can work independently, as long as both teams know the structure of the data. That is, the client and server implementations are independent of each other.

Compare the two examples

For example, let’s say we are displaying a feed for a user that contains that user’s posts and a list of his/her followers. In our case, we have to show the author of the post, the post, and the followers of the user. If we were using REST, we would make at least 2 or 3 requests like this:

  • /user/ Get user (author) details possible user name.
  • /user//posts gets a list of posts made by the user.
  • /user// Followers get the list of followers for that particular user.

But in all of these cases, we’re over-accessing data. For example, in the first request, we only needed the name, but when we used this approach, we got all the details relevant to the user. This is where GraphQL shows its potential. We need to specify the query so that we can get the desired output. To achieve the same functionality with GraphQL, we could use a query like the following:

query {
  User(id: '123') {
    name
    posts {
      title
    }
    followers {
      name
    }
  }
}
Copy the code

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, there are some notable differences between GraphQL and REST:

1. Data extraction

REST causes ** to be over-extracted or under-extracted, which is not the case with **GraphQL. In GraphQL, all you ask for is what you get.

2. Object definition (JSON response)

In REST, you define request objects on the back end, and in GraphQL, you define objects on the front end.

3. Automatic cache

REST implements caching automatically, while GraphQL does not have an automatic caching system, but using Apollo Client, Relay, and other clients will make caching possible. Caching enables your clients to respond to future queries for the same data without sending unnecessary network requests

4. Error handling

Error handling in REST is much simpler than GraphQL, which usually gives you a 200 OK status code, even if there are errors. However, when using Apollo Client, Relay, etc., it is easy to handle errors.

conclusion

GraphQL certainly has many advantages over REST, but it may not always be the best implementation. As I said earlier, the choice depends on your application, whether you choose REST or GraphQL.

I hope this will help you make decisions on future projects. If you want to share your experiences with GraphQL or REST, please put them in the comments section. And don’t forget to connect me to linkedin on Twitter. Thank you for reading 😊!