What is GraphQl and how it differs from REST?

Published On: 24 February 2021.By .
  • Digital Engineering
  • General

GraphQL A Query Language for APIs

GraphQL is a new API standard that provides a more efficient, powerful, and flexible alternative to REST. It was developed and open-sourced by Facebook and is now maintained by a large community of companies and individuals from all over the world.

GraphQL is a technology that can be used everywhere a client communicates with an API. Interestingly, other companies like Netflix or Coursera were working on comparable ideas to make API interactions more efficient.

Who is using GraphQL?

And many more

Why should we use GraphQL and How GraphQL makes your life easier?

This is the most frequently asked question, why we should use GraphQL as a substitute for REST. So here we will see some surprising features and examples of GraphQL.

  • In REST, we have to call multiple endpoints to fetch related resources, for example, you have to make three requests with different endpoints to fetch user information, user’s post information, user’s follower info, But in GraphQL- we will create a single endpoint for all these three APIs. Yes, we can do the same with REST, but let suppose, somewhere the client only wants user information and somewhere the client only wants the user’s follower information, then in that case we need to create separate APIs, but in GraphQL, we only create a single API.

Let’s take an example –

For user’s information

URL: /user/<id>

Response:

For user’s follower information

URL: /user/<id>/follower

Response:

In GraphQL:

This is our request

And our response to this request will be

If we don’t want follower information then our request would be:

And the response for this:

So,  in simple words, we only pass those params that are required, Here we are using a single endpoint that performing the role of 2 APIs or can be performed multiple APIs roles. Isn’t it interesting

It resolves the problem of over fetching and under fetching. Here frontEnd/client will get the exact required data.

  • We can get multiple APIs responses in a single hit on the server.

Example:

We need the information of students and teachers. In REST, we have to call two APIs, but in GraphQL

This will give you:

Here, on the backend, we created two different APIs for student data and teacher data. With help of GraphQL, we can get responses to these APIs in a single hit on the server. or we can get separate data of these APIs, like

This will give you:

  • When the front end requires any extra data in API, then there is no requirement to make any changes on the server-side. Simply from the frontend, we pass extra params for extra data. So with GraphQL, the developers can do rapid development.    
  • We got the data in the exact format that we sent in the request.

Status Codes in GraphQL:

As you all know, REST APIs use status codes as a part of the response (200-ok, 404-not found, 500-internal server error, etc.). In GraphQL it’s a bit different because in most cases you just get 200 (or 500 if something goes really bad)

Recommended links for GraphQL

https://www.howtographql.com/basics/0-introduction/

https://docs.graphene-python.org/en/latest/quickstart/

 

If you enjoyed this article, share it with your friends and colleagues!

Related content

That’s all for this blog

Go to Top