Node.js mutation example using Axios

If anyone familiar with Node.js wants to show an example of how to post to the new V2 Graphql api. Fetch or Request methods would be great as well.

Hi @kevinmarchese

Here’s a sample request to create an item in a board:

const axios = require('axios')
const body = {
  query: `
	mutation ($boardId: Int!, $groupId: String!, $itemName: String!, $columnValues: JSON!) {
	  create_item (
	    board_id: $boardId,
	    group_id: $groupId,
	    item_name: $itemName,
	    column_values: $columnValues
	  ) {
	    id
	  }
	}
  `,
  variables: {
  	boardId: 1234567,
	groupId: "topics",
	itemName: "New item name",
	columnsValues: JSON.stringify({ status: { index: 1 } })
  }
}
axios.post(`https://api.monday.com/v2`, body, {
    headers: {
      Authorization: 'API_TOKEN'
    }
  })
  .catch(err => {
    console.error(err.data)
  })
  .then(res => {
    console.log(res.data)
  })
3 Likes

@danielco :star_struck: I don’t even know what to say right now! Thank you of course! but its not enough for the time you saved me! I love yo…wait too strong. Seriously @danielco THANK YOU SO MUCH MAN!!!

Now that I know what I’m doing wrong and the correct structor I’m confident that I can figure the rest out based on this…so you gave me a great gift today and I hope one day I can at least pay it forward.

Glad i could help :slight_smile:
Let me know if you need more examples or have any issues

Hello Daniel,
This particular example I can’t get working. I get this error:
{ errors:
[ { message: ‘Integer isn’t a defined input type (on $boardId)’,
locations: [Array],
fields: [Array] } ] }

-All i changed was board id, and added my api key

Never Mind. there was a typo. It all works now.
This is the code that worked for me:

const axios = require(‘axios’);
axios({ url: ‘https://api.monday.com/v2’, headers: {
Authorization: myapikey
},
method: ‘post’,
data: {
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 } },
variables: {
boardId: 12345,
groupId: “Test”,
itemName: "New item ",
columnValues: JSON.stringify({ status: { index: 1 } })
}
}
}).then((result) => {
console.log(result.data)

})
.catch(function (error) {
console.log(error)
});
1 Like

Thanks for posting your revised code! I’ve gone ahead and edited the original post to reflect the correct formatting.

2 Likes

yes, I remember having to change to $boardId: Int! because it was just integer!, or something along these lines. It was close enough though to be very helpful.

2 Likes

I’m glad to hear it was helpful @kevinmarchese!