在下面的这个schema.graphql中,当我查询userPosts时,它将从最早的帖子返回最新的10个帖子。
type User @model {
id: String!
posts: [Post] @connection(name: "UserPosts", sortField: "createdAt")
}
我如何将此订单设置为DESC,以便它返回最新的10条帖子而不是最早的帖子?
答案 0 :(得分:1)
使用Amplify CLI创建@model
类型时,它将使用名为listPosts
的查询生成您的架构。该查询具有多个参数,其中一个为sortDirection
,类型为ModelSortDirection
。
ModelSortDirection是enum
类型,具有以下形状:
enum ModelSortDirection {
ASC
DESC
}
您可以通过DESC
。此外,如果您在AppSync console的API模式页面上编辑posts
解析器,则可以看到如何使用此参数。它使用DynamoDB scanIndexForward对数据源返回的行进行排序。
"scanIndexForward": #if( $context.args.sortDirection )
#if( $context.args.sortDirection == "ASC" )
true
#else
false
#end