Graphql vs rest
The content here is under the Attribution 4.0 International (CC BY 4.0) license
Introduction
This is a quick search done through Google Scholar using the keyword “REST versus Graphql”. The aim is to search for empirical data on the strengths and weaknesses of both approaches, as practitioners often argue that one is “better” than the other.
Often practitioners ignore that “best” might vary based on the context and level of knowledge of a given technology - this approach happens with the choice of using REST over GraphQL.
Approach to get information
- Search is set to use any time
- To sort by relevance and
- to be of any time
Top 5 papers in the result:
- REST vs GraphQL: A Controlled Experiment - https://arxiv.org/abs/2003.04761
- REST or GraphQL? A Performance Comparative Study - https://sci-hub.hkvisa.net/10.1145/3357141.3357149
- Additional material: https://speakerdeck.com/gustavopinto/rest-or-graphql-a-performance-comparative-study
- Experiences on Migrating RESTful Web Services to GraphQL - https://link.springer.com/chapter/10.1007/978-3-319-91764-1_23
- Performance Analysis of GraphQL and RESTful in SIM LP2M of the Hasanuddin University - https://ieeexplore.ieee.org/abstract/document/8878524
- Migrating to GraphQL: A Practical Assessment - https://ieeexplore.ieee.org/abstract/document/8667986
Discussion
in this section, we will discuss the different features of each approach.
Data fetching
Data fetching is the most common theme when the subject is rest and GraphQL. both offer different ways of consuming information. Rest is based on the HTTP verbs to fetch data. Richardson’s Maturity Model explores the HTTP verbs more powerfully. The server is the one that offers different possibilities for fetching the data such as filters and transformers. However, it is less common to see restful API offering granularity over the data being returned. In other words, if the client doesn’t want the entire payload from my specific get request, the server will return it anyway this is the argument that gives the graph well its foundation. graph QL does offer granularity over the data being sent back from the server to the client. however, rest API can also implement this kind of behavior but before the graph QL this was not common.
Graph QL on the other hand does not care about each different age TTP verbs that the protocol offers it uses only post and the graph QL specification handles the different types of behaviors based on its language. To fetch data with graph QL queries are used. Those queries enable the client to specify what data it needs to be retrieved its properties filtering and transforming as well. Q graph QL needs specific tooling to handle the complexity of dealing with its standard and language to communicate with clients and servers. The following are the most used ones:
- Apollo
-
Relay
- GraphQL playground explored how to use relay with code samples
Given that each type was created at different times both are used today however graph QL got its popularity with the appealing data that is sent from the server to the client as its main feature. Studies have shown that in a controlled experiment, GraphQL takes less time to implement in comparison with Rest [1]
Rest API on the other hand head also exploded in standardization. The open API for instance is the most used project with restful API.
Examples of Rest APIs:
Error handling
Tools to consume graphql
inmsonia
postman
intellij text http
Resources
Blogs that might complement content
- https://www.taniarascia.com/introduction-to-graphql
- https://escape.tech/blog/graphql-error-handling-a-security-pov
- Performance analysis of Web Services Comparison between RESTful & GraphQL web services - Bachelor Degree
- Exposing Rich Update Operations via REST APIs
- Testing
- https://miragejs.com/docs/advanced/graphql
Youtube videos/Talks
-
Comparing Native Java REST API Frameworks with Matt Raible
- This is a video that interests me since I gave a similar talk in the php ecosystem, comparing different frameworks when I was writing the Laravel book
- The video is not something related to graphql vs rest directly, but it talks about both of them and the support that each framework gives for each approach. I found it useful to see the comparison in the memory usage and startup time (even though it has nothing to do with the kind of api we are building it can make difference in the architecture)
- 15:09 - Introduction
- 18:51 - Why java
- 21:38 - Build REST API with java
- 26:00 - Get started with micronaut
- 30:48 - Get started with quarkus
- 34:13 - Get started with springboot
- 36:52 - Get started with Helidon
- 41:35 - What about GraphQL APIs
- 42:55 - Secure your API with OAuth 2.1
- 47:55 - What about testing
- 48:58 - Build with docker
- 52:21 - Go native with GraalVM - micronaut
- 52:46 - Go native with quarkus
- 53:02 - Go native with springboot
- 53:41 - Go native with Helidon
- 53:52 - Native startup time
- 56:56 - Native memory usage
- 58:31 - Demo time
- 01:14:55 - JHipster support
- 01:16:38 - Matt Raible thoughts
- 01:21:33 - Q&A
- This is the link for reference: Java REST API Showdown: Which is the Best Framework on the Market?
- Podcasts
References
- [1]G. Brito and M. T. Valente, “REST vs GraphQL: A controlled experiment,” in 2020 IEEE international conference on software architecture (ICSA), 2020, pp. 81–91.
Table of contents
Got a question?
If you have question or feedback, don't think twice and click here to leave a comment. Just want to support me? Buy me a coffee!