From 12c1afa80ca7d55337cf492c4822d4ab0f31565f Mon Sep 17 00:00:00 2001 From: Christopher Sundersingh <83315412+sundersc@users.noreply.github.com> Date: Thu, 21 Oct 2021 08:28:52 -0700 Subject: [PATCH] fix(graphql): searchable default sort direction (#8481) * fix(graphql): searchable default sort direction * update snapshots --- .../amplify-graphql-searchable-transformer.tests.ts.snap | 6 +++++- .../src/generate-resolver-vtl.ts | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/amplify-graphql-searchable-transformer/src/__tests__/__snapshots__/amplify-graphql-searchable-transformer.tests.ts.snap b/packages/amplify-graphql-searchable-transformer/src/__tests__/__snapshots__/amplify-graphql-searchable-transformer.tests.ts.snap index 05ba2103ed2..c434c286e47 100644 --- a/packages/amplify-graphql-searchable-transformer/src/__tests__/__snapshots__/amplify-graphql-searchable-transformer.tests.ts.snap +++ b/packages/amplify-graphql-searchable-transformer/src/__tests__/__snapshots__/amplify-graphql-searchable-transformer.tests.ts.snap @@ -1046,7 +1046,11 @@ $util.toJson($ListRequest) #set( $sortField = $util.toJson(\\"\${sortItem.field}.keyword\\") ) #end #end - #set( $sortDirection = $util.toJson({\\"order\\": $sortItem.direction}) ) + #if( $util.isNullOrEmpty($sortItem.direction) ) + #set( $sortDirection = $util.toJson({\\"order\\": \\"desc\\"}) ) + #else + #set( $sortDirection = $util.toJson({\\"order\\": $sortItem.direction}) ) + #end $util.qr($sortValues.add(\\"{$sortField: $sortDirection}\\")) #end #end diff --git a/packages/amplify-graphql-searchable-transformer/src/generate-resolver-vtl.ts b/packages/amplify-graphql-searchable-transformer/src/generate-resolver-vtl.ts index 21520eec474..e7244aed2d1 100644 --- a/packages/amplify-graphql-searchable-transformer/src/generate-resolver-vtl.ts +++ b/packages/amplify-graphql-searchable-transformer/src/generate-resolver-vtl.ts @@ -57,7 +57,11 @@ export function requestTemplate(primaryKey: string, nonKeywordFields: Expression set(ref('sortField'), ref('util.toJson("${sortItem.field}.keyword")')), ), ), - set(ref('sortDirection'), ref('util.toJson({"order": $sortItem.direction})')), + ifElse( + ref('util.isNullOrEmpty($sortItem.direction)'), + set(ref('sortDirection'), ref('util.toJson({"order": "desc"})')), + set(ref('sortDirection'), ref('util.toJson({"order": $sortItem.direction})')), + ), qref('$sortValues.add("{$sortField: $sortDirection}")'), ]), ),