GraphQL Admin API - productVariantsBulkCreate - UnitCost Field not defined

Topic summary

A developer is encountering an error when trying to set the unitCost field using Shopify’s productVariantsBulkCreate GraphQL mutation. The API returns an “undefinedField” error indicating that cost (or unitCost) doesn’t exist on the InventoryItem type within this mutation.

Proposed Solution:

  • Remove the unitCost/cost field from the bulk create mutation input
  • Use a separate inventoryItemUpdate mutation after variant creation to set cost values
  • This two-step workflow: create variants first, then update costs separately

Documentation Discrepancy:
The original poster notes that Shopify’s official documentation (shopify.dev) shows cost as available under variants > inventoryItem > cost, but the actual API rejects this field. This suggests potential inconsistency between the documentation and the implemented API.

Current Status:
The mutation works successfully when the cost field is removed. The issue remains unresolved regarding why the documentation doesn’t match the API behavior, with the developer seeking confirmation on whether they’re missing a configuration step or if the documentation is outdated.

Summarized with AI on November 2. AI used: claude-sonnet-4-5-20250929.

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

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! :blush:

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

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!