我将AWS Appsync与DynamoDB用作数据源。我有2张桌子,一张是“照片”,另一张是“喜欢”。在Appsync解析器中,我只想返回喜欢次数超过5张的照片。如何在Appsync中实现此目标
架构
type Photo {
id: ID!
likes: [LikedPhoto]
}
type LikedPhoto {
id: ID!
username: String!
photoId: String!
}
查询
type Query {
listPhotos(filter: PhotoFilterInput, limit: Int, nextToken: String): PhotoConnection
}
照片解析器
数据来源:PhotoTable
{
"version": "2017-02-28",
"operation": "Scan",
"filter": #if($context.args.filter) $util.transform.toDynamoDBFilterExpression($ctx.args.filter) #else null #end,
"limit": $util.defaultIfNull($ctx.args.limit, 20),
"nextToken": $util.toJson($util.defaultIfNullOrEmpty($ctx.args.nextToken, null)),
}
喜欢解析器
数据来源:LikesTable
{
"version": "2017-02-28",
"operation": "Query",
"index": "photoId-index",
"query": {
"expression": "photoId = :photoId",
"expressionValues": {
":photoId": {
"S": "$context.source.id"
}
}
}
}
我该如何为喜欢或照片编写解析器,以仅显示具有超过5个喜欢的照片。
答案 0 :(得分:0)
如何将document-based
这样的架构设计为仅包含PhotoTable
。
因此,您可以使用totalLike
轻松过滤照片。
type Photo {
id: ID!
likedUsername: [String]
totalLike: Int
}
// QUERY RESOLVER
{
"version" : "2017-02-28",
"operation" : "Scan",
"filter" : {
"expression": "totalLike > :totalLike",
"expressionValues": {
":totalLike": {
"N": 5
}
}
}
}