OverviewWhen creating a web service application, you can choose to use REST or GraphQL as the communication mode. Both may use JSON over HTTP, but have different advantages and disadvantages. This article mainly compares GraphQL and REST to operate a product database example, comparing the differences between the two solutions when performing the same client-side operations:
RESTThe main data element of REST (Representational State Transfer) is called Resource. In this case, the resource is "Product".
curl --request POST 'http://localhost:8081/product' \
curl --request PUT 'http://localhost:8081/product/{product-id}' \
curl --request GET 'http://localhost:8081/product?size=10&page=0' {
To get a product and its orders, you typically need to first call the product list API and then call the order resource to find the related orders: curl --request GET 'localhost:8081/order?product-id=1' { In addition to the original operation of getting all products, this operation needs to be performed once for each product of interest, which creates an N+1 dependency problem. GraphQLGraphQL API operations include Queries and Mutations. Queries are responsible for obtaining data, and Mutations are used to create and update. The Schema mode of Queries and Mutations defines the possible requests and responses of the client.
curl --request POST 'http://localhost:8081/graphql' \ {
curl --request POST 'http://localhost:8081/graphql' \ {
curl --request POST 'http://localhost:8081/graphql' \ {
curl --request POST 'http://localhost:8081/graphql' \ { GraphQL AdvantagesGraphQL allows for flexible and dynamic queries:
GraphQL tends to avoid expensive operations, and you can usually get all the data you need in a single request using GraphQL. When to use RESTGraphQL is not a replacement for REST. REST might be more appropriate in the following cases:
in conclusionThe choice of using REST or GraphQL as the communication mode needs to be determined by the business scenario. The flexibility of GraphQL also determines its complexity to a certain extent. When using GraphQL, you also need to consider cache optimization at the application level and batch operation optimization to solve the N+1 problem. |
<<: Saudi scientists use sunlight instead of WiFi signals to connect to the Internet
>>: Sharing of practical experience on routing technology pitfalls, have you learned it?
Xi'erqi subway station is so cool these days!...
In recent years, with the rapid growth of mobile ...
Communications operators must refocus on covering...
BandwagonHost has launched a special package call...
[51CTO.com original article] Today I want to shar...
[[393766]] What is 5G network? "5G" act...
In the face of the epidemic, staying at home and ...
On June 6, 2019, the Ministry of Industry and Inf...
From 2G to 4G, mobile networks have become an ind...
With the continuous development of mobile phones,...
For years, people have been talking about the tra...
What we are going to talk about today is other co...
iWebFusion (iWFHosting) was founded in 2001. It i...
In September 2020, the "China-EU Business Di...
This article is organized as follows: Cookies and...