Troubleshooting Shopify GraphQL API: Unexpected Token Error in PHP

I’m trying to use the Shopify GraphQL API with PHP and cURL. My code for setting inventory quantities is throwing an error. Here’s what I’ve got:

$query = <<<GRAPHQL
mutation InventoryUpdate($data: [StockAdjustmentInput!]!) {
    updateStockLevels(data: $data) {
        adjustmentRecord {
            timestamp
            cause
            modifications {
                item
                difference
            }
        }
        errors {
            attribute
            details
        }
    }
}
GRAPHQL;

$data = [
    'data' => [
        'type' => 'inStock',
        'adjustments' => [
            'productStockId' => $productStockId,
            'warehouseId' => $warehouseId,
            'amount' => $updatedAmount
        ],
        'cause' => 'stockTake'
    ]
];

$result = $this->sendGraphQLQuery($query, $data);

When I run this, I get an error saying ‘syntax error, unexpected invalid token (“"”) at [33, 2]’. Can someone explain what’s causing this and how to fix it? I’m new to GraphQL and could use some guidance. Thanks!

hey, i’ve dealt with this before. looks like ur json structure is off. try wrapping ur ‘data’ in square brackets like this:

$data = [
‘data’ => [[
‘productStockId’ => $productStockId,
‘warehouseId’ => $warehouseId,
‘adjustment’ => $updatedAmount,
‘type’ => ‘inStock’,
‘cause’ => ‘stockTake’
]]
];

shopify expects a list for StockAdjustmentInput. hope this helps!

The issue likely stems from a mismatch between your GraphQL query structure and the data you’re passing. Shopify’s API expects a specific format for inventory updates. Try modifying your data structure as follows:

$data = [
‘data’ => [
[
‘productStockId’ => $productStockId,
‘warehouseId’ => $warehouseId,
‘adjustment’ => $updatedAmount,
‘type’ => ‘inStock’,
‘cause’ => ‘stockTake’
]
]
];

This aligns the data with the expected StockAdjustmentInput format. Additionally, ensure your sendGraphQLQuery function properly encodes the variables as JSON. If problems persist, consider using Shopify’s GraphQL explorer to validate your query structure before implementation.

I’ve run into similar issues with the Shopify GraphQL API before. The error you’re seeing is likely due to a mismatch between your query structure and the data you’re sending.

From what I can see, your query is expecting a list of StockAdjustmentInput objects, but your data structure doesn’t quite match that. Try restructuring your $data array like this:

$data = [
    'data' => [
        [
            'productStockId' => $productStockId,
            'warehouseId' => $warehouseId,
            'adjustment' => $updatedAmount,
            'type' => 'inStock',
            'cause' => 'stockTake'
        ]
    ]
];

This should align better with the expected input format. Also, double-check that your sendGraphQLQuery function is properly encoding the variables as JSON before sending the request.

If you’re still having trouble, I’d recommend using Shopify’s GraphQL explorer to test your queries first. It can help catch these kinds of structural errors before you implement them in your code.