GraphQL v/s REST – Conclusion

The objective of this blog is to explain and determine which among the REST and GraphQL could be a best fit depending upon your requirement and use-case. Let’s start !

If you’re not aware of GraphQL, I would highly recommend you to checkout one of th…


This content originally appeared on DEV Community and was authored by tbaveja

The objective of this blog is to explain and determine which among the REST and GraphQL could be a best fit depending upon your requirement and use-case. Let's start !

If you're not aware of GraphQL, I would highly recommend you to checkout one of the below:

Similarities between GraphQL and REST

GraphQL and REST essentially accomplish the same task, i.e., communicate with a server and retrieve data. Here are a few similarities between the two:

  1. Both GraphQL and REST are based on the concept of a resource, and they specify IDs for those resources.
  2. Both GraphQL and REST rely on frameworks and libraries to handle the tiny networking details.
  3. Both can differentiate if an API request is intended to read data or write it.

Why GraphQL ?

1. One Unique Endpoint: GraphQL exposes a single endpoint that allows you to access multiple resources.

2. One request, Many resources: Query one or more resources in the same request. This will avoid making multiple API calls for an operation.

3. No Over Fetching or Under Fetching: The main advantage of GraphQl over REST is that REST responses contain too much data or sometimes not enough data, which creates the need for another request. GraphQL solves this problem by fetching only the exact and specific data in a single request.

4. Extending APIs: Adding functionality wouldn't affect existing client GraphQL queries.

5. Bandwidth: If your API is intended to be used on a mobile application or a large application like Facebook Newsfeed, use GraphQL since it offers better bandwidth usage.

Why REST over GraphQL ?

1. Handling complex queries: We have to specify everything we want to bring. “. If you’re not careful, a few big queries can bring your server down to its knees. In that sense, GraphQL’s greatest strength can also be its greatest weakness.”

2. Code vulnerability in GraphQL: If an attacker wants to carry out a DDoS (Denial-of-service-attack) to an unprotected server, all he has to do is execute an expensive nested query to overload your server.

3. HTTP Caching: REST allows data to be cacheable, which might improve performance and scalability whereas, GraphQL uses a single endpoint and custom requests, so caching is complicated.

4. Error Reporting: With REST, you can use API responses and build a monitoring system for error messages. But error reporting in GraphQL leaves a lot to be desired, with the ubiquitous “200 K Error — Something went wrong” message.

5. Data Formats: REST APIs support various data formats (XML, JSON, HTML, plain text, etc.) whereas, GraphQL only supports JSON.

6. Rate Limiting: Another problem with GraphQL is rate-limiting. In REST API, you can simply specify that we allow only this amount of requests in one day", but in GraphQL, it is difficult to specify this type of statement.

Conclusion:

If your API is intended to be used on a mobile application or a large application like Facebook Newsfeed, use GraphQL since it offers better bandwidth usage. Or, applications where we usually need nested data to be fetched. i.e. blog posts with their comments and people details.

If you need caching and monitoring facilities in your API, use REST. Or, public APIs where we want to determine what to expose to the clients.

You can also use a combination of GraphQL and REST for a project. It all depends on your data and performance requirements.

Must Read:

Always try to follow and learn from case studies. Below are the case studies from Netflix Tech Blog:

.

"Learn Together, Grow Together !"

.
That's It ! And don't forget to explore various tools available for GraphQL !
Cheers


This content originally appeared on DEV Community and was authored by tbaveja


Print Share Comment Cite Upload Translate Updates
APA

tbaveja | Sciencx (2021-06-28T20:09:09+00:00) GraphQL v/s REST – Conclusion. Retrieved from https://www.scien.cx/2021/06/28/graphql-v-s-rest-conclusion/

MLA
" » GraphQL v/s REST – Conclusion." tbaveja | Sciencx - Monday June 28, 2021, https://www.scien.cx/2021/06/28/graphql-v-s-rest-conclusion/
HARVARD
tbaveja | Sciencx Monday June 28, 2021 » GraphQL v/s REST – Conclusion., viewed ,<https://www.scien.cx/2021/06/28/graphql-v-s-rest-conclusion/>
VANCOUVER
tbaveja | Sciencx - » GraphQL v/s REST – Conclusion. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/28/graphql-v-s-rest-conclusion/
CHICAGO
" » GraphQL v/s REST – Conclusion." tbaveja | Sciencx - Accessed . https://www.scien.cx/2021/06/28/graphql-v-s-rest-conclusion/
IEEE
" » GraphQL v/s REST – Conclusion." tbaveja | Sciencx [Online]. Available: https://www.scien.cx/2021/06/28/graphql-v-s-rest-conclusion/. [Accessed: ]
rf:citation
» GraphQL v/s REST – Conclusion | tbaveja | Sciencx | https://www.scien.cx/2021/06/28/graphql-v-s-rest-conclusion/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.