Adding multiple product variants with graphql?

Solved
MrGuvna
New Member
1 0 0
I'm following the GraphQL documentation for productCreate but I can't figure out how to add multiple variants when creating a product. I'm using the following mutation in the GraphiQL app:

 

    mutation productCreate($input: ProductInput!) {
      productCreate(input: $input) {
    userErrors {
          field
          message
        }
        shop {
          id
        }
        product {
          title
          handle
          variants {
            edges {
              node {
                title
              }
            }
          }
        }
      }
    }

 

 
And passing the following variables:
   

 

 {
     "input": {
        "title": "Testing Products",
      "handle": "test-product-2",
      "variants": [
        { "title": "Variant 1" },
          { "title": "Variant 2" }
      ]
    }
    }​

 

 
The error I am getting back is:
 

 

{
      "data": {
        "productCreate": {
          "userErrors": [
            {
              "field": [
                "variants",
                "1"
              ],
              "message": "The variant 'Default Title' already exists."
            }
          ],
          "shop": {
            "id": "gid://shopify/Shop/59898691733"
          },
          "product": null
        }
      },
      "extensions": {
        "cost": {
          "requestedQueryCost": 12,
          "actualQueryCost": 10,
          "throttleStatus": {
            "maximumAvailable": 1000,
            "currentlyAvailable": 990,
            "restoreRate": 50
          }
        }
      }
    }

 

 
I've scoured the internet trying to figure out how to add multiple variants when creating a product on Shopify, but I can't figure it out. Is it possible to do this?
Accepted Solution (1)

Accepted Solutions
awwdam
Shopify Staff
Shopify Staff
249 42 33

This is an accepted solution.

Hey @MrGuvna

Just wanted to expand on what was shared by @VivekH and a pass on a few more insights.

The "title" field in the "variants" input (ProductVariantInput.title - documentation here) is deprecated, and not a writeable field at this time. Instead, this is generated from the variant input "options" field - options must be a string for example: small, medium, red, blue, 50g, 100g etc. 

Below is a mutation and input I used to create a product with multiple variants - Cheers!


EXAMPLE REQUEST:

mutation productCreate($input: ProductInput!) {
  productCreate(input: $input) {
    shop {
      id
    }
    userErrors {
      field
      message
    }
    product {
      id
      variants(first: 5) {
        edges {
          node {
            id
            title
            barcode
          }
        }
      }
    }
  }
}

{
  "input": {
    "title": "New Product",
    "variants":[
      {
        "barcode": "654321",
        "options": "V.1"
      },
      {
        "barcode": "765432",
        "options": "V.2"
      }
    ]
  }
}

awwdam | API Support @ Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

View solution in original post

Replies 2 (2)
VivekH
Excursionist
12 1 12

Hi,

Try with this json instead

{
    "input": {
        "title""Testing Products",
        "handle""test-product-2",
        "variants": [
            {
                "sku""Variant 1"
            },
            {
                "sku""Variant 2"
            }
        ]
    }
}

You might have to add price if it's required.

 

awwdam
Shopify Staff
Shopify Staff
249 42 33

This is an accepted solution.

Hey @MrGuvna

Just wanted to expand on what was shared by @VivekH and a pass on a few more insights.

The "title" field in the "variants" input (ProductVariantInput.title - documentation here) is deprecated, and not a writeable field at this time. Instead, this is generated from the variant input "options" field - options must be a string for example: small, medium, red, blue, 50g, 100g etc. 

Below is a mutation and input I used to create a product with multiple variants - Cheers!


EXAMPLE REQUEST:

mutation productCreate($input: ProductInput!) {
  productCreate(input: $input) {
    shop {
      id
    }
    userErrors {
      field
      message
    }
    product {
      id
      variants(first: 5) {
        edges {
          node {
            id
            title
            barcode
          }
        }
      }
    }
  }
}

{
  "input": {
    "title": "New Product",
    "variants":[
      {
        "barcode": "654321",
        "options": "V.1"
      },
      {
        "barcode": "765432",
        "options": "V.2"
      }
    ]
  }
}

awwdam | API Support @ Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog