DEV Community

Cover image for 👉 REST API vs GRAPHQL , which one is better? 🤔
rohan2734
rohan2734

Posted on

3 2

👉 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 😃

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (1)

Collapse
 
codewander profile image
Anon

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay