When @key specifies multiple fields, the first field is used as the HASH key and subsequent fields as the SORT key.
https://aws-amplify.github.io/docs/cli-toolchain/graphql#key
It is possible to list sorted items which have the same HASH key. For example, listing all comments by a user:
type Comment
@model
@key(fields: ["userId", "createdAt"]) {
userId: ID!
createdAt: String!
text: String
}
Assuming you're using AWS Amplify's GraphQL client, this will list all comments by a user, sorted newest first:
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);
}
To list comments since a specified date, newest first, change the query params to include a range query:
const queryParams = {
userId,
sortDirection: 'DESC',
createdAt: { gt: timestamp },
};