Amazon dynamodb 返回也具有字段解析程序的Appsync GraphQL类型
我想从AddImageSet中返回ImageSet类型。但我似乎无法做到这一点,因为我的ImageSet类型上的每个字段都有自己的解析器,它正在覆盖解析器映射模板 模式:Amazon dynamodb 返回也具有字段解析程序的Appsync GraphQL类型,amazon-dynamodb,graphql,aws-appsync,Amazon Dynamodb,Graphql,Aws Appsync,我想从AddImageSet中返回ImageSet类型。但我似乎无法做到这一点,因为我的ImageSet类型上的每个字段都有自己的解析器,它正在覆盖解析器映射模板 模式: type ImageSet { SetKey: ID SetName: String SetCreatedAt: AWSTimestamp SetDescription: String SetTags: [Tag] } type Mutation { AddImageSet(s
type ImageSet {
SetKey: ID
SetName: String
SetCreatedAt: AWSTimestamp
SetDescription: String
SetTags: [Tag]
}
type Mutation {
AddImageSet(setName: String!, setDescription: String): ImageSet
}
以下是突变:
{
"version" : "2017-02-28",
"operation" : "PutItem",
"key" : {
"PartitionKey" : { "S" : "ImageSet" },
"SortKey" : { "S" : "ImageSet-$util.autoId()" }
},
"attributeValues" : {
"Name": { "S" : "${context.arguments.setName}" },
"Description": { "S" : "${context.arguments.setDescription}" },
"CreatedAt": { "N" : $util.time.nowEpochSeconds() },
}
}
和响应映射模板
{
"SetKey" : "$context.result.SortKey",
"SetName" : "$context.result.Name",
"SetCreatedAt" : $context.result.CreatedAt,
"SetDescription" : "$context.result.Description"
}
当我运行此操作时,AddImageSet突变返回的对象将每个字段都设置为null,因为字段解析程序正在覆盖AddImageSet突变的响应映射模板
我通过创建一个重复的“ImageSetResult”类型解决了这个问题,只是没有将解析器附加到字段。如果AddImageSet突变返回该值,则一切正常。但这些似乎有些多余
有更好的方法吗?这是GraphQL中的预期行为。如果您在ImageSet.SetKey上有一个解析器,并且您的突变.AddImageSet突变返回一个类型为ImageSet的对象,则将调用选择集中列出的每个字段的解析器。子解析程序返回的值将覆盖由突变返回的值。AddImageSet 要添加更多上下文,当您在ImageSet.SetKey/SetName/SetCreatedAt解析程序中时,从突变.AddImageSet响应映射模板返回的值将是$ctx.source的值。从这些解析器中,您可以执行以下操作:
#if( $ctx.source.SetKey ) "$ctx.source.SetKey" #else .. whatever SetKey does .. #end
是否有理由在SetKey/SetName/etc上需要解析器?如果对象已由Mutation.AddImageSet或Query.AddImageSet等返回,则该类型不需要解析程序
另一张纸条。是否有理由希望使用名称ImageSet.SetName而不是仅使用ImageSet.Name将其存储在DynamoDB中?这似乎增加了很多复杂性,您可能可以避免它。正因为如此,我使用的是SetName而不仅仅是Name。我猜这是一个带有嵌套左轮手枪的AppsSync错误。此外,我在这里的设置比我最初的帖子所暗示的要复杂得多。我在DynamoDB中有一个多对多关系,类型通过嵌套查询获得其嵌套类型的数组。在某些情况下,“SetKey”是PK,在某些情况下是SK.Gotcha。是的,这个问题不应该再成为问题了。