3

I'm using the Monday.com API in a React bootstrapped app.

I can create a new board item successfully with an item name...

monday.api(
    `mutation {
        create_item (
          board_id: ${myBoardId}, 
          group_id: "new_group", 
          item_name: "new item creation",
        )
        {
          id
        }
      }`
 )

...but when I try to add additional column values I get a POST 500 error.

monday.api(
    `mutation {
        create_item (
          board_id: ${myBoardId}, 
          group_id: "new_group", 
          item_name: "new item creation",
          column_values: {
            person: 00000000,
          }
        )
        {
          id
        }
      }`
 )

I've tried passing a string in for the column values...

let columnValues = JSON.stringify({
          person: 00000000,
          text0: "Requestor name",
          text9: "Notes",
          dropdown: [0],
        })

    monday.api(
      `mutation {
        create_item (
          board_id:${myBoardId}, 
          group_id: "new_group", 
          item_name: "test item",
          column_values: ${columnValues}
      )
        {
          id
        }
      }`
    ).then(res => {
      if(res.data){
        console.log('new item info: ', res.data)
      };
    });

...but no item is created, I get no errors and nothing logs.

sbaden
  • 555
  • 3
  • 16
  • 30

2 Answers2

2

Here was the solution:

const variables = ({
    boardId : 00000000,
    groupId: "new_group",
    itemName : "New Item",
    columnValues: JSON.stringify({
        people78: { 
            personsAndTeams: [
            {
                id: 00000000, 
                kind: "person"
            }
            ] 
        },
        text0: "Yosemite Sam",
        dropdown: {
            labels: [
            "TAM"
            ]
        },
    })
});

const query = `mutation create_item ($boardId: Int!, $groupId: String!, $itemName: String!, $columnValues: JSON!) { 
    create_item (
        board_id: $boardId,
        group_id: $groupId,
        item_name: $itemName, 
        column_values: $columnValues
    ) 
    { 
        id
    } 
}`;

monday.api(query, {variables}).then((res) => {
    console.log('new item info: ', res);
});
sbaden
  • 555
  • 3
  • 16
  • 30
1

Probably the problem is with your GraphQL query. To create an Item in Monday you need to provide the column_values. Unfortunately in Monday API documentation it's not clearly specified how it should be done. The answer how you need to provision column_values to create_item query can be found in Changing column values with JSON section of Monday API documentation

Please try the following code:

const board_id = "<board_id>"
const group_id = "<group_id>"
const person_id = "<person_id>"
const item_name = "<item name>"

let query = `mutation { create_item (board_id:${board_id},group_id:  \"${group_id}\",item_name: \"${item_name}\",column_values: \"{\\\"person\\\":\\\"${person_id}\\\"}\"){id}}`

monday.api(query).then((res) => {
     console.log(res);
})

Where,

  • <board_id> - your Board ID
  • <group_id> - your Group ID
  • <item_name> - the name of item you want to create
  • <person_id> - User ID

If you console.log query, you should see something like the following:

mutation { create_item (board_id:1293656973,group_id: "group_1",item_name: "New Item",column_values: "{\"person\":\"14153685\"}"){id}}

Please note that in query variable I am using String Interpolation. So the string should start and end with ` sign

You can also always dump your GraphQL queries and test them online using Monday API Try-It yourself tool

Alexey Zelenkin
  • 701
  • 1
  • 6
  • 13
  • Tried passing dynamic data into your query string and am getting error: Unnecessary escape character \" – sbaden May 14 '21 at 22:19
  • const groupId = 'new_group'; const itemName = 'test item'; const personId = 20602378; let query = `mutation { create_item (board_id: ${myBoardId},group_id: \"${groupId}\",item_name: \"${itemName}\",column_values: \"{\\\"person\\\":\\\"${personId}\\\"}\"){id}}` monday.api(query).then((res) => { if(res.data){ console.log('new item info: ', res.data) }; }) – sbaden May 14 '21 at 22:19