From fcb81bded1c48226b840bab46db3a602785fb9df Mon Sep 17 00:00:00 2001 From: "daigo.tsuchiya" Date: Mon, 11 Nov 2024 20:33:32 +0900 Subject: [PATCH] fix(graphql-relational-transformer): support non string type in sort key(#2985) --- ...-graphql-has-many-references-transformer.test.ts.snap | 2 +- .../src/resolver/ddb-generator.ts | 8 ++++---- .../src/resolver/ddb-references-generator.ts | 9 +++++---- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/packages/amplify-graphql-relational-transformer/src/__tests__/__snapshots__/amplify-graphql-has-many-references-transformer.test.ts.snap b/packages/amplify-graphql-relational-transformer/src/__tests__/__snapshots__/amplify-graphql-has-many-references-transformer.test.ts.snap index b3f817bb58..51020ba221 100644 --- a/packages/amplify-graphql-relational-transformer/src/__tests__/__snapshots__/amplify-graphql-has-many-references-transformer.test.ts.snap +++ b/packages/amplify-graphql-relational-transformer/src/__tests__/__snapshots__/amplify-graphql-has-many-references-transformer.test.ts.snap @@ -1367,7 +1367,7 @@ exports[`has many references with partition key + sort key 1`] = ` }, \\"expressionValues\\": { \\":partitionKey\\": $util.dynamodb.toDynamoDB($partitionKeyValue), - \\":sortKey\\": $util.dynamodb.toDynamoDB(\\"\${sortKeyValue0}\\") + \\":sortKey\\": $util.dynamodb.toDynamoDB($sortKeyValue0) } } ) #set( $args = $util.defaultIfNull($ctx.stash.transformedArgs, $ctx.args) ) diff --git a/packages/amplify-graphql-relational-transformer/src/resolver/ddb-generator.ts b/packages/amplify-graphql-relational-transformer/src/resolver/ddb-generator.ts index 6f867a2ddc..94bce81f36 100644 --- a/packages/amplify-graphql-relational-transformer/src/resolver/ddb-generator.ts +++ b/packages/amplify-graphql-relational-transformer/src/resolver/ddb-generator.ts @@ -51,14 +51,14 @@ const PARTITION_KEY_VALUE = 'partitionKeyValue'; export class DDBRelationalResolverGenerator extends RelationalResolverGenerator { makeExpression = (keySchema: any[], connectionAttributes: string[]): ObjectNode => { if (keySchema[1] && connectionAttributes[1]) { - let condensedSortKeyValue; + let condensedSortKeyValue = `$${SORT_KEY_VALUE}0`; if (connectionAttributes.length > 2) { const rangeKeyFields = connectionAttributes.slice(1); - condensedSortKeyValue = rangeKeyFields + condensedSortKeyValue = `"${rangeKeyFields .map((keyField, idx) => `\${${SORT_KEY_VALUE}${idx}}`) - .join(ModelResourceIDs.ModelCompositeKeySeparator()); + .join(ModelResourceIDs.ModelCompositeKeySeparator())}"`; } return obj({ @@ -69,7 +69,7 @@ export class DDBRelationalResolverGenerator extends RelationalResolverGenerator }), expressionValues: obj({ ':partitionKey': ref(`util.dynamodb.toDynamoDB($${PARTITION_KEY_VALUE})`), - ':sortKey': ref(`util.dynamodb.toDynamoDB(${condensedSortKeyValue ? `"${condensedSortKeyValue}"` : `$${SORT_KEY_VALUE}0`})`), + ':sortKey': ref(`util.dynamodb.toDynamoDB(${condensedSortKeyValue})`), }), }); } diff --git a/packages/amplify-graphql-relational-transformer/src/resolver/ddb-references-generator.ts b/packages/amplify-graphql-relational-transformer/src/resolver/ddb-references-generator.ts index 4bd850322a..cc365f359c 100644 --- a/packages/amplify-graphql-relational-transformer/src/resolver/ddb-references-generator.ts +++ b/packages/amplify-graphql-relational-transformer/src/resolver/ddb-references-generator.ts @@ -57,9 +57,10 @@ export class DDBRelationalReferencesResolverGenerator extends DDBRelationalResol // #set( $sortKeyValue1 = // These variables are then used in the query object as // ":sortKey": $util.dynamodb.toDynamoDB("${sortKeyValue0}#${sortKeyValue1}"") - const condensedSortKeyValue = rangeKeyFields - .map((key, idx) => `\${${SORT_KEY_VALUE}${idx}}`) - .join(ModelResourceIDs.ModelCompositeKeySeparator()); + const condensedSortKeyValue = + rangeKeyFields.length === 1 + ? `$${SORT_KEY_VALUE}0` + : `"${rangeKeyFields.map((key, idx) => `\${${SORT_KEY_VALUE}${idx}}`).join(ModelResourceIDs.ModelCompositeKeySeparator())}"`; return obj({ expression: str('#partitionKey = :partitionKey AND #sortKey = :sortKey'), @@ -69,7 +70,7 @@ export class DDBRelationalReferencesResolverGenerator extends DDBRelationalResol }), expressionValues: obj({ ':partitionKey': ref(`util.dynamodb.toDynamoDB($${PARTITION_KEY_VALUE})`), - ':sortKey': ref(`util.dynamodb.toDynamoDB("${condensedSortKeyValue}")`), + ':sortKey': ref(`util.dynamodb.toDynamoDB(${condensedSortKeyValue})`), }), }); }