Can GraphQL be used to create new products and their variants?

Can GraphQL be used to create new products and their variants?

Omar_Hernandez
Shopify Partner
1 0 1

 

Hi there!,

I'm trying to create new products and variants using GraphQl. I've tried different approaches using the examples given by the Shopify docs but I received  an error as output. These are the examples we've used and the error. Any ideas ?

 

What we are trying to do is create a product and all its variants. Is it possible ? 

 

Thank you in advance

 

 

mutation productVariantsBulkCreate($productId: ID!, $variants: [ProductVariantsBulkInput!]!) {
  productVariantsBulkCreate(productId: $productId, variants: $variants) {
    userErrors {
      field
      message
    }
    product {
      id
      options {
        id
        name
        values
        position
        optionValues {
          id
          name
          hasVariants
        }
      }
    }
    productVariants {
      id
      title
      selectedOptions {
        name
        value
      }
    }
  }
}
{
  "productId": "gid://shopify/Product/8332936610007",
  "variants": [
    {
      "optionValues": [
        {
          "name": "Red",
          "optionName": "Color"
        },
        {
          "name": "Brand new style",
          "optionName": "Brand and Style"
        },
        {
          "name": "Silk",
          "optionName": "Material"
        }
      ],
      "price": 22
    }
  ]
}
{
  "errors": [
    {
      "message": "The option doesn't exist",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "productVariantsBulkCreate"
      ]
    }
  ],
  "data": {
    "productVariantsBulkCreate": null
  },
  "extensions": {
    "cost": {
      "requestedQueryCost": 13,
      "actualQueryCost": 10,
      "throttleStatus": {
        "maximumAvailable": 2000,
        "currentlyAvailable": 1924,
        "restoreRate": 100
      }
    }
  }

We're following the examples in this part of the docs:

https://shopify.dev/docs/api/admin-graphql/2024-04/mutations/productVariantsBulkCreate#examples-Crea... 

 

Reply 1 (1)

kissa14
Shopify Partner
7 0 1

Hi Omar Hernandez, 

I have the same problem with version 2024-04.. any solution?
even adding: "strategy": "REMOVE_STANDALONE_VARIANT"

 

Thank's