diff --git a/packages/client-content/tests/cases/unit/mutation.test.ts b/packages/client-content/tests/cases/unit/mutation.test.ts index 7418e9fdc8..15bead7011 100644 --- a/packages/client-content/tests/cases/unit/mutation.test.ts +++ b/packages/client-content/tests/cases/unit/mutation.test.ts @@ -22,8 +22,8 @@ describe('mutations in trx', () => { expect(result.data.ok).toBe(true) expect(calls).toHaveLength(1) expect(calls[0].query).toMatchInlineSnapshot(` - "mutation($options_MutationTransactionOptions_0: MutationTransactionOptions, $data_AuthorCreateInput_1: AuthorCreateInput!) { - mut: transaction(options: $options_MutationTransactionOptions_0) { + "mutation($MutationTransactionOptions_0: MutationTransactionOptions, $AuthorCreateInput_1: AuthorCreateInput!) { + mut: transaction(options: $MutationTransactionOptions_0) { ok errorMessage errors { @@ -32,7 +32,7 @@ describe('mutations in trx', () => { validation { ... ValidationResult } - mut: createAuthor(data: $data_AuthorCreateInput_1) { + mut: createAuthor(data: $AuthorCreateInput_1) { ok errorMessage errors { @@ -78,11 +78,11 @@ describe('mutations in trx', () => { `) expect(calls[0].variables).toMatchInlineSnapshot(` { - "data_AuthorCreateInput_1": { + "AuthorCreateInput_1": { "email": "xx@localhost", "name": "John", }, - "options_MutationTransactionOptions_0": {}, + "MutationTransactionOptions_0": {}, } `) }) @@ -107,8 +107,8 @@ describe('mutations in trx', () => { expect(result.data?.node?.id).toBe(123) expect(calls).toHaveLength(1) expect(calls[0].query).toMatchInlineSnapshot(` - "mutation($options_MutationTransactionOptions_0: MutationTransactionOptions, $data_AuthorCreateInput_1: AuthorCreateInput!) { - mut: transaction(options: $options_MutationTransactionOptions_0) { + "mutation($MutationTransactionOptions_0: MutationTransactionOptions, $AuthorCreateInput_1: AuthorCreateInput!) { + mut: transaction(options: $MutationTransactionOptions_0) { ok errorMessage errors { @@ -117,7 +117,7 @@ describe('mutations in trx', () => { validation { ... ValidationResult } - mut: createAuthor(data: $data_AuthorCreateInput_1) { + mut: createAuthor(data: $AuthorCreateInput_1) { ok errorMessage errors { @@ -166,11 +166,11 @@ describe('mutations in trx', () => { `) expect(calls[0].variables).toMatchInlineSnapshot(` { - "data_AuthorCreateInput_1": { + "AuthorCreateInput_1": { "email": "xx@localhost", "name": "John", }, - "options_MutationTransactionOptions_0": {}, + "MutationTransactionOptions_0": {}, } `) }) @@ -193,8 +193,8 @@ describe('mutations in trx', () => { }))) expect(calls).toHaveLength(1) expect(calls[0].query).toMatchInlineSnapshot(` - "mutation($options_MutationTransactionOptions_0: MutationTransactionOptions, $by_AuthorUniqueWhere_1: AuthorUniqueWhere!, $data_AuthorUpdateInput_2: AuthorUpdateInput!) { - mut: transaction(options: $options_MutationTransactionOptions_0) { + "mutation($MutationTransactionOptions_0: MutationTransactionOptions, $AuthorUniqueWhere_1: AuthorUniqueWhere!, $AuthorUpdateInput_2: AuthorUpdateInput!) { + mut: transaction(options: $MutationTransactionOptions_0) { ok errorMessage errors { @@ -203,7 +203,7 @@ describe('mutations in trx', () => { validation { ... ValidationResult } - mut: updateAuthor(by: $by_AuthorUniqueWhere_1, data: $data_AuthorUpdateInput_2) { + mut: updateAuthor(by: $AuthorUniqueWhere_1, data: $AuthorUpdateInput_2) { ok errorMessage errors { @@ -249,14 +249,14 @@ describe('mutations in trx', () => { `) expect(calls[0].variables).toMatchInlineSnapshot(` { - "by_AuthorUniqueWhere_1": { + "AuthorUniqueWhere_1": { "id": 1, }, - "data_AuthorUpdateInput_2": { + "AuthorUpdateInput_2": { "email": "xx@localhost", "name": "John", }, - "options_MutationTransactionOptions_0": {}, + "MutationTransactionOptions_0": {}, } `) }) @@ -275,8 +275,8 @@ describe('mutations in trx', () => { }))) expect(calls).toHaveLength(1) expect(calls[0].query).toMatchInlineSnapshot(` - "mutation($options_MutationTransactionOptions_0: MutationTransactionOptions, $by_AuthorUniqueWhere_1: AuthorUniqueWhere!) { - mut: transaction(options: $options_MutationTransactionOptions_0) { + "mutation($MutationTransactionOptions_0: MutationTransactionOptions, $AuthorUniqueWhere_1: AuthorUniqueWhere!) { + mut: transaction(options: $MutationTransactionOptions_0) { ok errorMessage errors { @@ -285,7 +285,7 @@ describe('mutations in trx', () => { validation { ... ValidationResult } - mut: deleteAuthor(by: $by_AuthorUniqueWhere_1) { + mut: deleteAuthor(by: $AuthorUniqueWhere_1) { ok errorMessage errors { @@ -328,10 +328,10 @@ describe('mutations in trx', () => { `) expect(calls[0].variables).toMatchInlineSnapshot(` { - "by_AuthorUniqueWhere_1": { + "AuthorUniqueWhere_1": { "id": 1, }, - "options_MutationTransactionOptions_0": {}, + "MutationTransactionOptions_0": {}, } `) }) @@ -358,8 +358,8 @@ describe('mutations in trx', () => { }))) expect(calls).toHaveLength(1) expect(calls[0].query).toMatchInlineSnapshot(` - "mutation($options_MutationTransactionOptions_0: MutationTransactionOptions, $by_AuthorUniqueWhere_1: AuthorUniqueWhere!, $create_AuthorCreateInput_2: AuthorCreateInput!, $update_AuthorUpdateInput_3: AuthorUpdateInput!) { - mut: transaction(options: $options_MutationTransactionOptions_0) { + "mutation($MutationTransactionOptions_0: MutationTransactionOptions, $AuthorUniqueWhere_1: AuthorUniqueWhere!, $AuthorCreateInput_2: AuthorCreateInput!, $AuthorUpdateInput_3: AuthorUpdateInput!) { + mut: transaction(options: $MutationTransactionOptions_0) { ok errorMessage errors { @@ -368,7 +368,7 @@ describe('mutations in trx', () => { validation { ... ValidationResult } - mut: upsertAuthor(by: $by_AuthorUniqueWhere_1, create: $create_AuthorCreateInput_2, update: $update_AuthorUpdateInput_3) { + mut: upsertAuthor(by: $AuthorUniqueWhere_1, create: $AuthorCreateInput_2, update: $AuthorUpdateInput_3) { ok errorMessage errors { @@ -413,21 +413,21 @@ describe('mutations in trx', () => { " `) expect(calls[0].variables).toMatchInlineSnapshot(` - { - "by_AuthorUniqueWhere_1": { - "id": 1, - }, - "create_AuthorCreateInput_2": { - "email": "xx@localhost", - "name": "John", - }, - "options_MutationTransactionOptions_0": {}, - "update_AuthorUpdateInput_3": { - "email": "xx@localhost", - "name": "John", - }, - } - `) + { + "AuthorCreateInput_2": { + "email": "xx@localhost", + "name": "John", + }, + "AuthorUniqueWhere_1": { + "id": 1, + }, + "AuthorUpdateInput_3": { + "email": "xx@localhost", + "name": "John", + }, + "MutationTransactionOptions_0": {}, + } + `) }) test('multiple mutations as array', async () => { @@ -462,8 +462,8 @@ describe('mutations in trx', () => { expect(result.data[1].ok).toBe(true) expect(calls).toHaveLength(1) expect(calls[0].query).toMatchInlineSnapshot(` - "mutation($options_MutationTransactionOptions_0: MutationTransactionOptions, $data_AuthorCreateInput_1: AuthorCreateInput!, $data_AuthorCreateInput_2: AuthorCreateInput!) { - mut: transaction(options: $options_MutationTransactionOptions_0) { + "mutation($MutationTransactionOptions_0: MutationTransactionOptions, $AuthorCreateInput_1: AuthorCreateInput!, $AuthorCreateInput_2: AuthorCreateInput!) { + mut: transaction(options: $MutationTransactionOptions_0) { ok errorMessage errors { @@ -472,7 +472,7 @@ describe('mutations in trx', () => { validation { ... ValidationResult } - mut_0: createAuthor(data: $data_AuthorCreateInput_1) { + mut_0: createAuthor(data: $AuthorCreateInput_1) { ok errorMessage errors { @@ -482,7 +482,7 @@ describe('mutations in trx', () => { ... ValidationResult } } - mut_1: createAuthor(data: $data_AuthorCreateInput_2) { + mut_1: createAuthor(data: $AuthorCreateInput_2) { ok errorMessage errors { @@ -528,15 +528,15 @@ describe('mutations in trx', () => { `) expect(calls[0].variables).toMatchInlineSnapshot(` { - "data_AuthorCreateInput_1": { + "AuthorCreateInput_1": { "email": "xx@localhost", "name": "John", }, - "data_AuthorCreateInput_2": { + "AuthorCreateInput_2": { "email": "xx@localhost", "name": "John", }, - "options_MutationTransactionOptions_0": {}, + "MutationTransactionOptions_0": {}, } `) }) @@ -571,8 +571,8 @@ describe('mutations in trx', () => { expect(result.data.createPost.ok).toBe(true) expect(calls).toHaveLength(1) expect(calls[0].query).toMatchInlineSnapshot(` - "mutation($options_MutationTransactionOptions_0: MutationTransactionOptions, $data_AuthorCreateInput_1: AuthorCreateInput!, $data_PostCreateInput_2: PostCreateInput!) { - mut: transaction(options: $options_MutationTransactionOptions_0) { + "mutation($MutationTransactionOptions_0: MutationTransactionOptions, $AuthorCreateInput_1: AuthorCreateInput!, $PostCreateInput_2: PostCreateInput!) { + mut: transaction(options: $MutationTransactionOptions_0) { ok errorMessage errors { @@ -581,7 +581,7 @@ describe('mutations in trx', () => { validation { ... ValidationResult } - createAuthor(data: $data_AuthorCreateInput_1) { + createAuthor(data: $AuthorCreateInput_1) { ok errorMessage errors { @@ -591,7 +591,7 @@ describe('mutations in trx', () => { ... ValidationResult } } - createPost(data: $data_PostCreateInput_2) { + createPost(data: $PostCreateInput_2) { ok errorMessage errors { @@ -636,18 +636,18 @@ describe('mutations in trx', () => { " `) expect(calls[0].variables).toMatchInlineSnapshot(` - { - "data_AuthorCreateInput_1": { - "email": "xx@localhost", - "name": "John", - }, - "data_PostCreateInput_2": { - "content": "World", - "title": "Hello", - }, - "options_MutationTransactionOptions_0": {}, - } - `) + { + "AuthorCreateInput_1": { + "email": "xx@localhost", + "name": "John", + }, + "MutationTransactionOptions_0": {}, + "PostCreateInput_2": { + "content": "World", + "title": "Hello", + }, + } + `) }) test('mutation and query combo trx', async () => { @@ -684,8 +684,8 @@ describe('mutations in trx', () => { expect(calls).toHaveLength(1) expect(calls[0].query).toMatchInlineSnapshot(` - "mutation($options_MutationTransactionOptions_0: MutationTransactionOptions, $data_PostCreateInput_1: PostCreateInput!, $by_PostUniqueWhere_2: PostUniqueWhere!) { - mut: transaction(options: $options_MutationTransactionOptions_0) { + "mutation($MutationTransactionOptions_0: MutationTransactionOptions, $PostCreateInput_1: PostCreateInput!, $PostUniqueWhere_2: PostUniqueWhere!) { + mut: transaction(options: $MutationTransactionOptions_0) { ok errorMessage errors { @@ -694,7 +694,7 @@ describe('mutations in trx', () => { validation { ... ValidationResult } - createPost(data: $data_PostCreateInput_1) { + createPost(data: $PostCreateInput_1) { ok errorMessage errors { @@ -705,7 +705,7 @@ describe('mutations in trx', () => { } } post: query { - value: getPost(by: $by_PostUniqueWhere_2) { + value: getPost(by: $PostUniqueWhere_2) { title content } @@ -745,17 +745,17 @@ describe('mutations in trx', () => { " `) expect(calls[0].variables).toMatchInlineSnapshot(` - { - "by_PostUniqueWhere_2": { - "id": 1, - }, - "data_PostCreateInput_1": { - "content": "World", - "title": "Hello", - }, - "options_MutationTransactionOptions_0": {}, - } - `) + { + "MutationTransactionOptions_0": {}, + "PostCreateInput_1": { + "content": "World", + "title": "Hello", + }, + "PostUniqueWhere_2": { + "id": 1, + }, + } + `) }) }) @@ -778,8 +778,8 @@ describe('mutations without trx', () => { expect(result.ok).toBe(true) expect(calls).toHaveLength(1) expect(calls[0].query).toMatchInlineSnapshot(` - "mutation($data_AuthorCreateInput_0: AuthorCreateInput!) { - mut: createAuthor(data: $data_AuthorCreateInput_0) { + "mutation($AuthorCreateInput_0: AuthorCreateInput!) { + mut: createAuthor(data: $AuthorCreateInput_0) { ok errorMessage errors { @@ -824,7 +824,7 @@ describe('mutations without trx', () => { `) expect(calls[0].variables).toMatchInlineSnapshot(` { - "data_AuthorCreateInput_0": { + "AuthorCreateInput_0": { "email": "xx@localhost", "name": "John", }, @@ -849,8 +849,8 @@ describe('mutations without trx', () => { expect(result.node?.id).toBe(123) expect(calls).toHaveLength(1) expect(calls[0].query).toMatchInlineSnapshot(` - "mutation($data_AuthorCreateInput_0: AuthorCreateInput!) { - mut: createAuthor(data: $data_AuthorCreateInput_0) { + "mutation($AuthorCreateInput_0: AuthorCreateInput!) { + mut: createAuthor(data: $AuthorCreateInput_0) { ok errorMessage errors { @@ -898,7 +898,7 @@ describe('mutations without trx', () => { `) expect(calls[0].variables).toMatchInlineSnapshot(` { - "data_AuthorCreateInput_0": { + "AuthorCreateInput_0": { "email": "xx@localhost", "name": "John", }, @@ -921,8 +921,8 @@ describe('mutations without trx', () => { })) expect(calls).toHaveLength(1) expect(calls[0].query).toMatchInlineSnapshot(` - "mutation($by_AuthorUniqueWhere_0: AuthorUniqueWhere!, $data_AuthorUpdateInput_1: AuthorUpdateInput!) { - mut: updateAuthor(by: $by_AuthorUniqueWhere_0, data: $data_AuthorUpdateInput_1) { + "mutation($AuthorUniqueWhere_0: AuthorUniqueWhere!, $AuthorUpdateInput_1: AuthorUpdateInput!) { + mut: updateAuthor(by: $AuthorUniqueWhere_0, data: $AuthorUpdateInput_1) { ok errorMessage errors { @@ -967,10 +967,10 @@ describe('mutations without trx', () => { `) expect(calls[0].variables).toMatchInlineSnapshot(` { - "by_AuthorUniqueWhere_0": { + "AuthorUniqueWhere_0": { "id": 1, }, - "data_AuthorUpdateInput_1": { + "AuthorUpdateInput_1": { "email": "xx@localhost", "name": "John", }, @@ -989,8 +989,8 @@ describe('mutations without trx', () => { })) expect(calls).toHaveLength(1) expect(calls[0].query).toMatchInlineSnapshot(` - "mutation($by_AuthorUniqueWhere_0: AuthorUniqueWhere!) { - mut: deleteAuthor(by: $by_AuthorUniqueWhere_0) { + "mutation($AuthorUniqueWhere_0: AuthorUniqueWhere!) { + mut: deleteAuthor(by: $AuthorUniqueWhere_0) { ok errorMessage errors { @@ -1015,7 +1015,7 @@ describe('mutations without trx', () => { `) expect(calls[0].variables).toMatchInlineSnapshot(` { - "by_AuthorUniqueWhere_0": { + "AuthorUniqueWhere_0": { "id": 1, }, } @@ -1041,8 +1041,8 @@ describe('mutations without trx', () => { })) expect(calls).toHaveLength(1) expect(calls[0].query).toMatchInlineSnapshot(` - "mutation($by_AuthorUniqueWhere_0: AuthorUniqueWhere!, $create_AuthorCreateInput_1: AuthorCreateInput!, $update_AuthorUpdateInput_2: AuthorUpdateInput!) { - mut: upsertAuthor(by: $by_AuthorUniqueWhere_0, create: $create_AuthorCreateInput_1, update: $update_AuthorUpdateInput_2) { + "mutation($AuthorUniqueWhere_0: AuthorUniqueWhere!, $AuthorCreateInput_1: AuthorCreateInput!, $AuthorUpdateInput_2: AuthorUpdateInput!) { + mut: upsertAuthor(by: $AuthorUniqueWhere_0, create: $AuthorCreateInput_1, update: $AuthorUpdateInput_2) { ok errorMessage errors { @@ -1087,14 +1087,14 @@ describe('mutations without trx', () => { `) expect(calls[0].variables).toMatchInlineSnapshot(` { - "by_AuthorUniqueWhere_0": { - "id": 1, - }, - "create_AuthorCreateInput_1": { + "AuthorCreateInput_1": { "email": "xx@localhost", "name": "John", }, - "update_AuthorUpdateInput_2": { + "AuthorUniqueWhere_0": { + "id": 1, + }, + "AuthorUpdateInput_2": { "email": "xx@localhost", "name": "John", }, @@ -1130,8 +1130,8 @@ describe('mutations without trx', () => { expect(result[1].ok).toBe(true) expect(calls).toHaveLength(1) expect(calls[0].query).toMatchInlineSnapshot(` - "mutation($data_AuthorCreateInput_0: AuthorCreateInput!, $data_AuthorCreateInput_1: AuthorCreateInput!) { - mut_0: createAuthor(data: $data_AuthorCreateInput_0) { + "mutation($AuthorCreateInput_0: AuthorCreateInput!, $AuthorCreateInput_1: AuthorCreateInput!) { + mut_0: createAuthor(data: $AuthorCreateInput_0) { ok errorMessage errors { @@ -1141,7 +1141,7 @@ describe('mutations without trx', () => { ... ValidationResult } } - mut_1: createAuthor(data: $data_AuthorCreateInput_1) { + mut_1: createAuthor(data: $AuthorCreateInput_1) { ok errorMessage errors { @@ -1186,11 +1186,11 @@ describe('mutations without trx', () => { `) expect(calls[0].variables).toMatchInlineSnapshot(` { - "data_AuthorCreateInput_0": { + "AuthorCreateInput_0": { "email": "xx@localhost", "name": "John", }, - "data_AuthorCreateInput_1": { + "AuthorCreateInput_1": { "email": "xx@localhost", "name": "John", }, @@ -1225,8 +1225,8 @@ describe('mutations without trx', () => { expect(result.createPost.ok).toBe(true) expect(calls).toHaveLength(1) expect(calls[0].query).toMatchInlineSnapshot(` - "mutation($data_AuthorCreateInput_0: AuthorCreateInput!, $data_PostCreateInput_1: PostCreateInput!) { - createAuthor(data: $data_AuthorCreateInput_0) { + "mutation($AuthorCreateInput_0: AuthorCreateInput!, $PostCreateInput_1: PostCreateInput!) { + createAuthor(data: $AuthorCreateInput_0) { ok errorMessage errors { @@ -1236,7 +1236,7 @@ describe('mutations without trx', () => { ... ValidationResult } } - createPost(data: $data_PostCreateInput_1) { + createPost(data: $PostCreateInput_1) { ok errorMessage errors { @@ -1281,11 +1281,11 @@ describe('mutations without trx', () => { `) expect(calls[0].variables).toMatchInlineSnapshot(` { - "data_AuthorCreateInput_0": { + "AuthorCreateInput_0": { "email": "xx@localhost", "name": "John", }, - "data_PostCreateInput_1": { + "PostCreateInput_1": { "content": "World", "title": "Hello", }, @@ -1323,8 +1323,8 @@ describe('mutations without trx', () => { expect(calls).toHaveLength(1) expect(calls[0].query).toMatchInlineSnapshot(` - "mutation($data_PostCreateInput_0: PostCreateInput!, $by_PostUniqueWhere_1: PostUniqueWhere!) { - createPost(data: $data_PostCreateInput_0) { + "mutation($PostCreateInput_0: PostCreateInput!, $PostUniqueWhere_1: PostUniqueWhere!) { + createPost(data: $PostCreateInput_0) { ok errorMessage errors { @@ -1335,7 +1335,7 @@ describe('mutations without trx', () => { } } post: query { - value: getPost(by: $by_PostUniqueWhere_1) { + value: getPost(by: $PostUniqueWhere_1) { title content } @@ -1375,13 +1375,13 @@ describe('mutations without trx', () => { `) expect(calls[0].variables).toMatchInlineSnapshot(` { - "by_PostUniqueWhere_1": { - "id": 1, - }, - "data_PostCreateInput_0": { + "PostCreateInput_0": { "content": "World", "title": "Hello", }, + "PostUniqueWhere_1": { + "id": 1, + }, } `) }) diff --git a/packages/client-content/tests/cases/unit/query.test.ts b/packages/client-content/tests/cases/unit/query.test.ts index 33ff6a2a51..ecd7156935 100644 --- a/packages/client-content/tests/cases/unit/query.test.ts +++ b/packages/client-content/tests/cases/unit/query.test.ts @@ -184,11 +184,11 @@ describe('queries', () => { }) expect(calls).toHaveLength(1) expect(calls[0].query).toMatchInlineSnapshot(` - "query($filter_PostWhere_0: PostWhere, $limit_Int_1: Int) { + "query($PostWhere_0: PostWhere, $Int_1: Int) { authors: listAuthor { name email - posts(filter: $filter_PostWhere_0, limit: $limit_Int_1) { + posts(filter: $PostWhere_0, limit: $Int_1) { title content } @@ -198,14 +198,14 @@ describe('queries', () => { `) expect(calls[0].variables).toMatchInlineSnapshot(` { - "filter_PostWhere_0": { + "Int_1": 10, + "PostWhere_0": { "tags": { "name": { "eq": "foo", }, }, }, - "limit_Int_1": 10, } `) }) @@ -223,8 +223,8 @@ describe('queries', () => { }) expect(calls).toHaveLength(1) expect(calls[0].query).toMatchInlineSnapshot(` - "query($filter_AuthorWhere_0: AuthorWhere, $orderBy_AuthorOrderBy_1: [AuthorOrderBy!], $limit_Int_2: Int, $offset_Int_3: Int) { - authors: listAuthor(filter: $filter_AuthorWhere_0, orderBy: $orderBy_AuthorOrderBy_1, limit: $limit_Int_2, offset: $offset_Int_3) { + "query($AuthorWhere_0: AuthorWhere, $AuthorOrderBy_1: [AuthorOrderBy!], $Int_2: Int, $Int_3: Int) { + authors: listAuthor(filter: $AuthorWhere_0, orderBy: $AuthorOrderBy_1, limit: $Int_2, offset: $Int_3) { name email } @@ -233,18 +233,18 @@ describe('queries', () => { `) expect(calls[0].variables).toMatchInlineSnapshot(` { - "filter_AuthorWhere_0": { - "name": { - "eq": "John", - }, - }, - "limit_Int_2": 10, - "offset_Int_3": 20, - "orderBy_AuthorOrderBy_1": [ + "AuthorOrderBy_1": [ { "name": "asc", }, ], + "AuthorWhere_0": { + "name": { + "eq": "John", + }, + }, + "Int_2": 10, + "Int_3": 20, } `) }) @@ -259,8 +259,8 @@ describe('queries', () => { }) expect(calls).toHaveLength(1) expect(calls[0].query).toMatchInlineSnapshot(` - "query($by_AuthorUniqueWhere_0: AuthorUniqueWhere!) { - authors: getAuthor(by: $by_AuthorUniqueWhere_0) { + "query($AuthorUniqueWhere_0: AuthorUniqueWhere!) { + authors: getAuthor(by: $AuthorUniqueWhere_0) { name email } @@ -269,7 +269,7 @@ describe('queries', () => { `) expect(calls[0].variables).toMatchInlineSnapshot(` { - "by_AuthorUniqueWhere_0": { + "AuthorUniqueWhere_0": { "id": 123, }, } diff --git a/packages/client/tests/cases/unit/generateUploadUrlMutationBuilder.spec.ts b/packages/client/tests/cases/unit/generateUploadUrlMutationBuilder.spec.ts index 94bc29493a..cff0de6456 100644 --- a/packages/client/tests/cases/unit/generateUploadUrlMutationBuilder.spec.ts +++ b/packages/client/tests/cases/unit/generateUploadUrlMutationBuilder.spec.ts @@ -12,8 +12,8 @@ describe('generate upload url mutation builder', () => { }), ).toMatchInlineSnapshot(` { - "query": "mutation($contentType_String_0: String) { - mySingleImage: generateUploadUrl(contentType: $contentType_String_0) { + "query": "mutation($String_0: String) { + mySingleImage: generateUploadUrl(contentType: $String_0) { url publicUrl method @@ -25,7 +25,7 @@ describe('generate upload url mutation builder', () => { } ", "variables": { - "contentType_String_0": "image/png", + "String_0": "image/png", }, } `) @@ -46,8 +46,8 @@ describe('generate upload url mutation builder', () => { }), ).toMatchInlineSnapshot(` { - "query": "mutation($contentType_String_0: String, $contentType_String_1: String, $expiration_Int_2: Int, $prefix_String_3: String, $acl_S3Acl_4: S3Acl) { - myPng: generateUploadUrl(contentType: $contentType_String_0) { + "query": "mutation($String_0: String, $String_1: String, $Int_2: Int, $String_3: String, $S3Acl_4: S3Acl) { + myPng: generateUploadUrl(contentType: $String_0) { url publicUrl method @@ -56,7 +56,7 @@ describe('generate upload url mutation builder', () => { value } } - myMp3: generateUploadUrl(contentType: $contentType_String_1, expiration: $expiration_Int_2, prefix: $prefix_String_3, acl: $acl_S3Acl_4) { + myMp3: generateUploadUrl(contentType: $String_1, expiration: $Int_2, prefix: $String_3, acl: $S3Acl_4) { url publicUrl method @@ -68,11 +68,11 @@ describe('generate upload url mutation builder', () => { } ", "variables": { - "acl_S3Acl_4": "PUBLIC_READ", - "contentType_String_0": "image/png", - "contentType_String_1": "audio/mpeg", - "expiration_Int_2": 123456, - "prefix_String_3": "foo", + "Int_2": 123456, + "S3Acl_4": "PUBLIC_READ", + "String_0": "image/png", + "String_1": "audio/mpeg", + "String_3": "foo", }, } `) diff --git a/packages/graphql-builder/src/GraphQlQueryPrinter.ts b/packages/graphql-builder/src/GraphQlQueryPrinter.ts index ca69a2c543..4b4d7152a4 100644 --- a/packages/graphql-builder/src/GraphQlQueryPrinter.ts +++ b/packages/graphql-builder/src/GraphQlQueryPrinter.ts @@ -12,6 +12,8 @@ export class GraphQlQueryPrinter { value: JSONValue }> = {} + private variableValueToName: Record> = {} + private usedFragments = new Set() private body = '' @@ -99,17 +101,13 @@ export class GraphQlQueryPrinter { if (arg.value === undefined) { continue } - const variableName = `${argName}_${arg.graphQlType.replace(/[\W]/g, '')}_${this.variableCounter++}` + const variableName = this.resolveVariableName(arg.graphQlType, arg.value) if (i++ === 0) { this.body += '(' } else { this.body += ', ' } this.body += `${argName}: $${variableName}` - this.variables[variableName] = { - type: arg.graphQlType, - value: arg.value, - } } if (i > 0) { this.body += ')' @@ -122,6 +120,23 @@ export class GraphQlQueryPrinter { this.body += '\n' } + private resolveVariableName(graphQlType: string, value: JSONValue): string { + const variableName = this.variableValueToName[graphQlType]?.get(value) + if (variableName) { + return variableName + } + const newVariableName = `${graphQlType.replace(/[\W]/g, '')}_${this.variableCounter++}` + this.variables[newVariableName] = { + type: graphQlType, + value: value, + } + if (!this.variableValueToName[graphQlType]) { + this.variableValueToName[graphQlType] = new Map() + } + this.variableValueToName[graphQlType].set(value, newVariableName) + return newVariableName + } + private cleanState(): void { this.body = '' this.variableCounter = 0 diff --git a/packages/graphql-builder/tests/cases/unit/builder.test.ts b/packages/graphql-builder/tests/cases/unit/builder.test.ts index 53406e78f8..83072a9884 100644 --- a/packages/graphql-builder/tests/cases/unit/builder.test.ts +++ b/packages/graphql-builder/tests/cases/unit/builder.test.ts @@ -18,4 +18,32 @@ describe('builder', () => { } `) }) + + test('variable deduplication', async () => { + const printer = new GraphQlQueryPrinter() + const someValue = { foo: 'bar' } + const result = printer.printDocument('query', [ + new GraphQlField('b', 'test', { + variable: { graphQlType: 'Json', value: someValue }, + }), + new GraphQlField('a', 'test', { + variable: { graphQlType: 'Json', value: someValue }, + }), + ], {}) + expect(result).toMatchInlineSnapshot(` + { + "query": "query($Json_0: Json) { + b: test(variable: $Json_0) + a: test(variable: $Json_0) + } + ", + "variables": { + "Json_0": { + "foo": "bar", + }, + }, + } + `) + + }) })