Hey folks,
I am trying to get IDs of all the images when I create products OR using createProductMedia mutations via GraphQL Admin API. In both cases, I am getting null values in the query response. I thought it might not work with productCreate mutation so I tried createProductMedia as per docs here in which the example query has id set in image field to return.
Broadly, I want to store these references of images in my database so that upon product update, I can delete them and create new images OR update images. Both cases I mentioned require image ids and I want to avoid the additional query to do a lookup of images by product ID. In a nutshell, if the product already exists in Store (I have reference to the product in my DB) I want to update the product data and efficiently update all the images. In order to do this, I want to store the image IDs in my database so, upon the next update, I can delete them and add new ones.
Query:
mutation createProductMedia($id: ID!, $media: [CreateMediaInput!]!) {
productCreateMedia(productId: $id, media: $media) {
media {
...fieldsForMediaTypes
mediaErrors {
code
details
message
}
}
product {
id
}
mediaUserErrors {
code
field
message
}
}
}
fragment fieldsForMediaTypes on Media {
alt
mediaContentType
preview {
image {
id // This always returns null or is empty
originalSrc
}
}
status
}
Variables:
{
"id": "gid://shopify/Product/6592068878536",
"media": [
{
"originalSource": "https://dummyimage.com/150x200/6f00a3/ffffff.jpg&text=Placeholder",
"alt": "Lets try this baby",
"mediaContentType": "IMAGE"
}
]
}