Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Graphql基因突变_Javascript_Vue.js_Graphql_Apollo_Vue Apollo - Fatal编程技术网

Javascript Graphql基因突变

Javascript Graphql基因突变,javascript,vue.js,graphql,apollo,vue-apollo,Javascript,Vue.js,Graphql,Apollo,Vue Apollo,我有一组足够相似的graphql类型,可以进行一般编辑。我已经编写了代码来显示它们并在客户端进行变异,但是现在我在编写变异查询时遇到了问题。目前的执行情况: let node1 = { id: 1, type: 'theme', fields: {name: 'potatoes'} }; let node2 = {

我有一组足够相似的graphql类型,可以进行一般编辑。我已经编写了代码来显示它们并在客户端进行变异,但是现在我在编写变异查询时遇到了问题。目前的执行情况:

 let node1 = {
                 id: 1,
                 type: 'theme',
                 fields: {name: 'potatoes'}       
             };

 let node2 = {
                 id: 1,
                 type: 'subject',
                 fields: {
                             short_name: 'cool potatoes',
                             long_name: 'cool potatoes that grow on the mountain'
                         }       
             };

....

 save: function (node) {
    this.$apollo.mutate({
      // Query
      mutation: gql`mutation ($id: Int!) {
      update_data_${node.type}(where: {id: {_eq: $id}}, _set: ${node.fields}) {
        affected_rows
      }
  }`,
      variables: {
        id: node.id
      }
    })
  }

不仅它现在不起作用(因为它不想正确地插值字段),而且我觉得我做错了什么。

我通过以下方法使它起作用:

_set: ${JSON.stringify(node.fields).replace(/"(w+)"s*:/g, '$1:')}

看起来很可怕,但很有效。我希望有一种更“graphql”的方法来实现这一点。

我通过以下方式实现了这一点:

_set: ${JSON.stringify(node.fields).replace(/"(w+)"s*:/g, '$1:')}
看起来很可怕,但很有效。我希望有一种更“graphql”的方法来实现这一点