AppSync查询sortField:“ ceatedAt”设置为降序

时间:2019-06-03 00:56:44

标签: amazon-web-services graphql aws-appsync

在下面的这个schema.graphql中,当我查询userPosts时,它将从最早的帖子返回最新的10个帖子。

type User @model {
  id: String!
  posts: [Post] @connection(name: "UserPosts", sortField: "createdAt")
}

我如何将此订单设置为DESC,以便它返回最新的10条帖子而不是最早的帖子?

1 个答案:

答案 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