Shopify graphql API bulk mutation of products, is it possible?

Shopify graphql API bulk mutation of products, is it possible?

mttwTHQ1216
Shopify Partner
1 0 0

I have made a program that changes the category of products based on a pre-mapped csv file. The file consists of a product type and taxonomy ID, all the products and are then collected by a query and their product types are compared to the ones on the file, if there's a match on the file, then a mutation is created where that product's category is changed with connecting to the Shopify API. The problem is that its pretty unfeasible for large scale lists of products as it takes about a second per product, so 1000 products becomes 15 minutes. And the majority of that time is the API call itself as I've tested with just the comparison algorithm itself and fake data.

 

 So I am wondering if its possible to apply a mutation to an array of products in one go instead of individually?

 

This is my mutation code, can i change the input of the productid to an array of ids? Thanks

 

 

mutation = """
    mutation ProductUpdate($input: ProductInput!) {
      productUpdate(input: $input) {
        product {
            id
            category {
                id
            }
        }
        userErrors {
          field
          message
        }
      }
    }
    """

    variables = {
        "input": {
            "id": product_id,
            "category": category_id  
        }
    }

 headers = {'Content-Type': 'application/json',
    'X-Shopify-Access-Token': TokenVar}
    response = requests.post(g_url, json={'query': mutation, 'variables': variables}, headers=headers)

 

 

Reply 1 (1)

Ablestar_Daniel
Shopify Partner
154 28 57

Hi @mttwTHQ1216,

Shopify does have a the option to run mutations in bulk but it's a little more complicated: you need to create a JSONL files with the variables you want to use, upload that to a temporary location in Shopify, create a 'bulk operation' and then wait for it to complete.

There's some more details in the docs that walk through the whole process.

Also, if in the future you have trouble getting answers here I'd try out the official Shopify dev forums at https://community.shopify.dev/. There's a lot more of the technical GraphQL nitty gritty on those forums.

Best,
Daniel

Founder @ Ablestar ✦
Manage product data, spreadsheets, Google Shopping data, and metafields with Ablestar Bulk Product Editor. Use previews, undos, and automations to be confident your product data is correct.
Please don't forget to Like and Mark Solution to the post that helped you. Thanks!