rest api vs graphql , which is better?

👉 REST API vs GRAPHQL , which one is better? 🤔

By rohandevaki | therohandevaki | 6 Jun 2022


🙋‍♂️ 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-rohan-devaki here , and I will be back with an informative post soon 😃.

Check my next blog here (best laptops for students) 😃 

 

first post was published here

 

If you are new to publishox , signup from here 

How do you rate this article?

2



therohandevaki
therohandevaki

Hi , this is rohan devaki ,and this is my personal blog

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.