From bab530f06f493107ad5cd5f4ab4a14746f0e19df Mon Sep 17 00:00:00 2001 From: Vishal-Sikka <145092393+Vishal-Sikka@users.noreply.github.com> Date: Fri, 29 Sep 2023 10:19:09 +0530 Subject: [PATCH 1/6] Update resolvePath.ts when the scalar type is identified as UUID and the argument a is an array, each UUID in the array is individually encoded as a string within the GraphQL mutation. This should solve the issue of incorrect encoding for an array of UUID scalars --- .../TreeToTS/functions/new/resolvePath.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/graphql-zeus-core/TreeToTS/functions/new/resolvePath.ts b/packages/graphql-zeus-core/TreeToTS/functions/new/resolvePath.ts index 245336ab..ca861dcd 100644 --- a/packages/graphql-zeus-core/TreeToTS/functions/new/resolvePath.ts +++ b/packages/graphql-zeus-core/TreeToTS/functions/new/resolvePath.ts @@ -133,10 +133,14 @@ export const InternalArgsBuilt = ({ } const checkType = ResolveFromPath(props, returns, ops)(p); if (checkType.startsWith('scalar.')) { - // eslint-disable-next-line @typescript-eslint/no-unused-vars - const [_, ...splittedScalar] = checkType.split('.'); - const scalarKey = splittedScalar.join('.'); - return (scalars?.[scalarKey]?.encode?.(a) as string) || JSON.stringify(a); + const scalarKey = checkType.split('.').slice(1).join('.'); + // If the type is a UUID array, apply the scalar encoding function to each element + if (scalarKey === 'UUID' && Array.isArray(a)) { + return `[${a.map((uuid) => scalars?.[scalarKey]?.encode?.(uuid) || JSON.stringify(uuid)).join(', ')}]`; + } else { + // Handling for other scalars + return (scalars?.[scalarKey]?.encode?.(a) as string) || JSON.stringify(a); + } } if (Array.isArray(a)) { return `[${a.map((arr) => arb(arr, p, false)).join(', ')}]`; From 3cf694eca569f0dcbda904b1a35e80abef070fa6 Mon Sep 17 00:00:00 2001 From: Vishal-Sikka <145092393+Vishal-Sikka@users.noreply.github.com> Date: Fri, 29 Sep 2023 10:23:34 +0530 Subject: [PATCH 2/6] Create uuid-test.spec.ts --- .../__tests__/uuid-test.spec.ts | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 packages/graphql-zeus-core/__tests__/uuid-test.spec.ts diff --git a/packages/graphql-zeus-core/__tests__/uuid-test.spec.ts b/packages/graphql-zeus-core/__tests__/uuid-test.spec.ts new file mode 100644 index 00000000..1c4e1263 --- /dev/null +++ b/packages/graphql-zeus-core/__tests__/uuid-test.spec.ts @@ -0,0 +1,41 @@ +import { ZeusScalars } from '@/TreeToTS/functions/new/clientFunctions'; +import { AllTypesPropsType, Operations, ReturnTypesType, ZeusArgsType } from '@/TreeToTS/functions/new/models'; +import { InternalArgsBuilt } from '@/TreeToTS/functions/new/resolvePath'; + +const scalars = ZeusScalars({ + UUID: { + encode: (e: unknown) => String(`"${e}"`), + decode: (e: unknown) => e as string, + }, +}); + +describe('InternalArgsBuilt function', () => { + let props: AllTypesPropsType; + let returns: ReturnTypesType; + let ops: Operations; + let vars: Array<{ name: string; graphQLType: string }>; + + beforeEach(() => { + props = {}; + returns = {}; + ops = {}; + vars = []; + }); + + it('should correctly encode empty UUID arrays', () => { + const arb = InternalArgsBuilt({ props, returns, ops, scalars, vars }); + const result = arb({ ids: [] } as ZeusArgsType, 'someMutation.ids', true); + expect(result).toMatchInlineSnapshot(`"ids: []"`); + }); + + it('should correctly encode non-empty UUID arrays', () => { + const uuid1 = '0170743b-6876-4ec2-839c-23480da37b28'; + const uuid2 = '3ed80968-a077-4e43-9552-21ee8812f615'; + + const arb = InternalArgsBuilt({ props, returns, ops, scalars, vars }); + const result = arb({ ids: [uuid1, uuid2] } as ZeusArgsType, 'someMutation.ids', true); + expect(result).toMatchInlineSnapshot( + `"ids: ["0170743b-6876-4ec2-839c-23480da37b28", "3ed80968-a077-4e43-9552-21ee8812f615"]"`, + ); + }); +}); From b6abe384c14fb993b9037cf12be4bab8cd29487e Mon Sep 17 00:00:00 2001 From: Vishal-Sikka <145092393+Vishal-Sikka@users.noreply.github.com> Date: Fri, 29 Sep 2023 12:07:03 +0530 Subject: [PATCH 3/6] Update uuid-test.spec.ts --- .../__tests__/uuid-test.spec.ts | 43 +++++++++++++++---- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/packages/graphql-zeus-core/__tests__/uuid-test.spec.ts b/packages/graphql-zeus-core/__tests__/uuid-test.spec.ts index 1c4e1263..565bd534 100644 --- a/packages/graphql-zeus-core/__tests__/uuid-test.spec.ts +++ b/packages/graphql-zeus-core/__tests__/uuid-test.spec.ts @@ -2,6 +2,7 @@ import { ZeusScalars } from '@/TreeToTS/functions/new/clientFunctions'; import { AllTypesPropsType, Operations, ReturnTypesType, ZeusArgsType } from '@/TreeToTS/functions/new/models'; import { InternalArgsBuilt } from '@/TreeToTS/functions/new/resolvePath'; +// Define your scalars const scalars = ZeusScalars({ UUID: { encode: (e: unknown) => String(`"${e}"`), @@ -22,20 +23,46 @@ describe('InternalArgsBuilt function', () => { vars = []; }); - it('should correctly encode empty UUID arrays', () => { + it('should correctly encode UUIDs in a mutation input', () => { + const uuidArrayInput = { + ids: ['0170743b-6876-4ec2-839c-23480da37b28', '3ed80968-a077-4e43-9552-21ee8812f615'], + } as ZeusArgsType; + const arb = InternalArgsBuilt({ props, returns, ops, scalars, vars }); - const result = arb({ ids: [] } as ZeusArgsType, 'someMutation.ids', true); - expect(result).toMatchInlineSnapshot(`"ids: []"`); + const result = arb(uuidArrayInput, 'someMutation.ids', true); + + const expectedOutput = `ids: ["0170743b-6876-4ec2-839c-23480da37b28", "3ed80968-a077-4e43-9552-21ee8812f615"]`; + + expect(result).toEqual(expectedOutput); }); - it('should correctly encode non-empty UUID arrays', () => { + it('should encode non-empty UUID arrays with consistent UUIDs', () => { const uuid1 = '0170743b-6876-4ec2-839c-23480da37b28'; const uuid2 = '3ed80968-a077-4e43-9552-21ee8812f615'; + const nonEmptyUUIDArrayInput = { ids: [uuid1, uuid2] } as ZeusArgsType; + const arb = InternalArgsBuilt({ props, returns, ops, scalars, vars }); + const result = arb(nonEmptyUUIDArrayInput, 'someMutation.ids', true); + + const expectedOutput = `ids: ["0170743b-6876-4ec2-839c-23480da37b28", "3ed80968-a077-4e43-9552-21ee8812f615"]`; + + expect(result).toEqual(expectedOutput); + }); + + it('should handle null input gracefully', () => { + const nullInput = { ids: null } as ZeusArgsType; const arb = InternalArgsBuilt({ props, returns, ops, scalars, vars }); - const result = arb({ ids: [uuid1, uuid2] } as ZeusArgsType, 'someMutation.ids', true); - expect(result).toMatchInlineSnapshot( - `"ids: ["0170743b-6876-4ec2-839c-23480da37b28", "3ed80968-a077-4e43-9552-21ee8812f615"]"`, - ); + const result = arb(nullInput, 'someMutation.ids', true); + + const expectedOutput = `ids: null`; + + expect(result).toEqual(expectedOutput); }); + + it('should handle undefined input gracefully', () => { + const arb = InternalArgsBuilt({ props, returns, ops, scalars, vars }); + const result = arb(undefined, 'someMutation.ids', true); // Pass undefined as input + expect(result).toEqual('ids: null'); // Expect the result to be 'ids: null' + }); + }); From 5f9ef1ff5972929b683ec52a9f89cbad50745126 Mon Sep 17 00:00:00 2001 From: Vishal-Sikka <145092393+Vishal-Sikka@users.noreply.github.com> Date: Fri, 29 Sep 2023 12:07:41 +0530 Subject: [PATCH 4/6] Update resolvePath.ts --- .../TreeToTS/functions/new/resolvePath.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/graphql-zeus-core/TreeToTS/functions/new/resolvePath.ts b/packages/graphql-zeus-core/TreeToTS/functions/new/resolvePath.ts index ca861dcd..09f52e4f 100644 --- a/packages/graphql-zeus-core/TreeToTS/functions/new/resolvePath.ts +++ b/packages/graphql-zeus-core/TreeToTS/functions/new/resolvePath.ts @@ -133,8 +133,9 @@ export const InternalArgsBuilt = ({ } const checkType = ResolveFromPath(props, returns, ops)(p); if (checkType.startsWith('scalar.')) { - const scalarKey = checkType.split('.').slice(1).join('.'); - // If the type is a UUID array, apply the scalar encoding function to each element + const [, ...splittedScalar] = checkType.split('.'); + const scalarKey = splittedScalar.join('.'); + if (scalarKey === 'UUID' && Array.isArray(a)) { return `[${a.map((uuid) => scalars?.[scalarKey]?.encode?.(uuid) || JSON.stringify(uuid)).join(', ')}]`; } else { @@ -142,6 +143,9 @@ export const InternalArgsBuilt = ({ return (scalars?.[scalarKey]?.encode?.(a) as string) || JSON.stringify(a); } } + if (a === undefined) { + return 'ids: null'; + } if (Array.isArray(a)) { return `[${a.map((arr) => arb(arr, p, false)).join(', ')}]`; } From 64af6daaac228736c099078d7d57db271345daff Mon Sep 17 00:00:00 2001 From: Vishal-Sikka <145092393+Vishal-Sikka@users.noreply.github.com> Date: Fri, 29 Sep 2023 18:21:27 +0530 Subject: [PATCH 5/6] Update resolvePath.ts --- .../graphql-zeus-core/TreeToTS/functions/new/resolvePath.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/graphql-zeus-core/TreeToTS/functions/new/resolvePath.ts b/packages/graphql-zeus-core/TreeToTS/functions/new/resolvePath.ts index 09f52e4f..b5fac158 100644 --- a/packages/graphql-zeus-core/TreeToTS/functions/new/resolvePath.ts +++ b/packages/graphql-zeus-core/TreeToTS/functions/new/resolvePath.ts @@ -133,7 +133,7 @@ export const InternalArgsBuilt = ({ } const checkType = ResolveFromPath(props, returns, ops)(p); if (checkType.startsWith('scalar.')) { - const [, ...splittedScalar] = checkType.split('.'); + const [_, ...splittedScalar] = checkType.split('.'); const scalarKey = splittedScalar.join('.'); if (scalarKey === 'UUID' && Array.isArray(a)) { From 6346fd03b3441ffbd143db22efc2f25f0a01a9bd Mon Sep 17 00:00:00 2001 From: Vishal-Sikka <145092393+Vishal-Sikka@users.noreply.github.com> Date: Sat, 30 Sep 2023 10:41:55 +0530 Subject: [PATCH 6/6] Update resolvePath.ts --- packages/graphql-zeus-core/TreeToTS/functions/new/resolvePath.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/graphql-zeus-core/TreeToTS/functions/new/resolvePath.ts b/packages/graphql-zeus-core/TreeToTS/functions/new/resolvePath.ts index b5fac158..69a69a37 100644 --- a/packages/graphql-zeus-core/TreeToTS/functions/new/resolvePath.ts +++ b/packages/graphql-zeus-core/TreeToTS/functions/new/resolvePath.ts @@ -133,6 +133,7 @@ export const InternalArgsBuilt = ({ } const checkType = ResolveFromPath(props, returns, ops)(p); if (checkType.startsWith('scalar.')) { + // eslint-disable-next-line @typescript-eslint/no-unused-vars const [_, ...splittedScalar] = checkType.split('.'); const scalarKey = splittedScalar.join('.');