Have your say in Community Polls: What was/is your greatest motivation to start your own business?

Help with productCreate and productVariantsBulkCreate in GraphQL API (2024-07 Version)

Help with productCreate and productVariantsBulkCreate in GraphQL API (2024-07 Version)

KhushiPandya
Shopify Partner
12 0 3

Hello Shopify Community,

I am working on creating products using the productCreate and productVariantsBulkCreate mutations in the 2024-07 version of the GraphQL API. However, I am running into an issue where the process doesn't work as expected.

Problem:

  • When I use the productCreate mutation, it creates a default variant with a price of 0 and no inventory or quantity, as productOptions doesn't support variants directly.
  • To add variants, I call productVariantsBulkCreate, passing the necessary options (e.g., price, quantity, SKU, and inventory). While this successfully creates additional variants, it does not override the default variant created by productCreate.

Questions:

  1. How can I manage this default variant so it doesn't remain as a placeholder with no proper data?
  2. Is there an alternative approach to create products with variants that bypasses this issue with minimum GraphQL calls?image.png

If anyone has faced a similar issue or knows a better way to handle this, I would appreciate your guidance.

Thank you in advance for your help!

Replies 3 (3)

KhushiPandya
Shopify Partner
12 0 3

In addition to it, here are mutation I am using for 2024-07,

mutation productVariantsBulkCreate($productId: ID!, $variants: [ProductVariantsBulkInput!]!) { productVariantsBulkCreate(productId: $productId, variants: $variants) { userErrors { field message } product { id title options { id name values position } } productVariants { id title selectedOptions { name value } compareAtPrice createdAt inventoryPolicy inventoryQuantity position price barcode taxable updatedAt inventoryItem { id measurement { weight { unit value } } requiresShipping sku tracked } } } }

    and
mutation CreateProduct($product: ProductInput!, $media: [CreateMediaInput!]!) { productCreate(media: $media, input: $product) { product { id: legacyResourceId productType status vendor title description metafields(first: 250) { edges { node { id key value type namespace } } } options { id name position values optionValues { id name hasVariants } } media(first: 10) { edges { node { id mediaContentType } } } } userErrors { field message } } }

ArtemFokin
Shopify Partner
18 0 3

have you tried productVariantUpdate mutation after product creation? and pass first variant from creation response?

ArtemFokin
Shopify Partner
18 0 3

Also check these mutations

inventoryActivate({

locationId,
inventoryItemId: variant.inventoryItem.id,
available: quantity,

})

inventoryAdjustQuantities({
{
reason: "correction",
name: "available",
changes: [
{
delta: quantity,
inventoryItemId: variant.inventoryItem.id,
locationId,
},
],
}})