Solved

Adding multiple product variants with graphql?

MrGuvna
Visitor
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)
awwdam
Shopify Staff
249 42 36

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 3 (3)

VivekH
Excursionist
12 1 14

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
249 42 36

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

chlorine_yantra
Shopify Partner
2 0 1

Hello! I am trying to use GraphQL to add/update standalone products as well as multi-variant products in Shopify. For Standalone products there will be only 1 variant so the variant parameter in the mutations can be variants (first : 1) {.....} (as shown below):

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

 But I will come across a multi-variant product where number variant will range anywhere from 2 to may be 100. Is there any way I can dynamically map the parameter first in the variant schema of the mutation  (e.g. Can I do something like variants (first: variants.length) {.....})

Thanks!