Optimising GraphQL Mutations for Static Usage

Nik Graf
InstructorNik Graf
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 4 years ago

In theory it's quite easy to dynamically generate queries at runtime based on a parameter, but not a lot of GraphQL clients handle it well. It's not so much about the clients, but all the tooling around it like generators, editor integrations and the opportunity to leverage persistent queries.

Using plural instead of singular arguments will allow your clients to use static queries for situations where they usually would need dynamic ones.

Instructor: [00:00] There's one improvement we can make to our addProductToCart mutation. Right now, it works well for user interface where the user can add one item at a time. What if the user interface allows them to select multiple items and add them to the cart at once?

[00:17] No problem. We can run multiple queries and mutations in one request. For example, we can add addProductToCart twice with different product ID values.

[00:29] To implement that on a client, we would trade a query, including n mutations where n is the amount of selected products. In reality, most graphQL clients are not doing well with this pattern, and static queries are way easier to handle, which then often results in a solution where we iterate over each product and sending one request per mutation.

[00:57] This is not ideal. Since we get a lot of products, this can result in a lot of requests. What if we change the mutation to addProductsToCart and refit, change the argument product ID to product IDs?

[01:14] Now, we can pass in multiple products. This is great. It's more flexible since we can implement both UIs. At the same time, we didn't lose any clarity on the used case. The mutation is still about adding products to the cart.

[01:31] The only thing to keep in mind here is that it also has an impact on the behavior. When running two addProductToCart mutations in one request, in the backend, the second one would only start once the first one executed successfully.

[01:47] In comparison to that, for addProductsToCart, it's up to you if you want this behavior or if you prefer inserting both at once.

egghead
egghead
~ 9 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today