Amplify-cli: Pagination sorted by createdAt with sortDirection

Created on 14 May 2020  路  2Comments  路  Source: aws-amplify/amplify-cli

Which Category is your question related to?
API(GraphQL)

Amplify CLI Version
4.13.1

Provide additional details e.g. code snippets
I am building a chat feature and currently my schema looks like:

type Chat
  @model(subscriptions: null)
  @key(
    name: "ByClientByMentor"
    fields: ["client", "mentor"]
    queryField: "chatsByClientByMentor"
  )
 {
  id: ID!
  client: String!
  mentor: String!
  text: String
  createdAt: String
  updatedAt: String
}

And my query (React):

const limit = 1000;
const sort = "DESC";
const result: any = API.graphql(
      graphqlOperation(chatsByClientByMentor, {
      sortDirection: sort,
      limit,
      client: <clientID>
      mentor: <mentorID>
    })
);

This returns up to 1000 items but not sorted correctly so I sorted afterwards on client side.

My ideal solution would be that sortDirection sorts all items (e.g 7,000) server side by createdAt before delivering limit of 1000 then the nextToken would deliver the next 1000 etc and could be done on smaller scale with a limit of 100.

I have tried different ways, for example:

type Chat
  @model(subscriptions: null)
  @key(
    name: "ByClientByMentorByCreatedAt"
    fields: ["client", "mentor", "createdAt]
    queryField: "chatsByClientByMentorByCreatedAt"
  )
 {
  id: ID!
  client: String!
  mentor: String!
  text!: String
  createdAt!: String
  updatedAt: String
}

with a query:

const limit = 1000;
const sort = "DESC";
const result: any = API.graphql(
      graphqlOperation(chatsByClientByMentorByCreatedAt, {
      sortDirection: sort,
      limit,
      client: <clientID>
      mentorCreatedAt: { eq: 
          { mentor: <mentorID>}
         }
    })
);

Returned nothing so tried with:

mentorCreatedAt: { beginsWidth: 
  { mentor: <mentorID>
    createdAt: <beginning of date string e.g "2020">
  }
}

Returned nothing - I tried a variety of ways but nothing worked.

With composite keys, do both queries have to match eq/beginsWidth/ etc. or is something like this possible:

mentorCreatedAt: { eq:  { mentor: <mentorID>} , 
               between: { createdAt: [ <date1>, <date2> ] }
            }

I also tried to specify date ranges in the code below. It worked in grabbing items which matched both client and mentor ids but the dates did nothing. It always returned all the matches, regardless of the date range I entered.

type ChatTwo
  @model
  @key(
    name: "ByClientByMentorByCreatedAt"
    fields: ["client", "mentor", "createdAt"]
    queryField: "chatsByClientByMentorByCreatedAt"
  ) {
  id: ID!
  client: String!
  mentor: String!
  text: String
  createdAt: String
  updatedAt: String
}

Then example query in React:

    const result = API.graphql(
      graphqlOperation(chatsByClientByMentorByCreatedAt, {
        sortDirection: sort,
        limit,
        client: <clientID>,
        mentorDate: {
          between: [
            {
              mentor: <mentorID>,
              date: "2020-05-13",
            },
            {
              mentor: <mentorId>,
              date: "2020-05-14",
            },
          ],
        },
      })
    );

My questions are:

Is it possible to return 100 out of 1000 (or 1000/10,000) items in correct descending/ascending order?

if not, is there a way for the composite keys above ["client", "mentor", "createdAt"] to get only the limit sorted in ascending/descending order similar yo using sortDirection with something like ["product", "price"]?

Also, is it possible to use three keys (hash plus composite sort) to get all items (out of 7,000) matching clientID and mentorID between a range of createdAt dates?

graphql-transformer pending-close-response-required pending-response question

Most helpful comment

Hi @thenderson55

Thank you for your patience and apologies for the delay. We are on top of this and will circle back to you with updates.

All 2 comments

Hi @thenderson55

Thank you for your patience and apologies for the delay. We are on top of this and will circle back to you with updates.

Hi @thenderson55

You can try something like this. I have tried this condition with latest amplify version and it is working for me.

query MyQuery {
chatsByClientByMentorByCreatedAt(client: "1",
sortDirection: DESC, 
mentorCreatedAt: {between: [
{createdAt: "2020-05-13", mentor: "<mentorID>"}, {createdAt: "2020-05-14", mentor: "<mentorId>"}
]},
 limit: 10, 
filter: {createdAt: {between: ["2020-05-13","2020-05-14"]}}) {
    nextToken
    items {
      client
      createdAt
      id
      mentor
      text
      updatedAt
    }
  }
}

The filter condition will sort the between the createdAt dates and provide the result in sorted order

Was this page helpful?
0 / 5 - 0 ratings