Skip to content

Commit

Permalink
Fix invalid query generation when query arguments have arrays of obje…
Browse files Browse the repository at this point in the history
…cts or are empty
  • Loading branch information
META-DREAMER committed Apr 19, 2022
1 parent 520e7eb commit bd216d3
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
47 changes: 47 additions & 0 deletions src/TreeToTS/functions/new/buildQuery.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,30 @@ describe('Test generated function buildQuery', () => {
}
}`);
});
test('Query with array and object arguments', () => {
const matchExact = replSpace(
builder('query', {
cards: [
{
where: { active: true },
order_by: [{ date: 'asc' }, { age: 'desc' }],
},
{
name: true,
age: true,
bio: true,
},
],
}),
);
matchExact(`query{
cards(where: {active: true}, order_by: [{date: "asc"}, {age: "desc"}]){
name
age
bio
}
}`);
});
test('Query with arguments and variables', () => {
const variables = useZeusVariables({ id: 'String!' })({
id: 'a1',
Expand Down Expand Up @@ -102,6 +126,28 @@ describe('Test generated function buildQuery', () => {
}
}`);
});

test('Query with empty arguments params', () => {
const matchExact = replSpace(
builder('query', {
cards: [
{},
{
name: true,
age: true,
bio: true,
},
],
}),
);
matchExact(`query{
cards {
name
age
bio
}
}`);
});
test('Mutation with arguments', () => {
const enum Status {
CREATED = 'CREATED',
Expand Down Expand Up @@ -155,6 +201,7 @@ describe('Test generated function buildQuery', () => {
}
}`);
});

test('Undefined param', () => {
const Children = undefined;
const matchExact = replSpace(
Expand Down
8 changes: 2 additions & 6 deletions src/TreeToTS/functions/new/buildQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,8 @@ export const InternalsBuildQuery = (
return `${k} ${o}`;
}
if (Array.isArray(o)) {
return `${ibb(
`${k}(${InternalArgsBuilt(props, returns, ops, options?.variables?.values)(o[0], newPath)})`,
o[1],
p,
false,
)}`;
const args = InternalArgsBuilt(props, returns, ops, options?.variables?.values)(o[0], newPath);
return `${ibb(args ? `${k}(${args})` : k, o[1], p, false)}`;
}
if (k === '__alias') {
const alias = Object.keys(o)[0];
Expand Down
2 changes: 1 addition & 1 deletion src/TreeToTS/functions/new/resolvePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const InternalArgsBuilt = (
) => {
const arb = (a: ZeusArgsType, p = '', root = true): string => {
if (Array.isArray(a)) {
return `[${a.map((arr) => arb(arr, p)).join(', ')}]`;
return `[${a.map((arr) => arb(arr, p, false)).join(', ')}]`;
}
if (typeof a === 'string') {
if (a.startsWith('$') && variables?.[a.slice(1)]) {
Expand Down

0 comments on commit bd216d3

Please sign in to comment.