Hello,
I’m migrating from REST API to GraphQL, I need create variants for product id.
I followed example on the documentation but the server replies “Bad Request”.
What am I doing wrong?
“query”: “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 } } } }”,
“variables”: {
“productId”: “gid://shopify/Product/1072481060”,
“variants”: [
{
“optionValues”: [
{
“name”: “Red”,
“optionName”: “Color”
}
],
“price”: 29.5
}
]
}
}’
Thank for replay
Not sure if its just how you’ve pasted it in here, but you’ve got an extra } at the end of your variables, I’ve removed it below
{
"productid": "gid://shopify/Product/1072481060",
"variants": [
{
"optionValues": [
{
"name": "Red",
"optionName": "Color"
}
],
"price": 29.5
}
]
}
Sorry, I made a mistake pasting the code, after working on it the correct code is:
"query": "mutation productVariantsBulkCreate($productId: ID!, $variants: [ProductVariantsBulkInput!]!) { productVariantsBulkCreate(productId: $productId, variants: $variants) { productVariants { id } } }",'
"variables": {
"productId": "gid://shopify/Product/9890232041812",
"variants": [
{"optionValues": [
{"name": "Red","optionName": "Colore"},
{"name": "40","optionName": "Taglia"}
],
"price": "199.99",
"inventoryItem": {"sku":"12345"},
"inventoryQuantities": {"availableQuantity":77,"locationId":"gid://shopify/Location/104004911444"}
}
]
}
I think your price needs to be a number.
You also need to check for userErrors on the graphql response (needs adding to your query as well) productVariantsBulkCreate - GraphQL Admin