【问题标题】:How can i perform batch update in graphQl我如何在graphQl中执行批量更新
【发布时间】:2022-07-12 23:02:16
【问题描述】:

我有一个用户架构并且有一个更新突变

mutation MyMutation {
  updateUser(input: {Order: 47, id: "981f3cb8-a369-4f0f-8d3d-28d159a3eace"}) {
    firstname
      id
  }
}

现在我必须用它们各自的订单列表更新多个 id 列表

mutation MyMutation($id: [String!] =  ["981f3cb8-a369-4f0f-8d3d-28d159a3eace","0b2cb7d2-8dd9-4b11-846c-6370d003f6f9"], $Order: [Int] = 
    [47,48]) {
  updateUser(input: {id: $id, Order: $Order}) {
    firstname
  }
}

但我在控制台中遇到错误

{
  "data": {
    "updateUser": null
  },
  "errors": [
    {
      "path": [
        "updateUser"
      ],
      "data": null,
      "errorType": "MappingTemplate",
      "errorInfo": null,
      "locations": [
        {
          "line": 2,
          "column": 3,
          "sourceName": null
        }
      ],
      "message": "Value for field '$[key][id][S]' must be text."
    }
  ]
}

【问题讨论】:

  • 您需要在您的 GraphQL 服务器架构上实现一个突变,该架构支持获取一组输入值并批量更新服务器端。 GraphQL 无法通过原子突变自行进行批处理。唯一的方法是在代码中执行此操作,但它会发出 N 个网络请求,其中 N 是您拥有的更新数量。
  • @LuckyTuvshee 我想知道如何从 GraphQl API 一次更新多条记录
  • @prachi,更新/删除是突变。是一样的。您可以发送 1 个包含所有突变的请求。

标签: graphql amazon-dynamodb aws-amplify


【解决方案1】:

选项 1:使用 For 循环

  async function update(e) {
    for (const item of array) {
      try {
          await API.graphql({
            query: createImage,
            variables: {
              input: {
                id: id,                  },
            },
            authMode: "AMAZON_COGNITO_USER_POOLS",
          });
        } catch (err) {
          console.log(err)
        }
      } 
    }
  }

选项二,创建自定义批处理解析器:

  1. https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-dynamodb-batch.html
  2. https://docs.amplify.aws/cli/graphql/examples-and-solutions/#batch-put-custom-resolver

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-11
    • 2012-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多