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

查看:401
本文介绍了如何使用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来创建Records表,然后再次单击来创建Users表,都在DynamoDB中.这还将自动向我的架构添加变异,订阅,输入类型和更多类型,并为我创建解析器.

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表(用户"和记录"),因此在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天全站免登陆