【发布时间】:2019-06-07 04:59:51
【问题描述】:
如何在 amplify graphql api 中对字段进行排序?我正在尝试在获取模型列表时对字段进行排序。
例如:在 listOrder 查询中对 createdDate 进行排序。
有什么帮助吗?
【问题讨论】:
如何在 amplify graphql api 中对字段进行排序?我正在尝试在获取模型列表时对字段进行排序。
例如:在 listOrder 查询中对 createdDate 进行排序。
有什么帮助吗?
【问题讨论】:
@key 指令可能是你想要的。
假设您通过createdAt 时间戳列出特定客户的Orders,并假设这是您的架构:
type Order @model @key(fields: ["customerEmail", "createdAt"]) {
customerEmail: String!
createdAt: String!
orderId: ID!
}
这将创建一个主索引,其哈希键为 customerEmail,排序键为 createdAt,由 AppSync 解析器代表您管理。这将允许您运行此查询:
query ListOrdersForJack {
listOrders(customerEmail:"jack@gmail.com") {
items {
orderId
customerEmail
createdAt
}
}
}
为什么没有明确的sort 关键字?排序将自动发生,因为@key 指定字段createdAt 应用作排序键。您可以参考https://github.com/aws-amplify/amplify-cli/issues/1502 进行类似讨论。
【讨论】:
Schema Errors: Unknown directive "key". GraphQL request (5:1) 4: ) 5: @key(fields: ["id", "createdAt"])
2021 年更新:我尝试了 Yik 提供的解决方案(Amplify 中的某些内容自动填充 createdDate 作为 sortKey 提供),但似乎在 AWS 中排序Amplify 未按预期工作。
为了节省其他人的时间,我使用以下模型写了这篇较长的帖子,该模型处理 1 个拥有 M任何通知的用户。
我使用了“aws-amplify”:“^4.3.1”和“aws-amplify-react-native”:“^5.0.3”。
我使用了下面的架构(打算是一对多的关系):
type User
@model {
id: ID!
username: String
lastname: String
firstname: String
description: String
avatar: String
externalHandles: [ExternalHandle]
followerCount: Int
followingCount: Int
verifiedEmail: String
createdDate: AWSTimestamp
enableMessageRequests: Boolean
--> notifications: [UserNotification] @connection(keyName: "byUser", fields: ["id"])
}
type UserNotification
@model
@key(name: "byUser", fields: ["userID", "createdAt"])
@auth(rules: [{allow: public}, {allow: public}]) {
id: ID!
text: String
---> userID: ID!
---> author: User! @connection(fields: ["userID"])
clubID: ID
club: Club @connection(fields: ["clubID"])
type: NotificationsType
seen: Boolean
time: AWSTimestamp
createdAt: String!
}
然后我运行“放大模拟 api”来生成查询并在 GraphiQ Web 控制台中查看它们(+ 我添加了一些虚拟数据)。
现在,如果您想列出来自用户(“所有者”对象)的通知,您可以指定排序方向。
但是,如果您尝试直接列出给定用户的通知(例如,使用 userID 作为过滤器而不是使用父对象),缺少排序方向 .
正如您在右侧看到的,结果未自动排序:
为了解决这个问题,我需要更新架构,并在其中添加了 queryField: "userNotificationsByDate",这将创建一个新的单独查询。请参阅下面的更新模型:
type UserNotification
@model @key(name: "byUser", fields: ["userID", "createdAt"], queryField: "userNotificationsByDate")
@key(name: "byNotifications", fields: ["userID", "createdAt"])
现在您将获得排序结果 - 但您仍然必须指定用户ID / 或您在@key 中定义的主键值指示。 @Key 指令/注解使用 ["primaryKey", "sortKey"] 格式。 如果您将 primaryKey 值留空,则查询将失败。使用这个新生成的查询时,请查看以下结果:
最后,如果你例如想要列出通知(不知道它们属于哪个用户),您需要使用 在所有持久对象中都相同的主键值来定义 @key。查看示例模型:
type UserNotification
@model
@key(name: "justByDate", fields: ["dummyType", "createdAt"])
@auth(rules: [{allow: public}, {allow: public}]) {
id: ID!
createdAt: String!
dummyType: String!
...
然后您需要创建这样的通知(使用相同的主键):
mutation {
createUserNotification(input: {
userID: "d69f62fe21b299",
dummyType: "ShouldBeSameForAllRooms"}) {
id
}
}
注意:除了字符串之外,您还可以在架构中为这个虚拟主键使用“枚举”。
如果你会使用枚举,比如
enum NotificationsType {
START_MEETING
JOINED_ROOM
}
type UserNotification @model
@key(name: "byUser", fields: ["userID", "createdAt"], queryField: "userNotificationsForSpecifiedUserByDate")
@key(name: "byUser3", fields: ["type", "createdAt"], queryField: "userNotificationsByDate")
@auth(rules: [{allow: public}, {allow: public}]) {
id: ID!
userID: ID!
text: String
clubID: ID
author: User! @connection(fields: ["userID"])
-> type: NotificationsType!
seen: Boolean
time: AWSTimestamp
createdAt: String!
}
您可以获得属于例如一些小组(START_MEETING 或在我的示例中为 JINED_ROOM)SORTED 使用以下查询:
query MyQuery {
userNotificationsByTypeSortedBtDate(sortDirection: ASC, type: START_MEETING) {
items{
createdAt
}
}
}
查看结果:
【讨论】: