Create Variant with productVariantsBulkCreate but missing created ids in response

Hello,

I’m working hard to upgrade to version 2024-07 (the same with 2024-10).

I would create a variant with the following request. The response is successful but contains not the created variantId and also not the productId.

Nevertheless in the Shopify-Backend the new variant then exists with the provided options but the price and the sku was not taken over.

I miss some error messages in the response. What I’m doing wrong here?

Request => POST: https://secret.myshopify.com/admin/api/2024-07/graphql.json
{
  "query": "mutation($variants: [ProductVariantsBulkInput!]!) { productVariantsBulkCreate(productId: \"gid://shopify/Product/9605053710656\", strategy: REMOVE_STANDALONE_VARIANT, variants: $variants) { product { id } productVariants { id } } }",
  "variables": {
    "variants": [
      {
        "inventoryItem": {
          "sku": "13791161",
          "tracked": true
        },
        "price": 3332.5,
        "compareAtPrice": 3749.1667,
        "optionValues": [
          {
            "id": "gid://shopify/ProductOptionValue/4066001224000"
          },
          {
            "id": "gid://shopify/ProductOptionValue/4066001256768"
          }
        ]
      }
    ]
  }
}

Response => OK
{
  "data": {
    "productVariantsBulkCreate": {
      "product": null,
      "productVariants": []
    }
  },
  "extensions": {
    "cost": {
      "requestedQueryCost": 10,
      "actualQueryCost": 10,
      "throttleStatus": {
        "maximumAvailable": 2000.0,
        "currentlyAvailable": 1990,
        "restoreRate": 100.0
      }
    }
  }
}

For more information. Here is the request of the product (with its default variant) before I did the request above.

Request => POST: https://secret.myshopify.com/admin/api/2024-07/graphql.json
{
  "query": "query { productVariants(first: 1, query: \"product_id:9605053710656\") { edges { node { id sku inventoryItem { id sku } selectedOptions { name value } product { id options { id name position optionValues { id name hasVariants } } } } } } }"
}

Response => OK
{
  "data": {
    "productVariants": {
      "edges": [
        {
          "node": {
            "id": "gid://shopify/ProductVariant/49749142110528",
            "sku": "",
            "inventoryItem": {
              "id": "gid://shopify/InventoryItem/51404363333952",
              "sku": ""
            },
            "selectedOptions": [
              {
                "name": "Groesse",
                "value": "50cm"
              },
              {
                "name": "Farbe",
                "value": "-"
              }
            ],
            "product": {
              "id": "gid://shopify/Product/9605053710656",
              "options": [
                {
                  "id": "gid://shopify/ProductOption/12015969239360",
                  "name": "Groesse",
                  "position": 2,
                  "optionValues": [
                    {
                      "id": "gid://shopify/ProductOptionValue/4066001224000",
                      "name": "50cm",
                      "hasVariants": true
                    }
                  ]
                },
                {
                  "id": "gid://shopify/ProductOption/12015969272128",
                  "name": "Farbe",
                  "position": 3,
                  "optionValues": [
                    {
                      "id": "gid://shopify/ProductOptionValue/4066001256768",
                      "name": "-",
                      "hasVariants": true
                    }
                  ]
                }
              ]
            }
          }
        }
      ]
    }
  },
  "extensions": {
    "cost": {
      "requestedQueryCost": 8,
      "actualQueryCost": 8,
      "throttleStatus": {
        "maximumAvailable": 2000.0,
        "currentlyAvailable": 1992,
        "restoreRate": 100.0
      }
    }
  }
}

Thank you advance.

Best regards

Florian

HI @ffetech

I’d suggest to start from the exact example we have in our documentation https://shopify.dev/docs/api/admin-graphql/unstable/mutations/productvariantsbulkcreate and work your way back to your needs

1 Like

Now I realized the userErrors possibility and I set optionValues like in the example and get the error “You need to add option values for Groesse”. I don’t understand the error message. The option values are existing in the product.

Request => POST: https://ffe-tech-dev1.myshopify.com/admin/api/2024-10/graphql.json
{
  "query": "mutation($variants: [ProductVariantsBulkInput!]!) { productVariantsBulkCreate(productId: \"gid://shopify/Product/9605053710656\", strategy: REMOVE_STANDALONE_VARIANT, variants: $variants) { userErrors { message } product { id } productVariants { id } } }",
  "variables": {
    "variants": [
      {
        "inventoryItem": {
          "sku": "13791161",
          "tracked": true
        },
        "price": 3332.5,
        "compareAtPrice": 3749.1667,
        "optionValues": [
          {
            "name": "50cm",
            "optionName": "Groesse"
          },
          {
            "name": "-",
            "optionName": "Farbe"
          }
        ]
      }
    ]
  }
}

Response => OK
{
  "data": {
    "productVariantsBulkCreate": {
      "userErrors": [
        {
          "message": "You need to add option values for Groesse"
        }
      ],
      "product": null,
      "productVariants": []
    }
  },
  "extensions": {
    "cost": {
      "requestedQueryCost": 10,
      "actualQueryCost": 10,
      "throttleStatus": {
        "maximumAvailable": 2000.0,
        "currentlyAvailable": 1990,
        "restoreRate": 100.0
      }
    }
  }
}

Prior I successful created the options in the parent product. So they must be available.

Request => POST: https://ffe-tech-dev1.myshopify.com/admin/api/2024-10/graphql.json
{
  "query": "mutation($options: [OptionCreateInput!]!) { productOptionsCreate(productId: \"gid://shopify/Product/9605053710656\", options: $options) { userErrors { message } product { id } } }",
  "variables": {
    "options": [
      {
        "name": "Groesse",
        "position": 2,
        "values": [
          {
            "name": "50cm"
          }
        ]
      },
      {
        "name": "Farbe",
        "position": 3,
        "values": [
          {
            "name": "-"
          }
        ]
      }
    ]
  }
}

I created the options on the parent product with productOptionsCreate. There’s a field variantStrategie with default value LEAVE_AS_IS. The descriptions says here:

No additional variants are created in response to the added options. Existing variants are updated with the first option value of each option added.”

But in my case variants ARE created.

I think then productVariantsBulkCreate should not work for existing variants with the same options.

But then the error message “You need to add option values for Groesse” is misleading.

Hi @ffetech ,

I’m having a hard time following the issues you are running into.
IIUC the original issue you ran into got resolved, you are seeing an error on your request.
If you think there is a diff issue (sorry but i am a bit doubtful) please start a new thread

Ok. Thank you :slightly_smiling_face: Doubtful? (I never reported an issue, I asked for what I’m doing wrong, sorry)