GraphQL API update product and variants

Solved
Highlighted
New Member
2 1 1

G'day,

 

I need to update a product ( tags, productType, ... )  and all its variants with the same ( price, compareToPrice ).

Obviously, I can update the product and then loop through the (max 6) variants. This works ok, but ...

Is it possible to do this in one? If so, where/how do I specify the 'ProductVariantInput' and 'IDs' in the example below.

 

 mutation {
               productUpdate(input: {id: "gid://shopify/Product/193......83", tags: ["Jacket, warm"], productType: "on-sale"}) {
               product {
                              id
                              tags
                              productType
                              variants(first: 6) {
                                    edges {
                                          node {
                                                   id
                                                   price
                                                   compareAtPrice
                                                    }
                                              }
                                         }
                                 }
                           }
                       }    

0 Likes

Success.

New Member
2 1 1

I have figured it out now. This is a solution, just in case someone else wants to know. 

The string for the mutation is generated within the Java program.

The number of blocks var0: ... var1: ... is determined by the number of variants of the product.

 

mutation {
      prod: productUpdate(input: {id: "gid://shopify/Product/193......99", tags: ["Knitwear, New Arrivals, Sale, Shorts"], productType: "Sale-Item"})

                {
                product {
                tags
                productType
               }
       }
       var0: productVariantUpdate(input: {id: "gid://shopify/ProductVariant/176.....219", price: 69.00, compareAtPrice: 89.95})

       {
       productVariant {
                price
                compareAtPrice
                }
     }
     var1: productVariantUpdate(input: {id: "gid://shopify/ProductVariant/1762....987", price: 69.00, compareAtPrice: 89.95})

    {
    productVariant {
               price
              compareAtPrice
            }
     }
}

1 Like