javascript - Graphql 通用突变

标签 javascript vue.js graphql apollo vue-apollo

我有一组足够相似的 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”的方式来做到这一点。

关于javascript - Graphql 通用突变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58505491/

相关文章:

javascript - 有没有办法在加载 DOM 之后但在执行任何页面脚本之前运行 Google Chrome 内容脚本?

javascript - process.env.PWD 与 process.cwd()

javascript - 如何在 jquery 动态添加的 html 中呈现 vuejs 组件

javascript - 模块化 Vue 需要哪些 NPM 依赖项?

jestjs - Apollo Boost MockedProvider 在查询中使用片段时返回空对象

GraphQL:返回 null 与抛出错误

python - 无法使用请求抓取 graphql 页面

javascript - 全类事件发生故障

javascript - angularJS 模板,正确使用指令或替代方案?

javascript - React 初始输入值为空且未采用状态或 prop 值