API PHP item_id as $variable

,

Hi,

I’m writing a function that updates certain items we have. Now when I use the create_update mutation, and send item_id as a variable I get:

{"errors":[{"message":"Variable myItemName of type Int! was provided invalid value","locations":[{"line":1,"column":11}],"value":null,"problems":[{"path":[],"explanation":"Expected value to not be null"}]}],"account_id":XXXXXXX}

However if I were to omit the variable, and enter the ID in code: item_id:YYYYYYYYYYYY the update works just fine. I am providing sample code under.

function testUpdate() {
    global $token, $apiUrl, $headers, $generalBoardID;
    $testID = YYYYYYYYYY;
    //settype($testID, "Integer");
    echo "item_id: $testID\n";
    echo "Datatype of testID: ".gettype($testID);
    echo "\n";

    $query = 'mutation ($myItemName: Int!) { create_update (item_id:$myItemName, body:"HELLO") { id } }';
    $vars = ['myItemName' => $testID];

    $data = @file_get_contents($apiUrl, false, stream_context_create([
    'http' => [
        'method' => 'POST',
        'header' => $headers,
        'content' => json_encode(['query' => $query]),
    ]
    ]));
    $responseContent = json_decode($data, true);
    echo json_encode($responseContent);
}

Any suggestions as to what is going wrong?

Hey @nidhogg

The problem is in the mutation ($myItemName: Int!) { part.

Just remove ($myItemName: Int!) and use

$query = 'mutation { create_update (item_id:$myItemName, body:"HELLO") { id } }';

Thank you for the reply @rob .

That could have been an option, but that leads to variable not declared error.

{"errors":[{"message":"Variable $myItemName is used by  but not declared","locations":[{"line":1,"column":35}],"fields":["mutation","create_update","item_id"]}],

Error lies there, forgot to add inn ‘variables’ => $vars