DEV Community

REST API vs GraphQL

Bentil Shadrack on November 28, 2022

You have probably heard about GraphQL, but you might not be entirely sure how and whether it differs from REST. You're in luck, then! Today, we'll ...
Collapse
 
brense profile image
Rense Bakker • Edited

Its worth noting that graphql lives on top of the HTTP protocol as well (its nothing more than a post request) and therefore it benefits from all the security benefits that come with it including https, the ability to send authorization headers, etc. Also graphql servers can return status codes other than 200. For example if you use Apollo server, it will return a status code of 400 if your graphql query is malformed.

Collapse
 
qbentil profile image
Bentil Shadrack

Wow!
Thank you for this input👏

Collapse
 
jefflindholm profile image
Jeff Lindholm

Wrong on so many levels. REST is not an RPC protocol, which is what you described, admittedly that is what most people call REST but that is not what the original specification says.
Look into HATEOS that is more what the original was supposed to be, but even the RPC is not just CRUD operations. Which is all GraphQL is.
REST payloads should include things like where to get the next values if it is a list type request, it should show how to get details if it is a summary dataset, it should also have a reference link to get the same answer again, or refresh it if the data could be changing.
GraphQL is nice and pretty cool, but it is also pretty hard to write all the connections for a complicated dataset, and it is not free.
It was also done in more of an RPC approach before in the OData specification which could do the same things, excluding fields, etc. But again both of these have to be written to best take advantage of this, most GraphQL services I have seen, still get everything from the database/storage and then limit the payload, which is an optimization, but might not actually be the place where the most time would have been saved.

Collapse
 
bklau2006 profile image
BK Lau • Edited

GraphQL is essentially a smart "aggregator" playing the role of data orchestrator( See dev.to/jacobsee/microservice-coord...)
The main difference from REST comes to 2 essential mechanisms:

1) Parsing and validation of request "shape" via schema parsing

2) Forwarding request fragments to different endpoints(microservices/data providers) and collect and aggregate the various data into the "shape" requested in #1

As result, technically-speak, GraphQL should be able to use other protocols besides HTTP protocol. Though current spec or implementation mainly uses HTTP.

Collapse
 
missamarakay profile image
Amara Graham

YES! Thank you for this great post!

Though it isn't necessarily a "replacement".
You will need to decide between GraphQL, REST API, or a combination of both depending on your use cases.

I really liked seeing this specifically called out.

Collapse
 
qbentil profile image
Bentil Shadrack

You are most welcome.
I’m glad you like it!

Collapse
 
andrewbaisden profile image
Andrew Baisden

Fantastic comparison I know so many people who keep asking what the difference between the two is and why they should even learn GraphQL.

Collapse
 
qbentil profile image
Bentil Shadrack

Thank you Andrew😊

Collapse
 
fruntend profile image
fruntend

Сongratulations 🥳! Your article hit the top posts for the week - dev.to/fruntend/top-10-posts-for-f...
Keep it up 👍

Collapse
 
qbentil profile image
Bentil Shadrack

Awesome🎉🎉🎉🙌

Collapse
 
professorhaseeb profile image
Haseeb ur Rehman

Nice article.

Also check 'restql' to does the same for rest apis on python,
useful for anyone who dont want to upgrade to graphql instantly but want to enjoy this structure.

Collapse
 
qbentil profile image
Bentil Shadrack

That's awesome!
I will check it out asap

Thank you Haseeb

Collapse
 
akuoko_konadu profile image
Konadu Akwasi Akuoko

Thanks a lot for this Shadrack

Collapse
 
qbentil profile image
Bentil Shadrack

You are welcome Akwasi

Collapse
 
yukikimoto profile image
Yuki Kimoto - SPVM Author

Thanks.

Collapse
 
pterpmnta profile image
Pedro Pimienta M.

Excelent post.

Collapse
 
qbentil profile image
Bentil Shadrack

Thank you Pedro!

Collapse
 
lovepreetsingh profile image
Lovepreet Singh • Edited

🔥 Very Helpful. Kindly checkout my blog at dev.to/lovepreetsingh/make-thousan...