如何使用 GraphQL 放置/更新嵌套数据? [英] How to PUT / UPDATE nested data with GraphQL?

查看:11
本文介绍了如何使用 GraphQL 放置/更新嵌套数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 AWS AppSync 尝试我的第一个 GraphQL 后端.我只是想弄清楚如何使用一对多关联.我希望收到许多相关对象作为子项列表,并且能够在创建新用户时写入其中一些子项.

I'm attempting my first GraphQL backend using AWS AppSync. I'm simply trying to figure out how to use one-to-many associations. I expect to receive the many related objects as a list of children, and to be able to write some of these children when creating a new user.

type User {
    id: ID!
    name: String!
    records: [Records!]!
}
type Records {
    id: ID!
    userId: ID!
    title: String!
    ... etc ...
}

使用 AppSync 界面,我单击 Create Resources 一次以在 DynamoDB 中创建 Records 表并再次创建 Users 表.这还会自动向我的架构添加变更、订阅、输入类型和更多类型,并为我创建解析器.

Using the AppSync interface, I click on Create Resources once to make a Records table and again to make a Users table, both in DynamoDB. This also automatically adds mutations, subscriptions, input types, and more types, to my schema, and creates resolvers for me.

创建与我的 User 对象关联的 Record 对象的突变的语法是什么?创建用户时如何放置记录数据?

What is the syntax for a mutation to create Record objects associated with my User objects? How can I PUT the Record data when I create the User?

如果需要,我可以包含更多 AppSync 自动生成的架构.

If needed I can include more of the schema that AppSync is autogenerating.

推荐答案

由于您使用了两个 DynamoDB 表(Users 和 Records),您将需要在 CreateUser 突变期间进行两次 DynamoDB 调用.在单个变更中进行两次 DynamoDB 调用的一种方法是利用 DynamoDB 的 BatchPutItem 操作.

Since you are using two DynamoDB tables (Users and Records), you will need to make two DynamoDB calls during the CreateUser mutation. One way to make two DynamoDB calls in a single mutation is to utilize DynamoDB's BatchPutItem operation.

要使用 BatchPutItem,您需要修改附加到您的 CreateUser 突变的解析器.解析器负责接收您的 graphQL 请求,将其转换为 DynamoDB 操作,然后将 DynamoDB 操作的结果转换为 graphQL 响应.解析器有两个组件:请求映射模板和响应映射模板.

To utilize BatchPutItem, you will need to modify the resolver which is attached to your CreateUser mutation. The resolver is responsible for taking your graphQL request, converting it into a DynamoDB operation, and then converting the results of the DynamoDB operation into a graphQL response. The resolvers have two components: a request mapping template, and a response mapping template.

请求映射模板将负责获取变异参数并将它们转换为 DynamoDB BatchPutItem 请求.

The request mapping template will be responsible for taking mutation arguments and converting them into a DynamoDB BatchPutItem request.

解析器的响应映射模板将负责将 DynamoDB BatchPutItem 操作的结果转换为您的变更的返回类型/结构.

The resolver's response mapping template will be responsible for converting the result of the DynamoDB BatchPutItem operation into your mutation's return type/structure.

以下是有关如何在解析器中使用多表 BatchPutItem 的教程:https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-dynamodb-batch.html

Here is a tutorial on how to utilize multi-table BatchPutItem in a resolver: https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-dynamodb-batch.html

以下是使用解析器所需模板语言的编程指南:https://docs.aws.amazon.com/appsync/latest/devguide/resolver-mapping-template-reference-programming-guide.html

Here is a programming guide for using the Template language required for the resolvers: https://docs.aws.amazon.com/appsync/latest/devguide/resolver-mapping-template-reference-programming-guide.html

这篇关于如何使用 GraphQL 放置/更新嵌套数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆