5

Stop Using REST For APIs

 3 years ago
source link: https://medium.com/javascript-in-plain-english/stop-using-rest-for-apis-d697727ae6dd
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Stop Using REST For APIs

GraphQL Is Way Better

Image for post
Image for post
Photo by bruce mars on Unsplash

REST has been used by many developers to send data over HTTP whereas GraphQL is typically presented 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.

What is REST?

REST(Representational state transfer) is an API design architecture that’s used to implement web services by using a predefined set of stateless operations (including GET, POST, PUT, and DELETE).

The core idea of REST 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 and server so that you can scale your application with ease.
  • Flexibility is one other advantage of using REST, as it can be designed to 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.

What is GraphQL?

GraphQL is an API design architecture that takes a different approach where everything is regarded as a graph implying it’s connected. This also means you can tailor your request in such a way that you can request for anything from the endpoint and you will get whatever you requested and nothing more. We pass queries and get responses. 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, which is a great plus.
  • 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 teams can work independently, provided that both the teams know the structure of the data.

Limitations of GraphQL

  • Can be a bit complex for simple applications, to set up types, queries, etc, as it can be done easily using REST.
  • It uses a single endpoint instead of following the HTTP specification for caching. Caching at the network level is important as it can reduce the amount of traffic to a server.

Simple Example Comparing Both

Let’s say, 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 name.
  • /user/<id>/posts to get the list of posts posted by that user.
  • /user/<id>/followers to get the list of followers for the 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 about the user when we use this approach.

This is when GraphQL shows it’s power. 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:

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. Neat and Simple, isn’t it?

GraphQL vs REST

To summarize, here are a few stand-out differences:

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 define the object on the Backend and in GraphQL you define this 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.

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.

Conclusion

GraphQL certainly has many advantages over REST, but it might not always 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. Thank you for reading!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK