Time to stop using REST...

Henry Boisdequin - Oct 23 '20 - - Dev Community

If you are using REST, I have something to tell you. Its time to stop using it. Creating REST APIs which return JSON or XML has many cons. There is a much better way to do it now. GraphQL is a revolutionary alternative to the classic REST API. I have been using GraphQL for some time and have been enjoying it. Today, I will show you the Pros and Cons of both using a REST API or a GraphQL API so you can decide which one to use in your next project.

Alt Text
Source

What is a REST API?

To compare a REST API to a GraphQL one, we first need to know exactly what a REST API is. REST is short for Representational State Transfer. REST is a very popular tool that you can use to provide services to your client. REST sets up an endpoint (e.g. localhost:4000) and provides some services on this endpoint (GET, POST, etc). The client makes a request to this endpoint and receives/changes the data it calls for.

What is GraphQL?

GraphQL was developed by Facebook in 2012. Since then, it has been steadily gaining popularity. GraphQL is a query language for your API. With this method, the client has more ability to narrow down the request for specific data. For example, in my REST API on localhost:4000, I give back a JSON object with an array of my favourite books. Each book has a title, an author, the cost, and the genre. The client just wants the title and author of my favourite book but by using a REST API, I get the whole JSON object back. With a GraphQL server I can make a query like this at localhost:4000/graphql:

query GetBook {
  books(place: 1) {
    author
    title
  }
}
Enter fullscreen mode Exit fullscreen mode


With this method, I get just the author and title of my favourite book. I hope that now you can see the power of using GraphQL.

Pros and Cons of using a REST API:

Pros:

  • REST is more popular
    • You will see more people using REST than GraphQL
  • REST is flexible in most areas
    • REST can return many different data formats such as JSON or XML
    • REST understands files

Cons:

  • REST requires multiple round trips
    • If I were to retrieve more than one endpoint, I would be making that many separate requests
  • Over fetching
    • As I mentioned before, REST returns the whole object and the client doesn't have a lot of flexibility to request for specific data

Pros and Cons of using a GraphQL API:

Pros:

  • Fetch data with a single API call
    • Since GraphQL only has one endpoint, there is only one roundtrip
  • Getting exactly what you ask for
    • With GraphQL, you can describe exactly what you ask for
  • GraphQL Playground
    • If you're using GraphQL, the only endpoint in your API is the GraphQL playground. With this playground, you get autogenerated documentation to tailor to your needs
  • Errors notified immediately (safer than a REST API)
    • With the GraphQL playground, you get immediate and detailed type errors to alert you with the problem right away

Cons:

  • Takes longer to learn than REST
    • With GraphQL, you need to learn their Schema Definition Language, the playground, and more
  • Over the top for smaller applications
    • Since REST is easier to learn and performs well for simple apps, GraphQL would be over the top for your normal to-do list

GraphQL Ecosystem

Now that you know the Pros and Cons of REST and GraphQL, I hope you are starting to realise the power of GraphQL. Of course, GraphQL is not perfect but it is definitely something worth migrating to. If you are considering to move over the GraphQL world, the list below will help you to get started with GraphQL right away.

Javascript/Typescript:

Libaries/Tools:

Tutorials:

Flutter/Dart:

Libaries/Tools:

Tutorials:

Python:

Libaries/Tools:

Tutorials:

Conclusion

I hope you have seen the true power of GraphQL in this article and have considered using it in your next project. If you use GraphQL in any other languages apart from Typescript/Javascript, Dart, and Python, then please share the GraphQL ecosystem in that language by commenting below.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player