What is GraphQl and how it differs from REST?
- Digital Engineering
- General
What is GraphQl and how it differs from REST?
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?
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:
1 |
{‘id’: 1001, ‘Name’: ‘Test User’, ‘Age’: 24} |
For user’s follower information
URL: /user/<id>/follower
Response:
1 |
[{‘id’: 1, ‘name’: ‘Follower 1’}, {‘id’: 2, ‘name’: ‘Follower 2’}] |
In GraphQL:
This is our request
1 2 3 4 5 6 7 8 9 10 11 |
Query{ User{ Id Name Age Follower{ Id name } } } |
And our response to this request will be
1 |
{‘id’: 1001, ‘Name’: ‘Test User’, ‘Age’: 24, ‘follwer’:[{‘id’: 1, ‘name’: ‘Follower 1’}, {‘id’: 2, ‘name’: ‘Follower 2’}]} |
If we don’t want follower information then our request would be:
1 2 3 4 5 6 7 |
Query{ User{ Id Name Age } } |
And the response for this:
1 |
{‘id’: 1001, ‘Name’: ‘Test User’, ‘Age’: 24} |
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
1 2 3 4 5 6 7 8 9 10 11 12 |
Query{ Student{ Id Name Age } Teacher{ Id Name subject } } |
This will give you:
1 2 3 4 |
{ Student: [{id: 001, name: stu_name, age:26} ], Teacher: [{id: 1001, name: prof_raman, subject: Mathematics}] } |
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
1 2 3 4 5 6 7 8 |
Query{ Student{ Id Name Age } } |
This will give you:
1 |
{ Student: [{id: 001, name: stu_name, age:26} ] } |
- 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/
Related content
Auriga: Leveling Up for Enterprise Growth!
Auriga’s journey began in 2010 crafting products for India’s