type Test @model @key(fields: ["id", "createdAt"]) {
id: ID!
name: String
createdAt: String!
}
此模型创建了查询:
getTest(createdAt: String!id: ID!): Test
listTests(
createdAt: ModelStringKeyConditionInput
filter: ModelTestFilterInput
id: ID
limit: Int
nextToken: String
sortDirection: ModelSortDirection
): ModelTestConnection
该方案应如何请求按日期排序的列表?
答案 0 :(得分:1)
当@key指定多个字段时,第一个字段用作HASH键,随后的字段用作SORT键。
https://aws-amplify.github.io/docs/cli-toolchain/graphql#key
可以列出具有相同HASH键的已排序项目。例如,列出用户的所有评论:
type Comment
@model
@key(fields: ["userId", "createdAt"]) {
userId: ID!
createdAt: String!
text: String
}
假设您使用的是AWS Amplify的GraphQL客户端,这将按用户列出所有注释,并按最新的顺序排列:
import { API, graphqlOperation } from 'aws-amplify';
import { listComments } from '@/graphql/queries';
export default async function listCommentsForUser(userId) {
const queryParams = {
userId,
sortDirection: 'DESC',
};
const operation = graphqlOperation(listComments, queryParams);
return API.graphql(operation);
}
要列出自指定日期以来的评论(最新的优先),请更改查询参数以包括范围查询:
const queryParams = {
userId,
sortDirection: 'DESC',
createdAt: { gt: timestamp },
};