这很容易。你可以用两种不同的方式来做,或者你可以同时使用这两种方式来获得更好的灵活性。 (如果你已经解决了,我希望它会帮助别人)。
这样做,您可以使用查询参数动态设置 sortDirection。
详细代码如下。在此之前请注意这一点。
-
第一点是您的评论类型 - 您正在使用
type Comment {
id: ID!
postId: String!
## rest of your type definition
}
这不是设置链接到帖子的评论类型的最佳方式。
更好的方法是:
type Comment {
postID: ID! ## select this as Primary key in DataSource in AppSync console
commentID: String! ## select this as Sort key in DataSource in AppSync console
## rest of your type definition
}
如果您这样做,您的 DynamoDB 表将具有类似于下图所示的结构 (from this AWS webpage)。
(在您的情况下 UserId 将是 PostId 并且 GameTitle 将是 CommentID )
这样,因为所有 cmets 将彼此相邻记录(在同一个 PostId 下)AppSync 响应时间会快得多。
在AppSync docs page 他们也使用了这个例子:
- 正如@Lisa M Shon 提到的,您可以在 CommentTable 上启动 GSI,其中 PostId 是分区键, addedTime 是排序键。如果您想使用下面提供的解析器,请将其称为“postID-addedTime-index”。确保在 GSI 中的 addedTime 上选择“数字”。
然后在您的架构中,您可以定义以下类型:
type Comment {
postID: ID!
commentID: String!
content: String!
addedTime: Int!
}
type CommentConnection {
items: [Comment]
nextToken: String
}
type Post {
id: ID!
postContent: String!
addedTime: Int!
## Option 1. Gets Post details with all related Comments.
## If 'startFromTime' is provided it will fetch all Comments starting from that timestamp.
## If 'startFromTime' is not provided it will fetch all Comments.
comments(
filter: TableCommentFilterInput,
sortDirection: SortDirection,
startFromTime: Int,
limit: Int,
nextToken: String
): CommentConnection
}
type Query {
## Option 2. It will fetch Comments only for a given PostId.
## If 'startFromTime' is provided it will fetch all Comments starting from that timestamp.
## If 'startFromTime' is not provided it will fetch all Comments.
postCommentsByAddTime(
postID: String!,
startFromTime: Int!,
sortDirection: SortDirection,
filter: TableCommentFilterInput,
count: Int,
nextToken: String
): PaginatedComments
## your other queries
}
## rest of your schema definition
您可以同时使用 - 选项 1 和选项 2 并同时使用。
完整的架构代码在这里(展开下面的sn-p):
type Comment {
postID: ID!
commentID: String!
content: String!
addedTime: Int!
}
type CommentConnection {
items: [Comment]
nextToken: String
}
input CreateCommentInput {
postID: ID!
commentID: String!
content: String!
addedTime: Int!
}
input CreatePostInput {
postContent: String!
addedTime: Int!
}
input DeleteCommentInput {
postID: ID!
commentID: String!
}
input DeletePostInput {
id: ID!
}
type Mutation {
createComment(input: CreateCommentInput!): Comment
updateComment(input: UpdateCommentInput!): Comment
deleteComment(input: DeleteCommentInput!): Comment
createPost(input: CreatePostInput!): Post
updatePost(input: UpdatePostInput!): Post
deletePost(input: DeletePostInput!): Post
}
type PaginatedComments {
items: [Comment!]!
nextToken: String
}
type Post {
id: ID!
postContent: String!
addedTime: Int!
comments(
filter: TableCommentFilterInput,
sortDirection: SortDirection,
startFromTime: Int,
limit: Int,
nextToken: String
): CommentConnection
}
type PostConnection {
items: [Post]
nextToken: String
}
type Query {
getComment(postID: ID!, commentID: String!): Comment
listComments(filter: TableCommentFilterInput, limit: Int, nextToken: String): CommentConnection
getPost(id: ID!): Post
listPosts(filter: TablePostFilterInput, limit: Int, nextToken: String): PostConnection
postCommentsByAddTime(
postID: String!,
startFromTime: Int!,
sortDirection: SortDirection,
filter: TableCommentFilterInput,
count: Int,
nextToken: String
): PaginatedComments
}
enum SortDirection {
ASC
DESC
}
type Subscription {
onCreateComment(
postID: ID,
commentID: String,
content: String,
addedTime: Int
): Comment
@aws_subscribe(mutations: ["createComment"])
onUpdateComment(
postID: ID,
commentID: String,
content: String,
addedTime: Int
): Comment
@aws_subscribe(mutations: ["updateComment"])
onDeleteComment(
postID: ID,
commentID: String,
content: String,
addedTime: Int
): Comment
@aws_subscribe(mutations: ["deleteComment"])
onCreatePost(id: ID, postContent: String, addedTime: Int): Post
@aws_subscribe(mutations: ["createPost"])
onUpdatePost(id: ID, postContent: String, addedTime: Int): Post
@aws_subscribe(mutations: ["updatePost"])
onDeletePost(id: ID, postContent: String, addedTime: Int): Post
@aws_subscribe(mutations: ["deletePost"])
}
input TableBooleanFilterInput {
ne: Boolean
eq: Boolean
}
input TableCommentFilterInput {
postID: TableIDFilterInput
commentID: TableStringFilterInput
content: TableStringFilterInput
addedTime: TableIntFilterInput
}
input TableFloatFilterInput {
ne: Float
eq: Float
le: Float
lt: Float
ge: Float
gt: Float
contains: Float
notContains: Float
between: [Float]
}
input TableIDFilterInput {
ne: ID
eq: ID
le: ID
lt: ID
ge: ID
gt: ID
contains: ID
notContains: ID
between: [ID]
beginsWith: ID
}
input TableIntFilterInput {
ne: Int
eq: Int
le: Int
lt: Int
ge: Int
gt: Int
contains: Int
notContains: Int
between: [Int]
}
input TablePostFilterInput {
id: TableIDFilterInput
postContent: TableStringFilterInput
addedTime: TableIntFilterInput
}
input TableStringFilterInput {
ne: String
eq: String
le: String
lt: String
ge: String
gt: String
contains: String
notContains: String
between: [String]
beginsWith: String
}
input UpdateCommentInput {
postID: ID!
commentID: String!
content: String
addedTime: Int
}
input UpdatePostInput {
id: ID!
postContent: String
addedTime: Int
}
schema {
query: Query
mutation: Mutation
subscription: Subscription
}
- (与选项 1 相关)。在右侧面板中 AppSync 控制台的 Schema 页面上,找到 Post 并在“cmets(...): CommentConnection”对面单击“附加”,将“CommentTable”添加为源并在 VTL 中添加以下解析器代码:
在请求映射模板中:
#set( $startFromTime = $util.defaultIfNull($context.args.startFromTime, 0) )
{
"version" : "2017-02-28",
"operation" : "Query",
"index" : "postID-addedTime-index",
"query" : {
"expression": "postID = :postID and addedTime > :startFrom",
"expressionValues" : {
":postID" : { "S" : "$context.source.id" },
":startFrom" : { "N" : "$startFromTime" }
}
},
"scanIndexForward": #if( $context.args.sortDirection )
#if( $context.args.sortDirection == "ASC" )
true
#else
false
#end
#else
true
#end,
#if( ${context.arguments.count} )
,"limit": ${context.arguments.count}
#end
#if( ${context.arguments.nextToken} )
,"nextToken": "${context.arguments.nextToken}"
#end
}
在响应映射模板中:
{
"items": $utils.toJson($context.result.items)
#if( ${context.result.nextToken} )
,"nextToken": "${context.result.nextToken}"
#end
}
- (与选项 2 相关)。在右侧面板中 AppSync 控制台的 Schema 页面上找到 Query 并在 'postCommentsByAddTime(...): PaginatedComments' 对面单击 'Attach',将 CommentTable 添加为数据源并在 VTL 中添加以下解析器代码:
在请求映射模板中:
{
"version" : "2017-02-28",
"operation" : "Query",
"index" : "postID-addedTime-index",
"query" : {
"expression": "postID = :postID and addedTime > :startFrom",
"expressionValues" : {
":postID" : { "S" : "${context.arguments.postID}" },
":startFrom" : { "N" : "${context.arguments.startFromTime}" }
}
}
#if( ${context.arguments.count} )
,"limit": ${context.arguments.count}
#end
#if( ${context.arguments.nextToken} )
,"nextToken": "${context.arguments.nextToken}"
#end
}
在响应映射模板中:
{
"items": $utils.toJson($context.result.items)
#if( ${context.result.nextToken} )
,"nextToken": "${context.result.nextToken}"
#end
}
就是这样。
现在您可以使用以下所有查询:
query ListPosts {
listPosts{
items {
id
postContent
## all below arguments are nullable
comments(startFromTime: 121111112222, count: 4
## default sortDirection is ASC, you can change it this way
## sortDirection: DESC
) {
items {
postID
commentID
content
addedTime
}
}
}
}
}
query GetPost {
getPost(id: "6548e596-d1ed-4203-a32f-52cfab8c9b20") {
id
comments (
## you can also add all three or any or none of these
## sortDirection: DESC,
## startFromTime: 189283212122
## count: 5
) {
items {
postID
commentID
content
addedTime
}
}
}
}
query GetCommentsByTime {
postCommentsByAddTime(postID: "6548e596-d1ed-4203-a32f-52cfab8c9b20", startFromTime: 12423455352342, count: 2) {
items {
postID
commentID
content
addedTime
}
}
}