productVariantBulkCreate fails silently

Hi, I’m trying to create products and variants via the GraphQL Admin API. Precisely, I

  • create the product with media and options
  • then try to create the product variant with option values and media id assigned.

While the former works perfectly, the latter fails silently. I get an empty result instead of an array of the variants created. Further, the variants are created but incomplete. The image is present in the product but isn’t assigned to the variant. Price and inventory item information also haven’t been applied. I have no idea why.

Here an example mutation with real data.

mutation CreateProductVariants(
      $productId: ID!,
      $media: [CreateMediaInput!],
      $variants: [ProductVariantsBulkInput!]!,
      $strategy: ProductVariantsBulkCreateStrategy
) {
      productVariantsBulkCreate(
            productId: $productId,
            media: $media,
            variants: $variants,
            strategy: $strategy
      ) {
            productVariants {
                  id
                  displayName
      	      product {
                        id
                  }
                  position
                  selectedOptions {
                        name
                  }
                  image {
                        id
                        url
                        width
                        height
                  }
            }
            media(first: 100) {
		      edges {
			      node {
                              id
                              mediaContentType
                              alt
                              status
            	      	preview {
                                    image {
                                          id
                                          altText
                                          url
                                          width
                                          height
                                    }
            			}
		            }
                  }
      		inventoryItem {
                        id
                  }
                  inventoryQuantity
                  inventoryPolicy
                  price
                  createdAt
                  updatedAt
            }
      }
}

and the data passed:

Array
(
    [productId] => gid://shopify/Product/8574635835549
    [variants] => Array
        (
            [0] => Array
                (
                    [price] => 19.90
                    [mediaId] => gid://shopify/MediaImage/30908724215965
                    [inventoryItem] => Array
                        (
                            [sku] => 1000132-1118-003-XS
                            [cost] => 11.87
                            [measurement] => Array
                                (
                                    [weight] => Array
                                        (
                                            [value] => 250
                                            [unit] => GRAMS
                                        )

                                )

                        )

                    [optionValues] => Array
                        (
                            [0] => Array
                                (
                                    [optionName] => Color
                                    [name] => black
                                )

                            [1] => Array
                                (
                                    [optionName] => Size
                                    [name] => XS
                                )

                        )

                )

        )

    [strategy] => REMOVE_STANDALONE_VARIANT
    [media] => 
)

Any help is appreciated.

Please can you check the userErrors that are returned from the query. They might contain information that could help you :slight_smile:

Pretty much all mutations contain userErrors so it needs to be checked on all of them :slight_smile:

That’s the point. There is no useErrors. The result is simply empty (see below) and only the first variant is created but is incomplete. Doesn’t look like an expected behavior to me.

{
  "data": { "productVariantsBulkCreate": { "productVariants": [] } },
  "extensions": {
    "cost": {
      "requestedQueryCost": 43,
      "actualQueryCost": 10,
      "throttleStatus": {
        "maximumAvailable": 2000,
        "currentlyAvailable": 1990,
        "restoreRate": 100
      }
    }
  }
}

User errors should be at the same level as Product variants. In your query you haven’t got the user errors block in order to see the errors.

I put your query into a GraphQL App and it highlighted that firstly you weren’t getting the userErrors back as I elluded to.
Also that your response query was incorrect as you had media at the wrong point in the query as it wasn’t inside productVariants, think you’d just bracketed } incorrectly.
You should check for these as well with your queries before running them.

I’ve fixed the query like so:

mutation CreateProductVariants($productId: ID!, $media: [CreateMediaInput!], $variants: [ProductVariantsBulkInput!]!, $strategy: ProductVariantsBulkCreateStrategy) {
  productVariantsBulkCreate(
    productId: $productId
    media: $media
    variants: $variants
    strategy: $strategy
  ) {
    productVariants {
      id
      displayName
      product {
        id
      }
      position
      selectedOptions {
        name
      }
      image {
        id
        url
        width
        height
      }
      inventoryItem {
        id
      }
      inventoryQuantity
      inventoryPolicy
      price
      createdAt
      updatedAt
      media(first: 100) {
        edges {
          node {
            id
            mediaContentType
            alt
            status
            preview {
              image {
                id
                altText
                url
                width
                height
              }
            }
          }
        }
      }
    }
    userErrors {
      message
      field
      code
    }
  }
}

This will likely return data and/or userErrors now it is valid

1 Like

Thanks a lot, Jordan. :pray:
While the media thingy was an error due to autocompletion when I manually formatted the query, the—obviously—missing “useErrors” brought the missing insights. My bad.

1 Like

Awesome! Glad it solved it for you :blush: yeah thought it might be the case with the brackets, happens to us all