如何在aws-amplify中对列表查询中的字段进行排序?

Gan*_*esh 6 aws-appsync aws-amplify

如何对放大graphql api中的字段进行排序?我正在尝试在获取模型列表的同时对字段进行排序。

例如:在 listOrder 查询中对 createdDate 进行排序。

请问有什么帮助吗?

Yik*_*han 10

@key 指令可能是您想要的。

假设您OrdercreatedAt时间戳列出特定客户的 s ,并假设这是您的架构:

type Order @model @key(fields: ["customerEmail", "createdAt"]) {
    customerEmail: String!
    createdAt: String!
    orderId: ID!
}
Run Code Online (Sandbox Code Playgroud)

这将创建一个主索引,其散列键为customerEmail,其排序键createdAt由 AppSync 解析器代表您管理。这将允许您运行此查询:

query ListOrdersForJack {
  listOrders(customerEmail:"jack@gmail.com") {
    items {
      orderId
      customerEmail
      createdAt
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

为什么没有显式sort关键字?排序将自动发生,因为 @key 指定该字段createdAt应用作排序键。您可以参考https://github.com/aws-amplify/amplify-cli/issues/1502进行类似的讨论。