Issue with Creating Products with Variants via productCreate Mutation in Shopify

AI-generated summary

A developer encountered an error when attempting to create Shopify products with variants using the productCreate mutation. The mutation failed with the error “Field is not defined on ProductCreateInput” when trying to include variant details like price, SKU, and barcode directly in the product creation.

Root Cause:
The productCreate mutation doesn’t support variant fields directly in its input structure.

Solution (Resolved):
The issue requires a two-step approach:

  1. Create the base product using productCreate mutation with basic fields (title, vendor, status, tags)
  2. Add variant details separately using the productVariantsBulkCreate mutation, which accepts:
    • Product ID from step 1
    • Variant-specific fields (price, SKU, barcode, inventory)
    • Strategy parameter (e.g., “REMOVE_STANDALONE_VARIANT”)

This workaround successfully creates draft products with complete variant information by separating product creation from variant assignment.

Hello,

I’m trying to create products in draft mode in my Shopify store using the productCreate mutation. Below is the mutation I am using, along with the input for creating the product:

const mutation = gql`
    mutation productCreate($product: ProductCreateInput) {
      productCreate(product: $product) {
        product {
          id
          title
          status
        }
        userErrors {
          field
          message
        }
      }
    }
  `;

Input (currently without variants):

const productInput = {
        title: product.Description || "Untitled Product",
        vendor: product.SupplierCode || "Unknown Vendor",
        status: "DRAFT",
        // variants: [
        //   {
        //     sku: product.PartNumber || "UNKNOWN-SKU",
        //     price: product.CurrentActivePrice || 0.0,
        //     barcode: product.UPC || null,
        //   },
        // ],
        tags: ["importLStoSHOP"],
      };

I am currently able to successfully create the product with only the title, vendor, status, and tags. However, when I try to include the variants section (which I have commented out in the code), I encounter the following error:

Variable $product of type ProductCreateInput was provided an invalid value for variants (Field is not defined on ProductCreateInput)

Question:

  • How can I add fields (such as price, cost, SKU, barcode, inventory quantity, and track inventory as true) to the product input in the mutation?
  • What changes should I make to properly define and include the variants field in the product creation process?

Any help or suggestions would be greatly appreciated as I’m still learning GraphQL.

Thank you!

Hi, I’m experiencing the same issue. Have you found a solution yet? Thanks!

First, create a unique ID for the product using its title, status, and tags. Then, use this ID to push updates for price, inventory, cost, SKU, and other details.

Create the Product: The first operation in your code is creating the product in Shopify. You use the productCreate mutation to create the product with the required details such as the title, vendor, and status (which is set to “DRAFT” in your example).

const productCreateMutation = gql`
mutation productCreate($product: ProductCreateInput!) {
    productCreate(product: $product) {
      product { id title status }
      userErrors { field message }
    }
  }
`;

// Example product input
const productInput = {
  title: product.Description || "Untitled Product",
  vendor: product.SupplierCode || "Unknown Vendor",
  status: "DRAFT", // Product is created in draft mode
  tags: ["importLStoSHOP"],
};

const productCreateResponse = await client.request(productCreateMutation, { product: productInput });

Above step creates the product but does not include any price or variant details.

Now, Create the Variant and Set the Price: Once the product is created, you can add a variant to it using the productVariantsBulkCreate mutation. This is where you set the price, SKU, barcode, and other variant-specific information.

const productVariantsBulkCreateMutation = gql`
  mutation productVariantsBulkCreate($productId: ID!, $strategy: ProductVariantsBulkCreateStrategy, $variants: [ProductVariantsBulkInput!]!) {
    productVariantsBulkCreate(productId: $productId, strategy: $strategy, variants: $variants) {
      product { id }
      productVariants { id inventoryItem { id } }
      userErrors { field message }
    }
  }
`;

// Example variant data
const variants = [{
  inventoryItem: { sku: product.PartNumber || "UNKNOWN-SKU" },
  price: product.CurrentActivePrice || 0.0, // Set the price here
  barcode: product.UPC || null,
}];

const variantsResponse = await client.request(productVariantsBulkCreateMutation, {
  productId,
  strategy: "REMOVE_STANDALONE_VARIANT",
  variants,
});

Above step creates the variant for the product and includes the price for that variant.