AWS Amplify Datastore GraphQL DyanmoDB API limit not working in query

Viewed 23

I am using AWS Amplify for a project. Within it, I'm using Datastore and GraphQL. The data is stored in DynamoDB. I store the statistics of each user in a table. Each time the user does something useful in the app, a new record is added in the table with the latest statistic. I want to show only the latest statistic in my interface. For this I'm using the following code.

 var userStats = await DataStore.query(
   UserStats, 
   (c) => c.userId('eq', userid),
   {sort: (s) => s.altCreatedAt(SortDirection.DESC)},
   {limit: 1}
  );

This query is fetching results from the table for the correct userid, it is sorting it correctly, but the {limit: 1} part of it is not working. I get everything, not just the last record. Then, in subsequent code, I have to take the first item in the array. I feel that is inefficient. It would be better if the query working right and I get just the latest statistic. How can I solve this? Is there a better way to handle this problem?

0 Answers
Related