๐โโ๏ธ Hi guys , I always had a small doubt when I heard there are more than one API, I heard about GRAPHQL, and REST, so I got a doubt that which one should I use and which one is better to use.
๐In this post, I would like to give a brief on which is the best, I know only REST API, but not GRAPHQL, so I am using some reference materials for completing this post.
๐ REST API vs GRAPHQL
Let me start with which one is the best
โ๏ธ GraphQL is the better than REST
๐จโ๐ป The main difference between GRAPHQL and REST
๐ A REST API is an architectural concept for network-based software. GraphQL, on the other hand, is a query language, a specification, and a set of tools that operates over a single endpoint using HTTP
๐จโ๐ป Where GraphQL is better than REST API
๐ When using a REST API to fetch information, youโll always get back a complete dataset. For example, if you wanted to request information from two objects, youโd need to perform two REST API requests.
Lets say you need initial user data, and you need to fetch it from /users/:userID
, and secondly you need posts data for that user, so another end point may be. /users/:userID/posts
.
So when the end point is REST API. It would take two HTTP GET requests. They are (I am using json data in this example)
/users/:userID
{
โuserโ:{
โidโ:โuser1โ,
โfirstNameโ:โrohanโ,
โlastNameโ:โdevakiโ
}
}
And one more request for posts of userID
with end point /users/:userID/posts
{
โpostsโ: [{
โidโ :โpost1โ,
โtitleโ: โREST_vs_GRAPHQLโ,
โcontentโ:โgraphql is better than RESTโ
}]
}
In GRAPHQL, on the other hand, you need to simply send a query to the GRAPHQL server that includes the data requirements. Then the server responds with the JSON object where the requirements are fulfilled
Query{
User(id:โuser1โ){
Name
Posts{
Title
}
}
๐ So by this we can understand that the GRAPHQL is powerful, flexible and efficient , where as REST is not that much flexible and inefficient.
๐ GRAPHQL also fetches the data , that is actually required in the query. But REST in the other hand, it fetches all the data that is not even required.
๐จโ๐ป as in the above example you can see REST fetched, all of the user and the post details, where as GRAPHQL only fetched the userโs name and the postโs title.
Donโt just read it ,and get the knowledge, like and share so that others also get the knowledge ๐.
you are welcomed to tell the points which are need to be improved in the comments section ๐.
Follow me on Linkedin hereย , and I will be back with an informative post soon ๐.
Check my next blog here : best laptops for students under 65K ๐
Top comments (1)
I like this too - jsonapi.org/format/#fetching-includes