【问题标题】:Graphql generic mutationGraphql 通用突变
【发布时间】:2020-02-18 16:13:32
【问题描述】:

我有一组足够相似的 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
      }
    })
  }

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

【问题讨论】:

    标签: javascript vue.js graphql apollo vue-apollo


    【解决方案1】:

    我已通过以下方式使其工作:

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

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

    【讨论】:

      猜你喜欢
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 2020-11-04
      • 2018-06-20
      • 2019-01-23
      • 2021-03-09
      • 1970-01-01
      • 2018-04-19
      相关资源
      最近更新 更多