How to get MediaImage id from a ProductImage id

I have an app that manages product images. My database already has image ids stored (“gid://shopify/ProductImage/xxx”) for when I need to replace them.

I see productImageUpdate is deprecated and I should use productUpdateMedia instead. However, productUpdateMedia requires the media image ID “gid://shopify/MediaImage/yyy”.

Is there a way I can map “gid://shopify/ProductImage/xxx” to its equivalent “gid://shopify/MediaImage/yyy”?

Thanks!

Related Questions:

https://community.shopify.com/post/1642462

https://community.shopify.com/topic/1427060

Hey @ko-lem - this is a great question! The easiest way to do this, I believe, would be to compare the URL values for the respective ProductImage GIDs and the MediaImage GIDs. Here’s an example GQL query you could use:

{
product(id: “gid://shopify/Product/7330367832086”) {
images (first:10) {
edges {
node {
id
url
}
}
}
media(first: 10) {
edges {
node {
id
… on MediaImage {
id
image {
id
url
}
mediaContentType
originalSource {
fileSize
}
}
}
}
}
}
}

The media image and product image objects should both have the same URLs (since they basically just represent the same image file in different contexts). Hope this helps - let me know if we can clarify anything on our end!

Al | Shopify Developer Support

Got it, thanks.

That’s really inconvenient though. I wish ProductImage and MediaImage pointed to each other.

We appreciate your feedback! We’ll let the product team know it would valuable to have a connection or mapping between ProductImage and MediaImage objects.

Are there no endpoints for getting a MediaImage/ProductImage by gid directly? you would have to loop through all files on a different object?

Seconding a way to map one to one would be very nice. Above solution is very cumbersome / clunky. Thanks!

Any news about this? There should be a better way to link product images with media for the new endpoint