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