Update variant image by graphql

Hello,

in graphql admin api V2023-07
I want to change a variant’s image, it’s says that we should use “mediaId” in “productVariantUpdate” to do that as “imageId” is deprecated (docs).

So I tried this

mutation UpdateProductVariantWithMedia($input: ProductVariantInput!) {
  productVariantUpdate(input: $input) {
    product{
      id
    }
    productVariant{
      id,image{
        id,altText,url
      }
      media(first:10){
        nodes{
          id,alt,mediaContentType,preview{
            image{
              id, altText,url
            }
          }
        }
      }
    }
    userErrors {
      field
      message
    }
  }
}

with those vars

{
  "input": {
    "id": "gid://shopify/ProductVariant/<numbers>",
    "mediaId": "gid://shopify/MediaImage/<numbers>"
  }
}

but it returns this

"data": {
    "productVariantUpdate": {
      "product": {
        "id": "gid://shopify/Product/7048653144201"
      },
      "productVariant": {
        "id": "gid://shopify/ProductVariant/40857155731593",
        "image": null,
        "media": {
          "nodes": []
        }
      },
      "userErrors": []
    }
  }

I also checked shopify admin side in case of it was a graphql error but variant has no image, I don’t understand how am I supposed to do this request.

Thanks if you can find why.

Hi @AW1234 ,

Thanks for your post. There are some great modern process docs about adding media to product variants using the productVariantAppendMedia mutation instead of productVariantUpdate at the following link, the whole page is really good with step 3 linked here specifically being about adding media to variants:
https://shopify.dev/docs/apps/online-store/media/product-variants#step-3-attach-media-to-an-existing-product-variant

It looks like you already have the MediaImage ID and it’s important to note that the docs mention attaching the media to the product first before attaching it to the variant. There’s a separate page about creating media and attaching it to products which can be found here:

https://shopify.dev/docs/apps/online-store/media/products

If you have further questions about this you may want to post on the Product specific discussion board instead.

Hope you have a great day,
Jon551

1 Like

productUpdateMedia might do it for you … you do need the (old) mediaIds to replace … jb

1 Like