From 63e098f372b1c7262c312f50472a5a818a59943f Mon Sep 17 00:00:00 2001 From: Ivan Artemiev <29709626+iartemiev@users.noreply.github.com> Date: Sun, 14 Apr 2024 07:43:05 -0500 Subject: [PATCH 1/3] fix: incorrect sk arg type --- .../__tests__/internals/APIClient.test.ts | 2 +- .../__snapshots__/generateClient.test.ts.snap | 12 ++-- .../api-graphql/src/internals/APIClient.ts | 63 ++++++++++++------- 3 files changed, 47 insertions(+), 30 deletions(-) diff --git a/packages/api-graphql/__tests__/internals/APIClient.test.ts b/packages/api-graphql/__tests__/internals/APIClient.test.ts index ccf607de57f..ec73ea9ca52 100644 --- a/packages/api-graphql/__tests__/internals/APIClient.test.ts +++ b/packages/api-graphql/__tests__/internals/APIClient.test.ts @@ -563,7 +563,7 @@ describe('generateGraphQLDocument()', () => { modelOperation ); - expect(document.includes(expectedArgs)).toBe(true); + expect(document).toEqual(expect.stringContaining(expectedArgs)) } ); }); diff --git a/packages/api-graphql/__tests__/internals/__snapshots__/generateClient.test.ts.snap b/packages/api-graphql/__tests__/internals/__snapshots__/generateClient.test.ts.snap index 88afaa71b88..a4a8f6dc6f8 100644 --- a/packages/api-graphql/__tests__/internals/__snapshots__/generateClient.test.ts.snap +++ b/packages/api-graphql/__tests__/internals/__snapshots__/generateClient.test.ts.snap @@ -4092,12 +4092,12 @@ exports[`generateClient basic model operations can list() with sortDirection (AS "abortController": AbortController {}, "options": { "body": { - "query": "query ($filter: ModelThingWithCustomPkFilterInput, $sortDirection: ModelSortDirection, $cpk_cluster_key: String, $cpk_sort_key: String, $limit: Int, $nextToken: String) { + "query": "query ($cpk_cluster_key: String, $cpk_sort_key: ModelStringKeyConditionInput, $sortDirection: ModelSortDirection, $filter: ModelThingWithCustomPkFilterInput, $limit: Int, $nextToken: String) { listThingWithCustomPks( - filter: $filter - sortDirection: $sortDirection cpk_cluster_key: $cpk_cluster_key cpk_sort_key: $cpk_sort_key + sortDirection: $sortDirection + filter: $filter limit: $limit nextToken: $nextToken ) { @@ -4139,12 +4139,12 @@ exports[`generateClient basic model operations can list() with sortDirection (DE "abortController": AbortController {}, "options": { "body": { - "query": "query ($filter: ModelThingWithCustomPkFilterInput, $sortDirection: ModelSortDirection, $cpk_cluster_key: String, $cpk_sort_key: String, $limit: Int, $nextToken: String) { + "query": "query ($cpk_cluster_key: String, $cpk_sort_key: ModelStringKeyConditionInput, $sortDirection: ModelSortDirection, $filter: ModelThingWithCustomPkFilterInput, $limit: Int, $nextToken: String) { listThingWithCustomPks( - filter: $filter - sortDirection: $sortDirection cpk_cluster_key: $cpk_cluster_key cpk_sort_key: $cpk_sort_key + sortDirection: $sortDirection + filter: $filter limit: $limit nextToken: $nextToken ) { diff --git a/packages/api-graphql/src/internals/APIClient.ts b/packages/api-graphql/src/internals/APIClient.ts index 39978196139..ddfc7a28730 100644 --- a/packages/api-graphql/src/internals/APIClient.ts +++ b/packages/api-graphql/src/internals/APIClient.ts @@ -721,6 +721,44 @@ export function generateGraphQLDocument( selectionSet as ListArgs['selectionSet'], ); + // default PK args for get and list operations + // modified below for CPK + const getPkArgs = { + [primaryKeyFieldName]: `${fields[primaryKeyFieldName].type}!`, + }; + const listPkArgs = {}; + + const generateSkArgs = (op: 'get' | 'list') => { + return sortKeyFieldNames.reduce( + (acc: Record, fieldName: string) => { + const fieldType = fields[fieldName].type; + + if (op === 'get') { + acc[fieldName] = `${fieldType}!`; + } else if (op === 'list') { + acc[fieldName] = `Model${fieldType}KeyConditionInput`; + } + + return acc; + }, + {}, + ); + }; + + if (isCustomPrimaryKey) { + Object.assign(getPkArgs, { + ...generateSkArgs('get'), + }); + + Object.assign(listPkArgs, { + // PK is only included in list query field args in the generated GQL + // when explicitly specifying PK with .identifier(['fieldName']) or @primaryKey in the schema definition + [primaryKeyFieldName]: `${fields[primaryKeyFieldName].type}`, // PK is always a nullable arg for list (no `!` after the type) + ...generateSkArgs('list'), + sortDirection: 'ModelSortDirection', + }); + } + switch (modelOperation) { case 'CREATE': case 'UPDATE': @@ -736,36 +774,15 @@ export function generateGraphQLDocument( // TODO(Eslint): this this case clause correct without the break statement? // eslint-disable-next-line no-fallthrough case 'READ': - graphQLArguments ?? - (graphQLArguments = isCustomPrimaryKey - ? [primaryKeyFieldName, ...sortKeyFieldNames].reduce( - (acc: Record, fieldName) => { - acc[fieldName] = `${fields[fieldName].type}!`; - - return acc; - }, - {}, - ) - : { - [primaryKeyFieldName]: `${fields[primaryKeyFieldName].type}!`, - }); + graphQLArguments ?? (graphQLArguments = getPkArgs); graphQLSelectionSet ?? (graphQLSelectionSet = selectionSetFields); // TODO(Eslint): this this case clause correct without the break statement? // eslint-disable-next-line no-fallthrough case 'LIST': graphQLArguments ?? (graphQLArguments = { + ...listPkArgs, filter: `Model${name}FilterInput`, - ...(sortKeyFieldNames.length > 0 - ? [primaryKeyFieldName, ...sortKeyFieldNames].reduce( - (acc: Record, fieldName) => { - acc[fieldName] = `${fields[fieldName].type}`; - - return acc; - }, - { sortDirection: 'ModelSortDirection' }, - ) - : []), limit: 'Int', nextToken: 'String', }); From 954e1127578741f568b097236455f2d5d1206367 Mon Sep 17 00:00:00 2001 From: Ivan Artemiev <29709626+iartemiev@users.noreply.github.com> Date: Sun, 14 Apr 2024 12:00:43 -0500 Subject: [PATCH 2/3] bump bundle size --- packages/aws-amplify/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/aws-amplify/package.json b/packages/aws-amplify/package.json index 9afcb4f8b36..9183da1a1bf 100644 --- a/packages/aws-amplify/package.json +++ b/packages/aws-amplify/package.json @@ -335,7 +335,7 @@ "name": "[API] generateClient (AppSync)", "path": "./dist/esm/api/index.mjs", "import": "{ generateClient }", - "limit": "38.5 kB" + "limit": "39.0 kB" }, { "name": "[API] REST API handlers", From 2361247e0a5996d0b3ee686cd97ef51061a0791e Mon Sep 17 00:00:00 2001 From: Ivan Artemiev <29709626+iartemiev@users.noreply.github.com> Date: Mon, 15 Apr 2024 12:16:18 -0500 Subject: [PATCH 3/3] pr feedback --- .../__snapshots__/generateClient.test.ts.snap | 8 +++---- .../api-graphql/src/internals/APIClient.ts | 23 ++++++++++--------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/packages/api-graphql/__tests__/internals/__snapshots__/generateClient.test.ts.snap b/packages/api-graphql/__tests__/internals/__snapshots__/generateClient.test.ts.snap index a4a8f6dc6f8..7550d94648c 100644 --- a/packages/api-graphql/__tests__/internals/__snapshots__/generateClient.test.ts.snap +++ b/packages/api-graphql/__tests__/internals/__snapshots__/generateClient.test.ts.snap @@ -4092,11 +4092,11 @@ exports[`generateClient basic model operations can list() with sortDirection (AS "abortController": AbortController {}, "options": { "body": { - "query": "query ($cpk_cluster_key: String, $cpk_sort_key: ModelStringKeyConditionInput, $sortDirection: ModelSortDirection, $filter: ModelThingWithCustomPkFilterInput, $limit: Int, $nextToken: String) { + "query": "query ($cpk_cluster_key: String, $sortDirection: ModelSortDirection, $cpk_sort_key: ModelStringKeyConditionInput, $filter: ModelThingWithCustomPkFilterInput, $limit: Int, $nextToken: String) { listThingWithCustomPks( cpk_cluster_key: $cpk_cluster_key - cpk_sort_key: $cpk_sort_key sortDirection: $sortDirection + cpk_sort_key: $cpk_sort_key filter: $filter limit: $limit nextToken: $nextToken @@ -4139,11 +4139,11 @@ exports[`generateClient basic model operations can list() with sortDirection (DE "abortController": AbortController {}, "options": { "body": { - "query": "query ($cpk_cluster_key: String, $cpk_sort_key: ModelStringKeyConditionInput, $sortDirection: ModelSortDirection, $filter: ModelThingWithCustomPkFilterInput, $limit: Int, $nextToken: String) { + "query": "query ($cpk_cluster_key: String, $sortDirection: ModelSortDirection, $cpk_sort_key: ModelStringKeyConditionInput, $filter: ModelThingWithCustomPkFilterInput, $limit: Int, $nextToken: String) { listThingWithCustomPks( cpk_cluster_key: $cpk_cluster_key - cpk_sort_key: $cpk_sort_key sortDirection: $sortDirection + cpk_sort_key: $cpk_sort_key filter: $filter limit: $limit nextToken: $nextToken diff --git a/packages/api-graphql/src/internals/APIClient.ts b/packages/api-graphql/src/internals/APIClient.ts index ddfc7a28730..748425831be 100644 --- a/packages/api-graphql/src/internals/APIClient.ts +++ b/packages/api-graphql/src/internals/APIClient.ts @@ -746,17 +746,18 @@ export function generateGraphQLDocument( }; if (isCustomPrimaryKey) { - Object.assign(getPkArgs, { - ...generateSkArgs('get'), - }); - - Object.assign(listPkArgs, { - // PK is only included in list query field args in the generated GQL - // when explicitly specifying PK with .identifier(['fieldName']) or @primaryKey in the schema definition - [primaryKeyFieldName]: `${fields[primaryKeyFieldName].type}`, // PK is always a nullable arg for list (no `!` after the type) - ...generateSkArgs('list'), - sortDirection: 'ModelSortDirection', - }); + Object.assign(getPkArgs, generateSkArgs('get')); + + Object.assign( + listPkArgs, + { + // PK is only included in list query field args in the generated GQL + // when explicitly specifying PK with .identifier(['fieldName']) or @primaryKey in the schema definition + [primaryKeyFieldName]: `${fields[primaryKeyFieldName].type}`, // PK is always a nullable arg for list (no `!` after the type) + sortDirection: 'ModelSortDirection', + }, + generateSkArgs('list'), + ); } switch (modelOperation) {