Amplify GraphQL order by CreatedAt

Viewed 32

My Report @model is below. I am trying to retrieve the latest items back by 'CreatedAt' date. I have 15 records in Report table. If I execute my request and ask for all 15 records (by 'ASC', for instance), then I get the order by date correctly (latest records -> earliest records). However, if i limit the number of records to be retrieved at any one time, e.g 5, then what happens is, the order of the records by date is correct, but instead of delivering the latest (the 5 most currently created records), I receive the earliest 5 records back (but in the correct 'ASC' order). Please can someone advise? aws documentation

type Report @model @auth(rules: [
{ allow: owner, operations: [create, read] },
{ allow: private, operations: [read] }
])
{
  id: ID!
  author: String! @index(name: "byAuthor", queryField: "reportByAuthor", sortKeyFields: ["createdAt"])
  submissionID: ID! @index(name: "bySubmissionID", queryField: "reportBySubmissionID")
  submission: Submission @hasOne(fields: ["submissionID"])
  result: String!
  createdAt: String!
  type: String! @index(name: "reportByDate", queryField: "reportByDate", sortKeyFields: ["createdAt"])
  customComment: String
  
}

Get Reports by 'ASC' orderbut limit to 5 records at a time

const result = await API.graphql({
            query: reportByDate,
            variables: {
                type: SubmitType.REPORT,
                sortDirection: 'ASC,
                limit: 5,
                nextToken: nxtToken
            }
        });
0 Answers
Related