Alternative to GraphQl API productImageUpdate

I am currently using the productImageUpdate API to update images. However, this interface has been deprecated and the documentation recommends using productUpdateMedia instead. I found that the new interface does not meet my needs.

In the ImageInput fields of productImageUpdate, you can set src so that you can replace images without changing the image id, but this is not supported in productUpdateMedia.

Can someone please tell me how to replace the original API call?

1 Like

The idea that the productUpdateMedia mutation can’t update the image without changing the image Id does not agree with my experience. I was able to use the following query and variables to update the image without, presumably, changing the id.

Query:

mutation productUpdateMedia($media: [UpdateMediaInput!]!, $productId: ID!) {
  productUpdateMedia(media: $media, productId: $productId) {
    media {
      id
      alt
      status
    }
    mediaUserErrors {
      # MediaUserError fields
    }
    product {
      # Product fields
    }
  }
}

Variables:

{
  "media": [
    {
      "alt": "xxx",
      "id": "gid://shopify/MediaImage/123456789012345",
      "previewImageSource": "https://cdn.shopify.com/shopifycloud/plus_website/assets/global/shopify-plus-share-default-f432a4be6ee92bc59e226940c1accfa5b197850e9f347cec9d79880152a20299.png"
    }
  ],
  "productId": "gid://shopify/Product/987654321983"
}

Reference: https://shopify.dev/docs/api/admin-graphql/2023-07/mutations/productupdatemedia

1 Like

Thank you for the reply. It works.

1 Like

Cheers.

Can confirm this works.
This is my query:

mutation productUpdateMedia($media: [UpdateMediaInput!]!, $productId: ID!) {
productUpdateMedia(media: $media, productId: $productId) {
media {
id
alt
status
}
mediaUserErrors {
field
message
}
product {
id
}
}
}

And this is my variable:

{
“media”: [
{
“alt”: “zyz”,
“id”: “gid://shopify/MediaImage/0000000000123”,
“previewImageSource”: “https://MYURL.jpg
}
],
“productId”: “gid://shopify/Product/000000000123”
}

1 Like

Thanks for the code, it seems to work but I’m running into an issue, I’m trying to replace existing .jpg/.png’s with .webp and I think Shopify is being miserable and not letting it happen. If you try and use the new ‘replace’ function in the FILES page and try to replace a .jpg with a different file extension it moans about it there.

Has anyone had any luck changing format types? Or is it really being backward and only allowing jpg for jpg etc…

OK, doing more tests and using a super super basic version of @aliaz_1 mutation. I can replace an image using any image I provide a source for (not from the staged but just random weblinks). This changes the image successfully (yay!), however, it will convert any image sent into the same format as the original (boo!)

So if your original image is .jpg then everything becomes .jpg (e.g: gif>jpg, png>jpg, webp>jpg), this applies to .png/.webp/.gif as well.

Not sure why I was having issues with staged uploads not working at all but it’s evident that productUpdateMedia only allows a like for like swap. That’s my plan to bulk convert to webp out the window.