AWS AppSync 响应排序结果?

AWS AppSync response sorted result?

我想排序 $ctx.result.items 并响应 sortedResult
我不想手动编写 Velocity Template Language 来排序 $ctx.result.items in Response Mapping。有没有更好的方法来响应 AWS AppSync 中的 sortedResult?

您想进行哪种类型的排序?如果它 ascending/descending 使用 DynamoDB 解析器,那么您可以在请求模板的 ScanIndexForward 参数上设置它:https://docs.aws.amazon.com/appsync/latest/devguide/resolver-mapping-template-reference-dynamodb.html

(如果您找到了解决方案,我希望这会对其他人有所帮助)

这取决于您如何为 DynamoDB 设计 GSI 或 LSI table。

如所述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 - "All items with the same partition key value are stored together, in sorted order by sort key value."

因此,如果您以上述方式将 GSI 或 LSI 添加到 DynamoDB table(例如,您所有的产品 ID 都是哈希/分区键,创建时间是范围/排序键,您需要排序按创建时间划分的产品)您可以使用类似于 .

的内容