Graphql productVariantsBulkCreate with media

Hi everyone,

I am having issues generating variations with images using the productVariantsBulkCreate with graphql.

My mutation:

mutation productVariantsBulkCreate($productId: ID!, $variants: [ProductVariantsBulkInput!]!) { 
	productVariantsBulkCreate(productId: $productId, variants: $variants) {
		product {
			id title 
		} 
		productVariants { 
			id selectedOptions { 
				name value 
			} 
			price 
		} 
		userErrors { 
			field message 
		} 
	} 
}

Here is a modified snippet of what i’m sending. The links in my code are valid, but changed here:

    {
        "productId": "gid://shopify/Product/1000000000001",
        "variants":
        [
            {
                "optionValues":
                [
                    {
                        "optionName": "Color",
                        "name": "Black"
                    },
                    {
                        "optionName": "Size",
                        "name": "Small"
                    }
                ],
                "inventoryItem":
                {
                    "sku": "100001",
                    "tracked": false
                },
                "mediaSrc": "https://url.com/image1.jpg",
                "price": "19.99"
            },
            {
                "optionValues":
                [
                    {
                        "optionName": "Color",
                        "name": "Black"
                    },
                    {
                        "optionName": "Size",
                        "name": "Medium"
                    }
                ],
                "inventoryItem":
                {
                    "sku": "100002",
                    "tracked": false
                },
                "mediaSrc": "https://url.com/image2.jpg",
                "price": "19.99"
            },
            {
                "optionValues":
                [
                    {
                        "optionName": "Color",
                        "name": "White"
                    },
                    {
                        "optionName": "Size",
                        "name": "Large"
                    }
                ],
                "inventoryItem":
                {
                    "sku": "100003",
                    "tracked": false
                },
                "mediaSrc": "https://url.com/image3.jpg",
                "price": "19.99"
            }
        ]
    }

The listing variations do successfully get created, but the mediasrc doesn’t appear to do anything.

Any help would be appreciated

Hi @Andrew_Lyle you first need to include all the images in the media field and then match mediaSrc for the variant with one of the media originalSource:

mutation productVariantsBulkCreateWithMedia(
  $productId: ID!
  $media: [CreateMediaInput!]!
  $variants: [ProductVariantsBulkInput!]!
) {
  productVariantsBulkCreate(
    productId: $productId
    media: $media
    variants: $variants
  ) {
    product {
      id
      title
    }
    productVariants {
      id
      selectedOptions {
        name
        value
      }
      price
    }
    userErrors {
      field
      message
    }
  }
}
{
  "productId": "gid://shopify/Product/1000000000001",
  "media": [
    {
      "originalSource": "https://url.com/image1.jpg",
      "alt": "Black - Small"
    },
    {
      "originalSource": "https://url.com/image2.jpg",
      "alt": "Black - Medium"
    }
  ],
  "variants": [
    {
      "optionValues": [
        { "optionName": "Color", "name": "Black" },
        { "optionName": "Size",  "name": "Small" }
      ],
      "inventoryItem": {
        "sku": "100001",
        "tracked": false
      },
      "mediaSrc": "https://url.com/image1.jpg",
      "price": "19.99"
    },
    {
      "optionValues": [
        { "optionName": "Color", "name": "Black" },
        { "optionName": "Size",  "name": "Medium" }
      ],
      "inventoryItem": {
        "sku": "100002",
        "tracked": false
      },
      "mediaSrc": "https://url.com/image2.jpg",
      "price": "19.99"
    }
  ]
}

Hope this helps!

Thanks for the help! This helped a lot. There was just some missing that I filled in. Here is the solution for anyone searching:

Mutation:

mutation productVariantsBulkCreateWithMedia($productId: ID!, $media: [CreateMediaInput!]!,	$variants: [ProductVariantsBulkInput!]!) {
	productVariantsBulkCreate(productId: $productId, media: $media,	variants: $variants) {
		product {
			id
			title
		}
		productVariants {
			id
			selectedOptions {
				name
				value
			}
			price
		}
		userErrors {
			field
			message
		}
	}
}

Here is my json:

{
    "query": "mutation_above",
    "variables":
    {
        "productId": "gid://shopify/Product/10000000001",
        "media":
        [
            {
                "originalSource": "https://www.url.com/image1.jpg",
                "alt": "Black - Preview",
                "mediaContentType": "IMAGE"
            },
            {
                "originalSource": "https://www.url.com/image2.jpg",
                "alt": "White - Preview",
                "mediaContentType": "IMAGE"
            }
        ],
        "variants":
        [
            {
                "optionValues":
                [
                    {
                        "optionName": "Color",
                        "name": "Black"
                    },
                    {
                        "optionName": "Size",
                        "name": "Small"
                    }
                ],
                "inventoryItem":
                {
                    "sku": "100001",
                    "tracked": false
                },
                "mediaSrc": "https://www.url.com/image1.jpg",
                "price": "19.99"
            },
            {
                "optionValues":
                [
                    {
                        "optionName": "Color",
                        "name": "Black"
                    },
                    {
                        "optionName": "Size",
                        "name": "Medium"
                    }
                ],
                "inventoryItem":
                {
                    "sku": "100002",
                    "tracked": false
                },
                "mediaSrc": "https://www.url.com/image1.jpg",
                "price": "19.99"
            },
            {
                "optionValues":
                [
                    {
                        "optionName": "Color",
                        "name": "White"
                    },
                    {
                        "optionName": "Size",
                        "name": "Large"
                    }
                ],
                "inventoryItem":
                {
                    "sku": "100003",
                    "tracked": false
                },
                "mediaSrc": "https://www.url.com/image2.jpg",
                "price": "19.99"
            }
        ]
    }
}
1 Like