With the new graphQL changes moving to Product.media instead of Product.images coming in API version 2023-07, we’re looking for the easiest way to manage updates to products with images via the graphQL API. I’ve checked out the Managing Media for Products article, but it didn’t cover my use-case.
Sometimes our users will add images to or remove images from the product in our system. We used to send an array of image source URLs, but now we have no way to connect a given MediaImage to images in our system (MediaImage.orginalSrc gives us a Shopify CDN URL instead of our original source URL). It looks like we’ll have to delete all the product’s images and re-create them any time we update a product. Has anyone found an efficient way to manage these Media records so we don’t have to re-create all the images every time we want to update a product?
Hey @downleft-dev ! This is a great question.
Taking a look at the guide you linked that would cover the basic process of adding new product media and adding it to a product.
For your specific use case, it would work well to query the current product media to get the MediaImage ID and then use the fileUpdate mutation to update the image. This will ensure the media is updated everywhere it’s used. There’s an example mutation in our documents here: https://shopify.dev/docs/api/admin-graphql/2023-07/mutations/fileupdate#examples-Update_an_image
The MediaImage ID can also be a constant that you can use so you don’t need to rely on specific source URL’s or ID’s.
Hope that helps!
Kyle G.
Hey Kyle, that’s a good idea. When creating a product with the productCreate($input, $media) mutation, I didn’t see a way to tell which image was which based on any field I could query in the response. Is it guaranteed that the product media is returned in the same order I initially supplied?
Great question @downleft-dev ! When creating a product with the productCreate with media mutation, requesting the media id in the response will return the mediaImage ID that you can then use to associate the media for any future updates and eliminate any reliance on media being returned in the same order it was initially supplied. Note: the initial response will return the media in the same order you provided in your variables.
Simplified example here of returning the media ID
productCreate(input: $input, media: $media) {
product {
id
title
media(first: 10) {
nodes {
alt
→ id }
Thanks for the detailed response that should be sufficient for us. Thank you!