【问题标题】:How to update dynamically of multiple rows in one table using Graphql如何使用 Graphql 动态更新一个表中的多行
【发布时间】:2020-01-09 05:41:39
【问题描述】:

我是 graphql hasura 的新手,我遇到了如何更新一个表中的多行的问题,因为通常我每个表只更新一行。

我想以动态方式更新多行

例如,我有现有的行数据。

id: 1, name: 'rosie', age: 12
id: 2, name: 'jane', age: 20
id: 3, name: 'rafaela', age: 25

我只想编辑 2 行。

id: 1, name: 'rosie update', age: 12
id: 2, name: 'jane update', age: 21

我有一组用户。 可以更新 1 个或多个用户。

const users = [
{id: 1, name: 'rosie update', age: 12}.
{id: 2, name: 'jane update', age: 21}
];

mutation update_article {
  update_user(
    where: {id: {_eq: users[0].id}},
    _set: {
      name: users[0].name,
      age: users[0].age
   }
  ) {
    affected_rows
  }
}

预期的输出应该是:

希望以动态方式而不是静态方式获得突变代码。

id: 1, name: 'rosie update', age: 12
id: 2, name: 'jane update', age: 21
id: 3, name: 'rafaela', age: 25

【问题讨论】:

  • 我也有同样的问题@rj.learn 你找到解决方案了吗,请分享

标签: graphql hasura


【解决方案1】:

阅读文档mutation,您可以使用insert 而不是update,它可以处理多个管理冲突行为的对象。

在您的示例中,可以通过以下方式完成突变:

mutation update_article {
  insert_user(objects: [
         {id: 1, name: "rosie update", age: 12},
         {id: 2, name: "jane update", age: 21}
      ],
      on_conflict: {
        constraint: user_id_key,
        update_columns: [name, age]
      }
  ) {
    affected_rows
  }
}

由于id 值已经存在,它将更新字段nameage

【讨论】:

  • 这仅在用户具有 id-insert 权限(她不应该)时才有效。处理主键是数据库的工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-31
  • 2020-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-08
相关资源
最近更新 更多