简体   繁体   English

Graphql 通用突变

[英]Graphql generic mutation

I have a set of graphql types that are similar enough that can be edited generically.我有一组足够相似的 graphql 类型,可以进行一般编辑。 I have written the code to display them and mutate on the client side yet now am having problems with writing the mutation query that would work.我已经编写了代码来显示它们并在客户端进行变异,但现在在编写可行的变异查询时遇到问题。 Current implementation:当前实施:

 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
      }
    })
  }

Not also it does not work now (because it does not want to interpolate the fields properly) but also I feel like I am doing something wrong.不仅它现在不起作用(因为它不想正确插入字段)而且我觉得我做错了什么。

I have made it work by:我通过以下方式使其工作:

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

Looks horrid but works.看起来很可怕,但很有效。 I hope there is a more "graphql" way to do this.我希望有一种更“graphql”的方式来做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM