I’m working with Shopify’s GraphQL API using pure cURL in PHP and getting a weird syntax error. Here’s my current implementation:
$mutation = <<<GQL
mutation UpdateStock(\$data: [InventorySetQuantitiesInput!]!) {
inventorySetQuantities(input: \$data) {
inventoryAdjustmentGroup {
createdAt
reason
changes {
name
delta
}
}
userErrors {
field
message
}
}
}";
GQL;
$params = [
'data' => [
'name' => 'available',
'quantities' => [
'inventoryItemId' => $itemId,
'locationId' => $storeId,
'quantity' => $amount
],
'reason' => 'correction'
]
];
return $this->sendGraphQLRequest($mutation, $params);
The error message says: syntax error, unexpected invalid token ("\"") at [33, 2]
What’s causing this parsing issue and how can I fix the code structure?
classic heredoc mistake! that "; at the end is wrong - heredoc closes with just GQL; and nothing else. remove the extra quote-semicolon and you’re good to go.
yep, that annoying extra quote is what messes things up. just take out the "; after GQL; n’ it’ll work fine. hope that helps!
You’re mixing heredoc syntax with regular string endings. Your code uses <<<GQL and GQL; correctly, but then you’re adding "; after it. Don’t do that. The GQL; delimiter already closes the string completely - no extra quotes or semicolons needed. PHP sees that extra "; and tries to parse it as separate code, which throws the unexpected token error. Just end your mutation at GQL; and you’re done. I’ve hit this same issue switching from regular strings to heredoc for GraphQL queries. It’s weird at first since we’re used to closing strings with quotes and semicolons.
Had the same problem when I started using heredoc for GraphQL queries in PHP. That extra semicolon and quote at the end is breaking everything. When you write GQL; that’s it - you’re done. Adding the "; after creates an invalid token the parser can’t read. Heredoc is picky about closing syntax, especially with GraphQL’s messy string formatting. Just remove that trailing quote-semicolon and your mutation should work. Also make sure your closing GQL; sits flush against the left margin with zero whitespace - that’ll cause the same parsing errors.
This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.