使用 AWS Amplify 创建批量操作 [GraphQL、DataStore、AppSync]

发布于 2025-01-11 12:22:08 字数 1126 浏览 0 评论 0原文

我目前一直在使用 for 循环处理批处理操作,但显然,这不是最好的方法,特别是当我添加“通过 CSV 上传”选项时,这将需要 1000 多个 putItems。

我四处寻找实现此目的的最佳方法,特别是此链接: https://docs.aws.amazon.com/ appsync/latest/devguide/tutorial-dynamodb-batch.html

但是,即使按照提到的这些步骤进行操作,我也无法实现批处理操作。下面是我的“批量删除”操作的代码。

这是我的 schema.graphql 文件:

type Client @model @auth(rules: [{ allow: owner }]) {
  id: ID!
  name: String!
  company: String
  phone: String
  email: String
}

type Mutation {
  batchDelete(ids: [ID]): [Client]
}

然后我创建两个新文件。一个请求映射模板和一个响应映射模板

#set($clientsdata = [])
#foreach($item in ${ctx.args.clients})
    $util.qr($clientsdata.delete($util.dynamodb.toMapValues($item)))
#end

{
    "version" : "2018-05-29",
    "operation" : "BatchDeleteItem",
    "tables" : {
        "Clients": $utils.toJson($clientsdata)
    }
}

然后按照教程的“简单传递”响应映射模板: $util.toJson($ctx.result.data.Posts)

但是现在当我运行batchdelete命令时,我一直没有得到任何返回。 非常感谢对此的指导!

I've currently been handling batch operations with a for loop, but obviously, this is not the best approach, especially as I'm adding an 'upload by CSV' option, which will take 1000+ putItems.

I searched around for the best ways to implement this, specifically this link:
https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-dynamodb-batch.html

However, even after following those steps mentioned I'm not able to achieve a batch operation. Below is my code for a 'batch delete' operation.

Here is my schema.graphql file:

type Client @model @auth(rules: [{ allow: owner }]) {
  id: ID!
  name: String!
  company: String
  phone: String
  email: String
}

type Mutation {
  batchDelete(ids: [ID]): [Client]
}

I then create two new files. One request mapping template and one response mapping template.

#set($clientsdata = [])
#foreach($item in ${ctx.args.clients})
    $util.qr($clientsdata.delete($util.dynamodb.toMapValues($item)))
#end

{
    "version" : "2018-05-29",
    "operation" : "BatchDeleteItem",
    "tables" : {
        "Clients": $utils.toJson($clientsdata)
    }
}

and then as per the tutorial a "simple pass through" response mapping template:
$util.toJson($ctx.result.data.Posts)

However now when I run the batchdelete command, I keep getting nothing returned.
Would really appreciate guidance on this!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

筑梦 2025-01-18 12:22:08

当涉及与 Amplify 一起执行 DynamoDB 批处理操作时,请注意,架构中指定的表名称实际上在每个环境中都不同,即您的“客户端”表不会被识别为“客户端”,正如您在请求映射模板,而是每个环境在 Amplify 推送上指定的名称。
例如 Client--envName

将表的完整名称添加到您的请求和响应映射模板中。

另外,您的 foreach 语句应为:
#foreach($item in ${ctx.args.clientsdata}) 其中迭代作为参数传递给上下文对象的数组中的每个项目。

希望这有帮助。

When it comes to performing DynamoDB batch operations in tandem with Amplify, note that the table name specified in the schema is actually different per environment, i.e. your "Client" table wouldn't be recognized as "Clients" as you have stated it in the request mapping template, but rather the name it is given on Amplify push, per environment.
E.g. Client-<some alphanumeric number>-envName

Add the full name of the table to your request and response mapping templates.

Also your foreach statement should read:
#foreach($item in ${ctx.args.clientsdata}) wherein you iterate through each of the items in the array that is passed as the argument to the context object.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文