GraphQL Admin API - productVariantsBulkCreate - UnitCost Field not defined

GraphQL Admin API - productVariantsBulkCreate - UnitCost Field not defined

BaseB
Shopify Partner
4 0 0

Hi,

I have the below mutation for productVariantsBulkCreate.
It is pretty much working except for the unitCost where I get the error that unitCost is not defined.

Does anyone know what might be causing this?

Mutation:

mutation CreateProductVariants($productId: ID!, $variantsInput: [ProductVariantsBulkInput!]!) {
        productVariantsBulkCreate(productId: $productId, variants: $variantsInput) {
          productVariants {
            id
            title
            displayName
            price
            selectedOptions {
              name
              value
            }
            inventoryItem {
              id
              unitCost{
                amount
                currencyCode
              }
              sku
              countryCodeOfOrigin
              harmonizedSystemCode
              tracked
              measurement{
                weight{
                  unit
                  value
                }
              }
              requiresShipping
            }
            inventoryPolicy
            inventoryQuantity
            selectedOptions {
              name
              value
            }
          }
          userErrors {
            field
            message
          }
        }
      }

variables:

{
        price: record.productPrice, // Variant price
        optionValues: [ // Options for the variant (e.g., Size, Color)
          { name: size, optionName: "Size" },
          { name: color, optionName: "Color" },
        ],
        inventoryItem: {
          sku: `${record.productName} - ${color} - ${size}`,
          tracked: trackQuantity,
          requiresShipping: isPhysicalProduct,
          harmonizedSystemCode: productData.hsCode,
          countryCodeOfOrigin: productData.countryOfOriginCode,
          unitCost:{
            amount: cost,
            currencyCode: currencyCode,
          },
          measurement:{
                weight:{
                  unit: weightUnit,
                  value: weight,
                },
              },
        },
        inventoryPolicy: continueSelling ? "CONTINUE" : "DENY",
        inventoryQuantities: [
          {
            availableQuantity: 100,
            locationId: podifylocationID, // Location ID
          },
        ],
      };


Thanks in advance!
Bas

Replies 2 (2)

FastBundle-Jay
Tourist
10 0 1

It looks like the issue here is that the unitCost field isn’t actually supported in the productVariantsBulkCreate mutation in Shopify’s GraphQL API. That’s why you’re getting the error saying it’s not defined.

Why does this happen?

Shopify’s API has specific fields that it allows in each mutation, and unitCost isn’t one of the fields allowed in this particular mutation. Cost information, like unitCost, is handled separately in Shopify and can’t be set while creating product variants in bulk.


How to Fix This

  1. Remove unitCost from the Input: If you don’t absolutely need to include unitCost in this step, just remove it from the input. Your mutation should work without it. Here’s how your inventoryItem input would look:

    {
      "inventoryItem": {
        "sku": `${record.productName} - ${color} - ${size}`,
        "tracked": trackQuantity,
        "requiresShipping": isPhysicalProduct,
        "harmonizedSystemCode": productData.hsCode,
        "countryCodeOfOrigin": productData.countryOfOriginCode,
        "measurement": {
          "weight": {
            "unit": weightUnit,
            "value": weight
          }
        }
      }
    }

    This way, the mutation doesn’t try to send a field that Shopify doesn’t recognize.

  2. Set the Cost Separately: If you need to set or update the unitCost, you can do it after the variants are created by using a different mutation. Shopify provides a way to update inventory item details with the inventoryItemUpdate mutation. Here’s an example:

    Mutation to Update Unit Cost:

    mutation UpdateUnitCost($inventoryItemId: ID!, $unitCost: MoneyInput!) {
      inventoryItemUpdate(id: $inventoryItemId, cost: $unitCost) {
        inventoryItem {
          id
          cost {
            amount
            currencyCode
          }
        }
        userErrors {
          field
          message
        }
      }
    }

    Example Variables:

    {
      "inventoryItemId": "gid://shopify/InventoryItem/1234567890",
      "unitCost": {
        "amount": "15.00",
        "currencyCode": "USD"
      }
    }

    So the workflow would look like this:

    • First, create the product variants with the productVariantsBulkCreate mutation.
    • Then, update the cost for each inventory item using the inventoryItemUpdate mutation.
  3. Double-Check Shopify’s API Docs: Shopify’s API is pretty strict about what fields can be used where. It’s always a good idea to check the documentation for the mutation you’re using. In this case, productVariantsBulkCreate doesn’t allow unitCost.


The Bottom Line

The error is happening because unitCost isn’t supported in this mutation. To fix it:

  • Remove unitCost from your input for the bulk create mutation.
  • Use a separate mutation (inventoryItemUpdate) to set the cost after the variants are created.

Let me know if you have more questions or need help setting this up! 😊

Please let me know if it works by a Like or marking it as a solution!
Best Regards | Jay

Fast Bundle - Seamless Bundle Builder for Shopify Merchants.
BaseB
Shopify Partner
4 0 0

Thanks for the information!
That is indeed how far I got as well. For now I have left the cost out and then the action is running as expected.

I do wonder if the documentation on Shopify's side is up to date on this.
On the .dev docs page for productVariantsBulkCreate (https://shopify.dev/docs/api/admin-graphql/2024-10/mutations/productvariantsbulkcreate) it does show that cost should be available under variants > inventoryItem > cost

BaseB_0-1736428325751.png


However when I update to this structure:

 

mutation CreateProductVariants($productId: ID!, $variantsInput: [ProductVariantsBulkInput!]!) {
        productVariantsBulkCreate(productId: $productId, variants: $variantsInput) {
          productVariants {
            id
            title
            displayName
            price
            selectedOptions {
              name
              value
            }
            inventoryItem {
              id
              sku
              cost
              countryCodeOfOrigin
              harmonizedSystemCode
              tracked
              measurement{
                weight{
                  unit
                  value
                }
              }
              requiresShipping
            }
            inventoryPolicy
            inventoryQuantity
            selectedOptions {
              name
              value
            }
          }
          userErrors {
            field
            message
          }
        }
      }

 


it gives the error response:
"equestError: Field 'cost' doesn't exist on type 'InventoryItem'"

Extensions: 

{

"code": "undefinedField",

"fieldName": "cost",

"typeName": "InventoryItem"

}

Options: {
"method": "POST",
"url": "https://podify-pro.myshopify.com/admin/api/2024-10/graphql.json"
}

path: [
"mutation CreateProductVariants",
"productVariantsBulkCreate",
"productVariants",
"inventoryItem",
"cost"
]

responseBody:
{
"errors": [
{
"extensions": {
"code": "undefinedField",
"fieldName": "cost",
"typeName": "InventoryItem"
},
"locations": [
{
"column": 15,
"line": 16
}
],
"message": "Field 'cost' doesn't exist on type 'InventoryItem'",
"path": [
"mutation CreateProductVariants",
"productVariantsBulkCreate",
"productVariants",
"inventoryItem",
"cost"
]
}
]
}


I might be missing a step here, but I think my set up aligns with the documentation.
Only the documentation might not match the actual set up.


If you see any missing steps or errors on my side, please do let me know!
Thanks in advance!