Correct ImageIds on ProductMedia in comparison to Rest

Hello!

I am currently working on rewriting logic to store product images. So far so good, but I’ve found an issue.

Using the Admin REST API to create ProductImages you receive a specific image id which we store in a database. When using GraphQL to query through products to get all the media I receive totally different Ids then stored. So i figured that the image_id we store in our database is the id you receive when you get the preview image node id. So far so good.

But now to my actual problem - we use stagedUploadsCreate to upload ProductImages via GraphQL and appending the ProductImages. When calling productCreateMedia i get an MediaImage ID as response. This id is not the same as the one you receive from the REST endpoint. So I tried using the preview node to fetch the correct id. Problem is once I receive the response from the request, the status of the image is not “READY” as expected and so i get no id at all.

mutation productCreateMedia($media: [CreateMediaInput!]!, $productId: ID!){ productCreateMedia(media: $media, productId: $productId){ media{ id preview{ <-- returns null until status "READY" image{ url id } } status } product{ id } userErrors{ field message } mediaUserErrors{ field message } } }

Is there a way to fetch the id I need without doing another request ?

Hi Jhorn,

Thanks for flagging this behaviour, just to confirm is this the ID which is not available when you use the productCreateMedia mutation instead of REST?


As an alternative to REST are you querying the media field on the product object after you run the productCreateMedia mutation?

Yes exactly thats the ID i am missing out when calling productCreateMedia mutation. It’s stated in the documentation that this ID ( assuming its the MediaPreviewImage ) is only available once the media creation process has been finished.

I wanna figure out if there’s a way to retrieve the ID without having to run a GraphQL Query again on productMedia

Hi again Jhorn,

I’ve confirmed with the product team that you can’t currently use a synchronous approach to get the ID back while calling the productCreateMedia mutation (how you would previously with REST).

It would be helpful to learn a bit more about your use case, our product team is curious to know "if you are trying to get product_image id from graphql, if they are trying to map media to product image? If that is the case then we do not expose product_image.id in GraphQL.

HI Liam,

alright thanks for letting me know. Might have to run an additional query to fetch the ids.

So we have a table where we store all image ids we retrieve from REST, compare them on daily basis to see if images need to be deleted, created or just let them be. Since GraphQL doesnt return the id required on the mutation Call, I am trying to figure out what would be the best approach since the productMedia ID and the image ID do differ. I guess for my use case I will have to remap the REST image ids towards the productMedia IDs from GraphQL to have a proper solution

Thanks Jhorn - this is very helpful info for our product teams to have when evaluating the best approach forward.