Reputation: 620
I want to sort $ctx.result.items
and reponse the sortedResult
I don't want to manually write Velocity Template Language
to sort $ctx.result.items
in Response Mapping
. Is there any better approach to response the sortedResult in AWS AppSync ?
Upvotes: 4
Views: 5691
Reputation: 2748
( if you found a solution I hope this will help someone else )
It depends on how you designed GSI or LSI to your DynamoDB table.
As stated here "DynamoDB builds an unordered hash index on the hash primary key attribute, and a sorted range index on the range primary key attribute."
Here hash index is same as partition key, and range index is same as sort key (old and new terms).
Similar text is stated here - "All items with the same partition key value are stored together, in sorted order by sort key value."
So if you added a GSI or LSI to your DynamoDB table in a way stated above (e.g. all your Products IDs are hash / partition keys and creation times are range / sort keys and you need to sort Products by creation time) you can use something similar to example defined in this page of StackOverflow.
Upvotes: 2
Reputation: 1780
What type of sorting are you looking to do? If it's ascending/descending using a DynamoDB resolver then you can set that on the ScanIndexForward
argument for this on the request template: https://docs.aws.amazon.com/appsync/latest/devguide/resolver-mapping-template-reference-dynamodb.html
Upvotes: 3