GraphQL execute an Automation Command problem


#1

Dear All,

We have a web service creating orders on SambaPOS. We can create orders with no problems however when I try to execute an Automation Command on the orderline I recieve { “message”: “An error has occurred.”}. The command I send is mutation cancel{executeAutomationCommandForTerminalTicket(orderUid:“XhInw8YkmU-ZMSKccquCmA”, terminalId:“f_cxMQ6u_0GtBt2Q2PG6Rw”, name:“İkram”, value:"") {id}}. When I try to replicate the command flow through graphiql interface the automation command is executed without any problems. I will appreciate any advises.

Thanks,

Nihat


#2

We gotta see your code to understand the problem. There is no way we can help with just that info.


#3

Hi Kendash,

I thought it would be too confusing to post the code. Sorry about that. The php code related to the problem is below:

//The user and terminal details are stored in DB as well as Message Server settings

        $GetSessionTokenSQL = "Select * from ConnectionData order by ID desc";
        $GetSessionToken = mysqli_query($DBConnection, $GetSessionTokenSQL);
        $SessionToken = mysqli_fetch_array($GetSessionToken);


        $url = $SambaSettings["Address"] . ":" . $SambaSettings["Port"] . '/api/graphql';

        $query = 'mutation loadTicket{loadTerminalTicket(terminalId:\"' . $TerminalID . '\", ticketId:\"' . $TicketID . '\"){id, uid, number}}';

//I can load the ticket without any problems.

        $header = array();
        $header[] = 'Authorization: bearer ' . $SessionToken["access_token"];
        $header[] = 'dataType: json';
        $header[] = 'Content-type: application/json';

        $fields_string = '{ "query": "' . $query . '", "variables" : null , "operationName" : null }';

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);
echo "load result: " . $result . “
”;
//close connection
curl_close($ch);

        $TicketList = json_decode(trim($result), TRUE);


        for ($fidx = 0; $fidx < sizeof($FreeProductsArray); $fidx++) {

//$FreeProductsArray is an array that stores the orderUIDs of the orderlines that we want execute the commands on
$query = ‘mutation cancel{executeAutomationCommandForTerminalTicket(orderUid:"’ . $FreeProductsArray[$fidx] . ‘", terminalId:"’ . $TerminalID . ‘", name:“İkram”, value:"") {id}}’;
echo “free : " . $query . “
”;
//This query here returns { “message”: “An error has occurred.”}
$fields_string = ‘{ “query”: "’ . $query . '”, “variables” : null , “operationName” : null }’;
//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);
echo "Free result : " . $result . ‘
’;

            $RequestResult = json_decode($result, TRUE);
        }

        $query = 'mutation CloseTicket{closeTerminalTicket(terminalId:\"' . $TerminalID . '\" )}';
        echo "6 : " . $query . "<br>";
        $fields_string = '{ "query": "' . $query . '", "variables" : null , "operationName" : null }';

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

I hope this makes it more clear. Please let me know if any more clarification is required for the code.


#4

Dear All,

I have found the source of the problem. I missed to escape the " in the graphql command and that was the problem. In case anyone else experiences the same thing I wanted put a note here.