graphql - 如何使用Graphql动态更新一张表中的多行

标签 graphql hasura

我是 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 个或 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

最佳答案

阅读文档 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 .

关于graphql - 如何使用Graphql动态更新一张表中的多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57820410/

相关文章:

node.js - GraphQL 查询结果中未返回扩展

Elixir、苦艾酒 如何在与苦艾酒约会后创建用户?

javascript - 在 redux action creators 中访问 Apollo GraphQL 客户端的最佳方式?

graphql - 使用 Hasura 作为数据访问层的最佳和正确方法是什么

postgresql - 如何将 .csv 文件导入我的 Hasura PostgreSQL 数据库?

javascript - 如何将 Postgres bytea 转换为 Base64 字符串

filtering - Graphcool(Graphql): how to make exact filter in array of Id

graphql - 如何使用 apollo 服务器从变量运行 graphql 查询

swift - 如何将 Hasura/postgres uuid 与 apollo-ios 和 swift 一起使用

graphql - 为什么我无法在 graphql 中过滤深度嵌套查询?