I can't insert values into my monday table via PHP

I can insert an item if it looks like this

$mutationQuery = 'mutation {
        create_item(
            board_id: ' . $boardId . ',
            group_id: "' . $groupId . '",
            item_name: "' . $cursus . '"
        ) {
            id
        }
    }';

but when i try to insert any value to another column i always get an error, am i doing something wrong?. this the code i have

$mutationQuery = 'mutation {
        create_item(
            board_id: ' . $boardId . ',
            group_id: "' . $groupId . '",
            item_name: "' . $cursus . '",
            column_values: {
                 "lange_tekst": { "text": "'.$cursus.'" }
            }
        ) {
            id
        }
    }';

this is my full code if you want to know

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $token = 'MY_API';
    $apiUrl = 'https://api.monday.com/v2';
    $headers = ['Content-Type: application/json', 'Authorization: ' . $token];

    $cursus = 'lolinit';

    $boardId = '5554649549';
    $groupId = 'topics';


    // Prepare mutation query
    $mutationQuery = 'mutation {
        create_item(
            board_id: ' . $boardId . ',
            group_id: "' . $groupId . '",
            item_name: "' . $cursus . '",
            column_values: {
                 "lange_tekst": { "text": "'.$cursus.'" }
            }
        ) {
            id
        }
    }';

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

    $responseContent = json_decode($data, true);

    // Handle the response as needed
    if (isset($responseContent['data']['create_item']['id'])) {
        echo 'Item created successfully!';
    } else {
        echo 'Error creating item.';
        if (isset($responseContent['errors'])) {
            echo 'API Error: ' . print_r($responseContent['errors'], true);
            echo '<pre>';
            print_r($responseContent);
            echo '</pre>';
        }
    }
}

It would be helpful if errors were included, since those often provide the direction to know whats wrong. If there were errors.

However, I do see the issue, the value of “column_values” needs to be a JSON string. You need create the column_values value as an object then serialize it, and then use that string in your query. Not just substitute the one bit of text into the object.

Also, investigate GraphQL variables, since that lets you make the query a simple string you don’t need to do any manipulation to. The API server just takes the a variables alongside the query in the request and then it handles all of the substitution, and you don’t have to worry about escaping quotes etc.

Lastly, remember the query is only a string. While its a string that looks like JSON it is not JSON. The request is JSON though, with the structure:

{
   "query": "mutation($boardId: ID!, $groupId: ID!, $cursus: String!, $values: JSON!)  {
        create_item(
            board_id: $boardId,
            group_id: $groupId,
            item_name: $cursus,
            column_values: $values
        ) {
            id
        }
    }"
   "variables": {
      "boardId": "5554649549",
      "groupId": "topics",
      "cursus": "lolinit",
      "values:  "json_encoded value of the values object"
   }
}
1 Like

thanks! it worked.

1 Like

Thank you @codyfrisch !!!

1 Like