我正在尝试创建一种结构,该结构列出以postId
顺序排序的帖子的评论。他们的lastChangeTime
降序。
以下共享模式中的模型。
type Comment {
id: ID!
postId: String!
user: String!
lastChangeTime: String
commentBody: String
}
它已经有一个支持DynamoDB表和通用CRUD解析器。
并且id
字段是表中的主键。
我计划按以下方式构建查询:
{
"version": "2017-02-28",
"operation" : "Query",
"index" : "postId-index",
"query" : {
"expression": "post = :postId",
"expressionValues" : {
":postId" : {
"S" : "${ctx.args.postId}"
}
}
},
"limit": $util.defaultIfNull($ctx.args.first, 20),
"nextToken": $util.toJson($util.defaultIfNullOrEmpty($ctx.args.after, null)),
"scanIndexForward": false
}
要使其正常运行,我应该如何在postId
(即postId-index
)上添加全球二级索引(GSI)?
在定义它时,我应该在lastChangeTime
上添加一个排序键吗?还是lastChangeTime
字段需要自己的单独索引进行排序?
答案 0 :(得分:4)
很容易。您可以通过两种不同的方式来做到这一点,也可以同时使用两种方式来获得更好的灵活性。 (如果您已经解决了该问题,希望对您有所帮助)。
通过这种方式,您可以使用查询参数动态设置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)
这样,因为所有注释都将彼此相邻记录(在相同的PostId下),因此AppSync响应时间要快得多。
在AppSync docs page中,他们还使用了以下示例:
然后,您可以在架构中定义以下类型:
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,并同时使用。
完整的架构代码在此处(展开下面的代码段):
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
}
在请求映射模板中:
#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
}
在请求映射模板中:
{
"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
}
}
}