Differences of GraphQL and REST

3 months ago SETHA THAY 204
Differences of GraphQL and REST

In this post, I am going to share some thoughts on GraphQL vs REST which often developers tend to feel confusing due to its similarities and functionalities. There is no best API out there, you can find the best fit for your app depending on business requirements and user cases. Let's start

Similarities between GraphQL and REST

GraphQL and REST essentially accomplish the same goal to communicate with a server and retrieve data for clients to use. Below are a few similarities between the two methodology

  • They are based on the concept of a resource, identified by IDs for those resources
  • They rely on frameworks and libraries to handle the tiny networking details
  • Both can differentiate if an API request is intended to read or write data

WHY GRAPHQL?

graphql-vs-rest-graphql-icon

  • One Unique Endpoint: GraphQL exposes a single endpoint that allows you to access multiple resources.
  • One request, many resources: Query one or more resources in the same request. You will avoid making multiple API calls for an operation.
  • No over or under fetching: The main advantage of GraphQL is that it's eliminated too much or not enough response data which REST does. GraphQL solves this problem by fetching only exact and specific data in a single request.

  • Extending APIs: Adding more functionality wouldn't affect existing client GraphQL queries.
  • Bandwidth: If you intended to use your API in a mobile or large application like Facebook Newsfeed, GraphQL offers better bandwidth usage.

WHY REST?

graphql-vs-rest-restful-icon

  • Handling complex queries: Specify everything you want to expose to the front-end. If you careless, big queries can lead your server down. In that sense, GraphQL’s greatest strength can also be its greatest weakness.
  • Code vulnerability in GraphQL: Attacker can carry out a DDoS (Denial-of-service-attack) to an unprotected server, they can possibly execute an expensive nested query to overload your server.
  • HTTP Caching: REST allows data to be cacheable, which improves performance and scalability whereas, GraphQL uses a single endpoint and custom requests, so caching is complicated.
  • 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 “Server Error — Something went wrong” message.
  • Data Formats: REST APIs support various data formats (XML, JSON, HTML, plain text, etc.) whereas, GraphQL only supports JSON.

  • Rate Limiting: You can simply specify that we allow only this amount of requests in one day, but it is difficult to specify this type of statement in GraphQL.

Conclusion

You can use GraphQL if your API is intended to be used on mobile or large applications like Facebook Newsfeed since it offers better bandwidth usage. Or, applications where you need to expose nested data i.e. blog posts with their comments and people details.

You can use REST if your API need caching and monitoring facilities or APIs where you want to determine the amount of API call 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.

For more knowledge of GraphQL, I would recommend you to check this GraphQL tutorial by Net Ninja 

THANK YOU !!


About author

Author Profile

SETHA THAY

Coding lover. I am willing to share my knowledge and 10 years experiences in the IT field. Please feel free to feedback on my post if you found any problems. Thank You.


DigitalOcean Referral Badge

Scroll to Top