Search code examples
graphqlshopifyshopify-api

How to use Shopify Graphql ProductVariantsBulkInput


I am trying to bulk update the price on multiple product variants at once. I'm just copying the "interactive" example from Shopify's page

All I did was copy and paste the code and put in my own id's. And of course it does not work.

It looks like this:

mutation productVariantsBulkUpdate($variants: [ProductVariantsBulkInput!]!, $productId: ID!) {
  productVariantsBulkUpdate(variants: $variants, productId: $productId) {
    product {
      cursor
    }
    productVariants {
      cursor
    }
    userErrors {
      code
      field
      message
    }
  }
}

With Variables like this:

{
  "variants": [
    {
      id:  "gid://shopify/ProductVariant/39369514385591",
      price: "50.00"
    }
  ],
  "productId": "gid://shopify/Product/6591908577463"
}

I'm getting this error: Variables are invalid JSON: Unexpected token i in JSON at position 30.


Solution

  • It's OK for me. (with some quick tweaks)

    I tweaked the request a little since the cursor is not present in the product/variant object, don't know why Shopify has not updated the example in their docs.

    mutation productVariantsBulkUpdate($variants: [ProductVariantsBulkInput!]!, $productId: ID!) {
      productVariantsBulkUpdate(variants: $variants, productId: $productId) {
        product {
          id
        }
        productVariants {
          id
          price
        }
        userErrors {
          code
          field
          message
        }
      }
    }
    

    So try to fix the query and remove the cursor object and check if you are using the proper end-point since the bulk operation is available in the unstable version only if I'm not mistaken.

    See the image below showing that the response is OK for me.

    enter image description here