LogoLogo
User ManualChangelogRoadmapAboutYouTube
GraphQL API
GraphQL API
  • GraphQL API
  • Overview
    • Query types
      • Stream queries
    • Error codes
    • Schema
      • Queries
      • Mutations
      • Objects
      • Interfaces
      • Scalars
      • Input objects
      • Enums
      • Unions
  • CHANGELOG
    • Changelog
    • Breaking changes
  • Guides
    • Authentication
    • Basic query tutorial
    • Batching mutations
    • Integrating data
  • Query examples
    • List of products with attributes and they values in product stream
    • Get information about specific product and specific attribute values in specific language
    • List of 100 grouped products with simple and variable products in stream
    • List of grouped products with simple and variable products AFTER some end cursor
    • List of active languages
    • List of templates with attributes
    • List of all multimedia in stream
    • List of product relations for a specific product
    • Get values of custom fields
    • Create a simple product
    • Create a grouping product
    • Create a variable product
    • Add a child product to grouping one
    • Set quantity of child product
    • Remove a child product from grouping one
    • Add a variant to variable product
    • Get products with variants, binding attributes and variants list
    • Remove a variant product from variable one
    • Multimedia create
    • Add images to the gallery attribute
    • Change the name of the multimedia
    • Set alternative value for a multimedia
    • Delete Multimedia
    • Add a file to the product
    • Get attributes list by SKU
    • Create a product and assign / modify attributes values
    • Assign the template to a product
    • Create a category
    • Get category tree by category tree code
    • Get a specific category with values of the category attribute
    • Set multiple options in multiselect attribute on specific product
  • Add option to select type attribute
Powered by GitBook
On this page

Was this helpful?

Export as PDF
  1. Guides

Batching mutations

API mutations are designed to be very small and atomic on purpose - thanks to that design, you don't have to prefetch data to send it back to the server and worry only about the actual change you want to perform.

On the other hand, occasionally your intention is to create or change resources with data that a single mutation does not handle. GraphQL comes in handy here - thanks to its design you can batch mutations(and queries) which means that multiple operations can be performed in one, single request - this not only makes it easier to make updates but also speeds up operations.

mutation {
  create: productCreateSimple(input: {sku: "new_product", templateCode: "template"}) {
    __typename
  }
  assignDescription: productAddAttributeValueTranslationsTextarea(
    input: {
      sku: "new_product"
      attributeCode: "description"
      translations: [{ value: "Long description", language: "en_GB" }]
    }
  ) {
    __typename
  }
  assignShortDescription: productAddAttributeValueTranslationsTextarea(
    input: {
      sku: "new_product"
      attributeCode: "short_description"
      translations: [{ value: "Short description", language: "en_GB" }]
    }
  ) {
    __typename
  }
}

Batch mutations are executed synchronously which also means their order does matter. You need to create the product first in order to assign values to it.

All mutations in the batch are performed one after another and if one or more fails all the others will still be tried to execute. The batch result will contain requested data from each mutation as well as specific errors which occurred for those which failed.

Note that each mutation has an alias (create: (...)) - which means a custom name is assigned to it. Aliasing is required if there are at least two same mutations in one batch - productAddAttributeValueTranslationsTextarea in this case - this behavior allows differentiation of the data in the result which in another case would not be possible.

In the above example for simplicity, all mutations have been aliased in the example but it'd also suffice to only alias duplicates.

As tempting as it may sound to make one update of all data in the system bear in mind the fact of synchronicity of the batch process - it means that the process will last as long as there are some mutations left to be executed.

In practice, it means that too-long running processes are usually more faulty and also it's harder to handle potential errors. In most extreme cases, you can even reach timeout from your HTTP client or the server can drop the handling of the request as it takes too long to process.

A usually recommended approach would be to limit the batch to a single resource(product, attribute, multimedia) or with a similar intention in mind.

PreviousBasic query tutorialNextIntegrating data

Last updated 2 years ago

Was this helpful?