DEV Community

Cover image for ๐Ÿ‘‰ REST API vs GRAPHQL , which one is better? ๐Ÿค”
rohan2734
rohan2734

Posted on

๐Ÿ‘‰ REST API vs GRAPHQL , which one is better? ๐Ÿค”

๐Ÿ™‹โ€โ™‚๏ธ 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โ€

         }

}
Enter fullscreen mode Exit fullscreen mode

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โ€

      }]

}
Enter fullscreen mode Exit fullscreen mode

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     

      }

}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ 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.
Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
codewander profile image
codewander