Time to stop using REST...
Tags: javascript, graphql, api, programming
Published: 2020-10-23 15:13:22

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](https://dev-to-uploads.s3.amazonaws.com/i/2flxm07c91pkfa3bin7j.png)
<a href="https://graphql.org/">Source</a>

### 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 **Re**presentational **S**tate **T**ransfer. 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:
```graphql
query GetBook {
  books(place: 1) {
    author
    title
  }
}
``` 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:
  * <a href="https://github.com/dotansimha/graphql-code-generator">GraphQL Codegen</a>
  * <a href="https://github.com/apollographql/apollo-client">Apollo</a>
  * <a href="https://github.com/prisma/prisma">Prisma</a>
  * <a href="https://github.com/FormidableLabs/urql">URQL</a>

Tutorials:
  * <a href="https://www.youtube.com/watch?v=ZQL7tL2S0oQ">Crash Course</a>
  * <a href="https://www.youtube.com/watch?v=ed8SzALpx1Q">In Depth Course</a>
  * <a href="https://www.youtube.com/watch?v=I6ypD7qv3Z8">Advanced Project</a>

**Flutter/Dart**:

Libaries/Tools:
  * <a href="https://github.com/comigor/artemis">Artemis</a>
  * <a href="https://github.com/zino-app/graphql-flutter">Flutter GraphQL</a>

Tutorials:
  * <a href="https://www.youtube.com/watch?v=GxmQd8IaghE">Crash Course</a>
  * <a href="https://www.youtube.com/watch?v=f6jdoEtZt_o&list=PLXbYsh3rUPSzdOMQJUH52S6TQAXjlt_vy">In Depth Course</a>
  * <a href="https://morioh.com/p/c905f39b3282">Project</a>

**Python**:

Libaries/Tools:
  * <a href="https://github.com/graphql-python/graphene">Graphene</a>

Tutorials:
  * <a href="https://www.youtube.com/watch?v=-0uxxht4mko">Crash Course</a>
  * <a href="https://www.youtube.com/watch?v=aB6c7UUMrPo&list=PLwTfTay2sq81Gq7iJpYRdPCMenyl4p8Ha">In Depth Course</a>
  * <a href="https://www.youtube.com/watch?v=3lD7zdwSYaU&list=PLkiMTDL66MXSX2Mq8c8xAe_gXEYSzULTh">Advanced Project</a>

### 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.