amazon-dynamodb - AppSync:嵌套类型解析器
问题描述
我尝试包含在以下 graphql 模式中定义的嵌套类型:
type User {
id: String!
posts: [Post]
}
type Post {
id: String!
}
type Query {
getUser(id: String!): User
getPost(id: String!): Post
}
如您所见,用户有多个帖子。我将 AppSync 与相邻列表 Dynamodb 表(包含用户和发布相关行)作为数据源。在 AppSync 中,我必须使用请求映射模板,但在阅读文档后,我不明白嵌套类型是如何解析的?
我想在查询getUser
Post 解析器时应该使用 User_id 调用。如果是这样,我如何访问帖子解析器中的父 ID?这是${context.source}
到位的地方吗?
由于getPost
查询解析器与由 getUser Post 子调用的 Post 解析器相同,我是否必须将一些逻辑与解析器的请求模板集成来处理这两种情况?
一个例子真的很有帮助!
解决方案
您还必须为 User.posts 编写解析器。当你调用Query.getUser
它时,它的解析器将被调用,然后如果你有一个 User.posts 的解析器,它将被调用,并使用${context.source}
.
不幸的是,我没有一个干净的例子,但如果你使用 CloudFormation,你最终会得到两个解析器,有点像这样:
UserResolver:
Type: "AWS::AppSync::Resolver"
DependsOn: Schema
Properties:
ApiId: !Ref YourApiId
TypeName: Query
FieldName: getUser
DataSourceName: !Ref YourDataSource
RequestMappingTemplate: # you already have this
ResponseMappingTemplate: ...
UserPostsResolver:
Type: "AWS::AppSync::Resolver"
DependsOn: Schema
Properties:
ApiId: !Ref YourApiId
TypeName: User
FieldName: posts
DataSourceName: !Ref YourDataSource
RequestMappingTemplate: |
# use context.source.id here to reference the user id
ResponseMappingTemplate: "$util.toJson($ctx.result.items)"
差不多就是这样。您走在正确的轨道上,但是从字段到解析器的映射需要比您想象的更明确。
推荐阅读
- python - 找出 1000 以下所有 3 或 5 的倍数之和。(我的代码有什么问题)
- sql - 修改要过滤的约束
- r - 过滤具有两个不同标准的个人?
- javascript - 是否为每个滚动事件在变量内存中重新创建?
- github - GitHub API 速率限制是否适用于 GitHub 操作?
- html - 从移动设备切换到停止 html
- node.js - Node.js Mocha 单元测试不改变对象值
- node.js - 使用 api 更改 Twitter 用户名
- r - 使用 lightgbm Tweedie 目标将 SHAP 值从原始单位转换为原生单位?
- microsoft-graph-api - Microsoft Graph API 调用 getOffice365ActiveUserDetail 失败并出现 HTTP 403