Manage variant media with the GraphQL Admin API
As of version 2020-10, you can use the GraphQL Admin API to add a media object of type image to a product's variants.
For example, you can create a product, variant, and media object, and attach a media object to a variant in a single step. You can also associate the product's media to its variants.
This guide illustrates how to manage media on product variants using the GraphQL Admin API.
RequirementsAnchor link to section titled "Requirements"
- You've completed our Getting started with the GraphQL Admin and REST Admin APIs guide and you're authenticated with the API.
- You've created products and product variants in your development store.
To use the variant media GraphQL mutations, your app needs to request the following access scopes for a Shopify store:
read_products: Allows your app to read products.
write_products: Allows your app to write mutations on products.
ConsiderationsAnchor link to section titled "Considerations"
- Although the media input accepts an array, as of version 2020-10, the input is limited to one media object, which must be an image.
mediaSrcattribute is currently non-operational on
Query a product and display its variantsAnchor link to section titled "Query a product and display its variants"
The following query returns information about any image media objects that are attached to the variants of a product.
Create a product and associate media to its variantAnchor link to section titled "Create a product and associate media to its variant"
When you create a new product using the
productCreate mutation, you can include the
mediaSrc attribute to specify the URL of the media to associate with the variant. This field match one of the media
originalSource fields that you created on the product.
When you create the variant options, make sure that you include the
options values. These values must match the options that were set up on the product. For example, if the product has the variant options
[“Color”, “Size”], then each variant should have a color and a size option, like
Attach media to an existing product variantAnchor link to section titled "Attach media to an existing product variant"
To attach a media object to an existing product variant, you must do the following:
- Create the media on the product using the
- If the product doesn't have variants, then you must create at least one variant on the product.
When you have a product with a media object and variants, you can use the
productVariantAppendMedia mutation to associate the media
id to one or more variant
ids, as shown in the following example query:
Detach media from a variantAnchor link to section titled "Detach media from a variant"
To detach media from a variant, you can use the
After you use the mutation, the media won't be associated with the variant anymore, but the media will still be associated with the product. You can use the
productDeleteMedia mutation to remove the media from the product as well.
The following example demonstrates how to use the
productVariantDetachMedia mutation to disassociate a media object from a product variant.