I cannot create multi variants product by graphql

Solved
Highlighted
New Member
2 0 0

I tried to run following data and received error message "The variant 'Default Title' already exists."

There are options and variant's title in query variables. Why do I get an error?

Query:

mutation productCreate($input: ProductInput!) {
  productCreate(input: $input) {
    product {
      id
    }
    shop {
      id
    }
    userErrors {
      field
      message
    }
  }
}

 

Query Variables:

{
  "input": {
    "title": "test product 1",
    "descriptionHtml": "<div>Hello!</div>",
    "options": ["size"],
    "variants": [
      {
        "title": "big",
        "barcode": "900000000001",
        "position": 1,
        "price": 100
      },
      {
        "title": "small",
        "barcode": "900000000002",
        "position": 2,
        "price": 100
      }
    ]
  }
}

 

Response:

{
  "data": {
    "productCreate": {
      "product": null,
      "shop": {
        "id": "gid://shopify/Shop/0000000000"
      },
      "userErrors": [
        {
          "field": [
            "variants",
            "1"
          ],
          "message": "The variant 'Default Title' already exists."
        }
      ]
    }
  }
}

 

 

0 Likes
Highlighted
Shopify Partner
543 39 110

This is an accepted solution.

Since you define options in the product mutation, you need to define them in the variants portion as well. Try this...

{
  "input": {
    "title": "test product 1",
    "descriptionHtml": "<div>Hello!</div>",
    "options": ["size"],
    "variants": [ {
        "title": "big",
        "options": ["big"],
        "barcode": "900000000001",
        "position": 1,
        "price": 100
      },
      {
        "title": "small",
        "options": ["small"],        
        "barcode": "900000000002",
        "position": 2,
        "price": 100
      }
]
  }
}
0 Likes
Highlighted
New Member
2 0 0

Thank you. It works!

0 Likes