Mutating data through API

泄露秘密 提交于 2019-12-24 20:06:53

问题


I am using Zapier to create a Monday.com task every time a new lead is created in Copper (my CRM). The problem is that Zapier only allows the information to stored in the task name on Monday.com. I have created a webhook that is supposed to parse the needed data out of the Monday.com task title and update the column values as needed. However, my code is currently not doing that. I am receiving no errors when I create a task yet the columns are not populating properly. I am not sure what is the issue.

Here is the code:

const express = require('express')
const path = require('path')
const bodyParser = require('body-parser')
const PORT = process.env.PORT || 5000

const updateMultipleColumnValues = require("./updateMultipleColumnValue").updateMultipleColumnValue;

const app = express();

  app.use( bodyParser.json());
  app.use(bodyParser.urlencoded({extended: true}));
  app.set('views', path.join(__dirname, 'views'));
  app.set('view engine', 'ejs');



  app.post('/', (req, res) => {
    console.log(req.body)
    const { boardId, pulseId } = req.body.event

      let parsedRecord = extractData(req.body.event.pulseName)
      console.log(parsedRecord);

      let newData = {
        "text0": parsedRecord.DURATION
      };

      let stringData = JSON.stringify(newData);

      console.log(boardId);
      console.log(pulseId);
      console.log(stringData);
      updateMultipleColumnValues(boardId, pulseId, stringData);
    res.json(parsedRecord);
  });

  app.listen(PORT, () => console.log(`Listening on ${ PORT }`));





// console.log(extractData(targetStr, fields));

function extractData(str) {
  let fields = ['DATE', 'TIME', 'DURATION', 'TYPE'];

  return str.split(/\s*\|\s*/).reduce((res, entry) => {
    let dat = entry.split(/\s*:\s*/);
    return fields.indexOf(dat[0]) > -1 ? Object.assign(res, { [dat[0]]: dat[1] }) : res;
  }, {});
}

Here is the updateMultipleColumnValue file:

const executeMondayQuery = require("./executeMondayQuery").executeMondayQuery;

const updateMultipleColumnValue = async (boardId, itemId, newData) => {
  const updateColumnValueBody = {
    query: `mutation {
                change_multiple_column_values(
                board_id: ${boardId},
                item_id: ${itemId},
                column_values: ${newData}
                ) { id }
            }`
  };

  await executeMondayQuery(updateColumnValueBody);
};

exports.updateMultipleColumnValue = updateMultipleColumnValue;

Monday.com use graphQL


回答1:


Edit: We'll release a new Copper integration next week, so if you don't want to go through the trouble of recreating it with Zapier, you can wait for this release.

Anyway, there are a two things you probably need to address in this implementation.

TL;DR Solution

You need to escape your string before sending it in the mutation. This should work:

const updateColumnValueBody = {
    query: `mutation {
                change_multiple_column_values(
                board_id: ${boardId},
                item_id: ${itemId},
                column_values: "${newData.replace(/"/g, '\\"')}"
                ) { id }
            }`
  };

A word about GraphQL error handling

I'm not sure what's the logic inside the executeMondayQuery function, but if a parsing error occurred during the execution of a GraphQL mutation, the HTTP response itself still has a 200 status code.

The body of the response will contain an errors array describing all syntax errors in the payload.

Escaping strings in GraphQL JSON attributes

In this specific case the issue is probably that your newData (originally stringData) isn't escaped, and isn't wrapped in double quotes (indicating it's a string).

What happens is that after string interpolation your payload looks like so:

mutation {
  change_multiple_column_values(
    board_id: 12345678,
    item_id: 87654321,
    column_values: {"text": "123"}
  ) { id }
}

But the column_values attribute of change_multiple_column_values accepts a JSON formatted string

So what you actually want is:

mutation {
  change_multiple_column_values(
    board_id: 12345678,
    item_id: 87654321,
    column_values: "{\"text\": \"123\"}"
  ) { id }
}


来源:https://stackoverflow.com/questions/58534326/mutating-data-through-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!