gpt4 book ai didi

amazon-dynamodb - AppSync:嵌套类型解析器

转载 作者:行者123 更新时间:2023-12-04 00:58:02 28 4
gpt4 key购买 nike

我尝试包括在以下graphql模式中定义的嵌套类型:

type User {
id: String!
posts: [Post]
}

type Post {
id: String!
}

type Query {
getUser(id: String!): User
getPost(id: String!): Post
}


如您所见,一个用户有多个帖子。我正在将AppSync与 Adjacent List Dynamodb Table(同时包含User和Post相关行)一起用作数据源。在AppSync中,我必须使用请求映射模板,但是在阅读文档后,我还不了解如何解析嵌套类型?

我可以想象在查询 getUser时应使用User_id调用Post解析器。如果是这样,我如何在后解析器中访问父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)"


就是这样。您处在正确的轨道上,但是从字段到解析器的映射需要比您想象的更加明确。

关于amazon-dynamodb - AppSync:嵌套类型解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51342523/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com