简体   繁体   中英

Is it good practice to bundle multiple GQL mutations to one HTTP call

I have a mutation on my GQL server. The resolver for this basically just proxies the request to a backend Java service that handles Dynamo table mapping. I cant really edit anything on that service right now.

The mutation signature looks like this:

mutation {
  addItemRelation(child:"child1Id", parent:"parentId"),
}

mutation {
  removeItemRelation(child:"child1Id", parent:"parentId"),
}

On the front end I have an interface that lets me check boxes to relate certain properties to the parent item. Currently the way I handle this is by calculating the items to add/remove. Then sending one HTTP call to GQL per item.

Im wondering would it be better to bundle these all into one request. For example, if I was adding 2 items and removing one I could compute a GQL query like this.

mutation {
  addItemRelation(child:"child1Id", parent:"parentId"),
  addItemRelation(child:"child2Id", parent:"parentId"),
  removeItemRelation(child: "child3id", parent: "parentId")
}

Would this be considered better practice than sending out 3 separate http requests?

On the one hand, the rule of thumb is the fewer network requests, the better. If you break the query into three separate HTTP requests, it's possible any one could fail due to poor connectivity while the others succeed. At least with a single request, either all three mutations will be executed or none at all.

On the other hand, root level mutations are executed sequentially (compared to all other fields, which are actually executed in parallel). This sort of sequentially execution may be desirable in itself depending on your business case. However, technically, it also means if your server is capable of handling multiple requests in parallel, sending each field as a separate request may result in all three mutations being completed faster. In practice, latency between the client and the server is usually the biggest factor so the difference may not be noticeable.

Be mindful that when your selection set includes the same field multiple times, you'll need to uses aliases:

mutation {
  addItemRelation(child:"child1Id", parent:"parentId"),
  addItemRelation2: addItemRelation(child:"child2Id", parent:"parentId"),
  removeItemRelation(child: "child3id", parent: "parentId")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM