ProductVariantCreate -Create variants using GraphQL

Topic summary

A developer is encountering an error while attempting to create product variants using Shopify’s GraphQL API (specifically the productVariantsBulkCreate mutation). The error message indicates an issue with the variable type: Variable $variants of type [ProductVariantsBulkInput!]!

Key Details:

  • Using the 2024-10 API documentation as reference
  • Attempting to create variants with properties including optionValues, price, SKU, and mediaSrc
  • The productId is formatted as gid://shopify/Product/1111
  • Strategy set to REMOVE_STANDALONE_VARIANT

Current Status:

  • The developer has reviewed both official documentation and forum discussions without finding a solution
  • The code snippet shows a PHP implementation using Shopify’s GraphQL client
  • The mutation query structure appears partially corrupted or improperly formatted in the shared code

Issue Remains Unresolved:
The developer is seeking community assistance to identify the root cause of the variable type error and successfully create product variants via GraphQL.

Summarized with AI on November 4. AI used: claude-sonnet-4-5-20250929.

Create product variants using GraphQl API documentation (2024-10). Error occurred while creating variant,Tip: Variable $variants of type [ProductVariantsBulkInput!]!, I referred to the documentation and discussion forum, but did not find the problem. I hope this issue can be resolved in the forum. Thank you very much!

function addProductVariant($shop, $accessToken, $apiKey, $apiSecret, $scopes, $productId, $variables) {
        self::initializeContext($apiKey, $apiSecret, $shop, $scopes);

        $client = new Graphql($shop, $accessToken);
        $variables = [
            'productId' => 'gid://shopify/Product/1111',
            'strategy' => 'REMOVE_STANDALONE_VARIANT',
            'variants' => [
                [
                    'optionValues' => [
                            'name' => 'Style',
                            'optionName' => 'Style1'
                    ],
                    "price"=> 22,
                    'sku' => 'Jacket01',
                    'mediaSrc'=>'1.jpg'
                ],
                [
                    'optionValues' => [
                            'name' => 'Style2',
                            'optionName' => 'Style2'
                    ],
                    "price"=> 22,
                    'sku' => 'Jacket01-v2',
                    'mediaSrc'=>'1.jpg'
                ]
            ]
        ];

        try {
            $response = $client->query(['query' => self::getProductVariantsBulkCreateMutation(), 'variables' => $variables]);

            $data = $response->getDecodedBody();
            var_dump($data);
            $variants = $data['data']['productVariantsBulkCreate']['product']['variants']['edges'] ?? null;
            return $variants;

        } catch (Exception $e) {
            echo  $e->getMessage();
        }
    }

    private static function getProductVariantsBulkCreateMutation() {
        return <<<'GRAPHQL'
mutation productVariantsBulkCreate($productId: ID!, $variants: [ProductVariantsBulkInput!]!) {
    productVariantsBulkCreate(
      productId: $productId, 
      variants: $variants ) {
        product {
            id
            title
            variants(first: 10) {
                edges {
                    node {
                        id
                        title
                        price
                        sku
                        availableForSale
                    }
                }
            }
        }
        userErrors {
            field
            message
        }
    }
}
GRAPHQL;
    }