Hi,
I’m trying to attach an image (MediaImage) to a ProductVariant using the Admin GraphQL API.
The previous developer used a mutation called updateProductVariantMetafields,
but it seems that this mutation no longer exists or no longer works in the current API versions.
Does anyone know which mutation is currently the correct one for attaching an image to a ProductVariant?
Any updated documentation or hints would be appreciated.
Thanks!
1 Like
Hey @user274 updateProductVariantMetafields isn’t a standard Shopify mutation. Your previous developer was probably using a custom wrapper function with that name.
For attaching a MediaImage to a variant via metafields, you’ll want to use metafieldsSet:
mutation SetVariantImageMetafield {
metafieldsSet(metafields: [
{
ownerId: "gid://shopify/ProductVariant/123456"
namespace: "custom"
key: "variant_image"
type: "file_reference"
value: "gid://shopify/MediaImage/789012"
}
]) {
metafields {
id
namespace
key
value
}
userErrors {
field
message
}
}
}
The important parts are using file_reference as the type and passing the MediaImage GID as the value. If you need to upload the image first, use fileCreate to get that GID.
You can also use productVariantUpdate with a metafields input if you’re updating other variant fields at the same time.