Batch mutations
Many Integration API mutations support batching - sending multiple items in one mutation. For massive updates, batch mutations are faster and work atomically (either all items are saved or none) whenever it makes sense. Always consider using batch mutations for large updates.
Because every API call is sent, authorized, processed and responded to individually, a big profit from using batch mutations is how much network round trip time and server processing time it can save you. For example, sending 10 calls to create ID conversions will require you to wait 10 times for the requests and responses to be processed and carried over the Internet. If you instead send those mutations in a single API call, not only the total execution on the server will be faster, but you will also save a lot on network time to receive the single response.
Risks
With greater performance, you need to know about the risks:
- In some cases, large batches can cause an instant high load. To choose a batch size that works best for you, you can test different sizes and compare response times.
- Processing some items can fail due to errors. Most mutations roll back all changes if any item fails, including successful ones. You will see this as a non-empty
userErrorsin the response. Other mutations throwuserWarningsand save valid items. Pay attention to the errors you receive to determine if partial changes have been applied or not. See more on handling errors in the Integration API. - Using these mutations for updating a single object at a time is ineffective. Try to collect updates on your side to send a single mutation with many objects.
Example
When you need to update multiple prices of a single pricelist, you can use setPrices. Pay attention to individual variant prices: there can be many of them in one mutation as well.
mutation setPricesForOnePricelist {
setPrices(
input: {
pricelist: { id: 10 }
productPrices: [
{ product: { id: 1 }, price: { value: 99.99, currencyIsoCode: "USD" } }
{
product: { id: 2 }
price: { value: 199.99, currencyIsoCode: "USD" }
individualVariantPrices: [
{
productVariant: { id: 1002 }
price: { value: 202, currencyIsoCode: "USD" }
}
{
productVariant: { id: 1003 }
price: { value: 203, currencyIsoCode: "USD" }
}
]
}
# and more
]
}
) {
userErrors {
message
path
}
userWarnings {
message
path
}
pricelist {
id
}
}
}
When you need to update multiple prices of multiple pricelists, setPricesBatch will do it in one go, while setPrices would have to be called for as many pricelists as you have.
mutation setPricesForMultiplePricelists {
setPricesBatch(
input: [
{
pricelist: { id: 10 }
productPrices: [
{
product: { id: 1 }
price: { value: 99.99, currencyIsoCode: "USD" }
}
{
product: { id: 2 }
price: { value: 199.99, currencyIsoCode: "USD" }
individualVariantPrices: [
{
productVariant: { id: 1002 }
price: { value: 202, currencyIsoCode: "USD" }
}
{
productVariant: { id: 1003 }
price: { value: 203, currencyIsoCode: "USD" }
}
]
}
]
}
{
pricelist: { id: 11 }
productPrices: [
{
product: { id: 1 }
price: { value: 79.99, currencyIsoCode: "EUR" }
}
{
product: { id: 2 }
price: { value: 189.99, currencyIsoCode: "EUR" }
individualVariantPrices: [
{
productVariant: { id: 1002 }
price: { value: 182, currencyIsoCode: "EUR" }
}
{
productVariant: { id: 1003 }
price: { value: 183, currencyIsoCode: "EUR" }
}
]
}
]
}
]
) {
userErrors {
message
path
}
userWarnings {
message
path
}
pricelists {
id
}
}
}
Examples of batch mutations
anonymizeCustomers/anonymizeOrders- to remove all personal data connected to multiple customers / multiple orders. Batch size limit: 50 customers / 50 orders.assignAttributes/unassignAttributes- to assign attributes to a single object / to unassign attributes from a single object. Batch size limit: no limit (order lines can have up to 10 attributes).assignAttributesBatch/unassignAttributesBatch- to assign attributes to multiple objects / to unassign attributes from multiple objects. Batch size limit: 100 objects.createMediaBatch- to queue an upload of multiple product images from URLs. Batch size limit: 100 images. Processing is asynchronous: use the returnedqueueIdwith themediaBatchquery to check the batch status, asuserErrorsin the response don't tell you the final outcome of the upload.issueInvoiceDrafts- to issue multiple invoice drafts. Batch size limit: 200 invoices.setCampaignVariants/unsetCampaignVariants- to enable selected variants in one campaign/to remove variants from a campaign. Batch size limit: no limit.setCategoryDisplays/unsetCategoryDisplays- to add displays to a single category, or remove displays from a single category. Batch size limit: 100 displays.setDeliveryWindowVariants/unsetDeliveryWindowVariants- to set how variants are available in a single delivery window, or remove variants from a delivery window. Batch size limit: 100 variants.setMarketDisplays/unsetMarketDisplays- to add displays to a single market, or remove displays from a single market. Batch size limit: 100 displays.setIdConversions/unsetIdConversions- to set/unset multiple ID conversions. Batch size limit: 100 objects.setPrices/setAlteredPrices- to set prices for a single pricelist. Batch size limit: 100 prices.setPricesBatch/setAlteredPricesBatch- to set prices for multiple pricelists. Batch size limits: 100 prices per one pricelist, 1000 prices in total.setOrdersLock- to lock or unlock multiple orders. Batch size limit: 100 orders.setSubscriptionPlanVariants/unsetSubscriptionPlanVariants- to add variants to a single subscription plan, or remove variants from a subscription plan. Batch size limit: 100 variants.setStock- to set absolute stock values for multiple product variants or product sizes in one warehouse. Batch size limit: 10,000 product sizes.changeStock- to add or remove stock quantities in a warehouse, or transfer stock between two warehouses. Batch size limit: 100 product sizes.setTranslations- to set translations to a single object.setTranslationsBatch- to set translations for multiple objects and languages in one call. If any input has errors, nothing is saved. Batch size limit: 100 objects.addSubvouchers- to add subvouchers with given codes to a voucher. Batch size limit: 50,000 subvouchers.generateSubvouchers- to generate subvoucher codes for a voucher. Batch size limit: 30,000 subvouchers.updateSubvouchers- to update multiple subvouchers (replaces the deprecatedupdateSubvoucher). Batch size limit: 20,000 subvouchers.removeSubvouchers- to completely remove multiple subvouchers. Batch size limit: 10,000 subvouchers.- ...and others.
Many mutations from this list can only set multiple values to a single object. To update many objects at once, use the *Batch variants: assignAttributesBatch, unassignAttributesBatch, setTranslationsBatch, setPricesBatch and setAlteredPricesBatch.