0

I've this mutation that allows me to insert a client inside a collection in MongoDB. when i create the function the php trows a error 500. it's strange for me because it happens when i write the function, and not when i call it

the function is:

function graphQLPost(string $endpoint, string $query, array $variables = [], ?string $token = null)
{
    $payload =  json_encode(['query' => $query, 'variables' => $variables]);
    $headers = array();
    if ($token != null) {
        $headers[] = "Content-Type: application/json\r\n" .
        'Authorization: Bearer ' . $token;
    } else
        $headers[] = 'Content-Type: application/json';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $endpoint);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    // Submit the POST request
    $resultJson = curl_exec($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    // Close cURL session handle
    curl_close($ch);
    return array($resultJson, $httpcode);
}
// definiamo la mutation
$mutation = '
    mutation createClients($project_id: ID!, $input: ClientInput!) {
    createClients(project_id: $project_id, input: $input) {
      id
      firstName
      lastName
      tel
      email
      trattamento
      profilazione
      marketing
    }
  }  ';

// parametri da passare come argomenti
$param = [
    'project_id' => $PROJECT_ID,
    'input' => array(
        'firstName' => $nome,
        'lastName' => $cognome,
        'email' => $email,
        'tel' => $telefono,
        'trattamento' => $trattamento_DB,
        'profilazione' => $profilazione_DB,
        'marketing' => $marketing_DB,
        'status' => 'lead',

    )
];

// esecuzione del comando
$response = graphQLPost($API_URI, $mutation, $param, $TOKEN);
s.zane
  • 95
  • 1
  • 9
  • 2
    A 500 error is a generic server-error. You should check your web servers error log to see what the actual error message says. if you're working locally, you can make [errors and warnings](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) to show up directly on the screen. – Magnus Eriksson Sep 20 '20 at 11:30
  • Thank you @MagnusEriksson, your suggestion helped me to find out the error. just a really stupid error... I had a question mark before `string $token =null` – s.zane Sep 20 '20 at 11:34
  • _"I had a question mark"_ : This question mark is the indicator of a [nullable type](https://stackoverflow.com/a/48450841/9193372) as of PHP 7.1. – Syscall Feb 26 '21 at 10:41

1 Answers1

0

just find out why. i made a stupid syntax error here ?string $token = null

the question mark isn't necessary.

s.zane
  • 95
  • 1
  • 9
  • using php 7.1+ code [feature] used on server with lower php version interpreter ... is not a syntax error ;) – xadm Feb 26 '21 at 11:59
  • It allows to pass either `null` or `string` (as of php 7.1). It's called [nullable types](https://stackoverflow.com/a/48450841/9193372). – Syscall Mar 02 '21 at 18:03