作者:丹洋2012_757 | 来源:互联网 | 2022-12-02 13:20
我正在尝试创建一种结构,该结构列出具有降序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
}
为了使其正常工作,我该如何在(即)上添加全球二级索引(GSI)?postId
postId-index
我应该在定义时在其上添加排序键lastChangeTime
吗?还是lastChangeTime
字段需要对自己的单独索引进行排序?
1> Ula..:
这很容易。您可以通过两种不同的方式来做到这一点,也可以同时使用两种方式来获得更好的灵活性。(如果您已经解决了该问题,希望对您有所帮助)。
这样,您可以使用查询参数动态设置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表的结构将类似于下面显示的结构(来自此AWS网页)。
(在您的情况下,UserId将为PostId,而GameTitle将为CommentID)
这样,因为所有注释都将彼此相邻记录(在同一PostId下),因此AppSync响应时间将更快。
在AppSync文档页面中,他们还使用了以下示例:
正如@Lisa M Shon所提到的,您可以在CommentTable上启动GSI,其中PostId是分区键,而addTime是排序键。如果要使用下面提供的解析器,则称其为“ postID-addedTime-index”。确保您在GSI中的addedTime上选择了“ Number”。
然后,您可以在架构中定义以下类型:
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 {
id: ID!
postId: String!
## rest of your type definition
}