Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:346 Error encoding arrays of custom scalars #379

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion packages/graphql-zeus-core/TreeToTS/functions/new/resolvePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,16 @@ export const InternalArgsBuilt = ({
// 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);

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 (a === undefined) {
return 'ids: null';
}
if (Array.isArray(a)) {
return `[${a.map((arr) => arb(arr, p, false)).join(', ')}]`;
Expand Down
68 changes: 68 additions & 0 deletions packages/graphql-zeus-core/__tests__/uuid-test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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}"`),
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 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(uuidArrayInput, 'someMutation.ids', true);

const expectedOutput = `ids: ["0170743b-6876-4ec2-839c-23480da37b28", "3ed80968-a077-4e43-9552-21ee8812f615"]`;

expect(result).toEqual(expectedOutput);
});

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(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'
});

});