Skip to content

Commit bd216d3

Browse files
committed
Fix invalid query generation when query arguments have arrays of objects or are empty
1 parent 520e7eb commit bd216d3

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

src/TreeToTS/functions/new/buildQuery.spec.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,30 @@ describe('Test generated function buildQuery', () => {
6969
}
7070
}`);
7171
});
72+
test('Query with array and object arguments', () => {
73+
const matchExact = replSpace(
74+
builder('query', {
75+
cards: [
76+
{
77+
where: { active: true },
78+
order_by: [{ date: 'asc' }, { age: 'desc' }],
79+
},
80+
{
81+
name: true,
82+
age: true,
83+
bio: true,
84+
},
85+
],
86+
}),
87+
);
88+
matchExact(`query{
89+
cards(where: {active: true}, order_by: [{date: "asc"}, {age: "desc"}]){
90+
name
91+
age
92+
bio
93+
}
94+
}`);
95+
});
7296
test('Query with arguments and variables', () => {
7397
const variables = useZeusVariables({ id: 'String!' })({
7498
id: 'a1',
@@ -102,6 +126,28 @@ describe('Test generated function buildQuery', () => {
102126
}
103127
}`);
104128
});
129+
130+
test('Query with empty arguments params', () => {
131+
const matchExact = replSpace(
132+
builder('query', {
133+
cards: [
134+
{},
135+
{
136+
name: true,
137+
age: true,
138+
bio: true,
139+
},
140+
],
141+
}),
142+
);
143+
matchExact(`query{
144+
cards {
145+
name
146+
age
147+
bio
148+
}
149+
}`);
150+
});
105151
test('Mutation with arguments', () => {
106152
const enum Status {
107153
CREATED = 'CREATED',
@@ -155,6 +201,7 @@ describe('Test generated function buildQuery', () => {
155201
}
156202
}`);
157203
});
204+
158205
test('Undefined param', () => {
159206
const Children = undefined;
160207
const matchExact = replSpace(

src/TreeToTS/functions/new/buildQuery.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,8 @@ export const InternalsBuildQuery = (
2929
return `${k} ${o}`;
3030
}
3131
if (Array.isArray(o)) {
32-
return `${ibb(
33-
`${k}(${InternalArgsBuilt(props, returns, ops, options?.variables?.values)(o[0], newPath)})`,
34-
o[1],
35-
p,
36-
false,
37-
)}`;
32+
const args = InternalArgsBuilt(props, returns, ops, options?.variables?.values)(o[0], newPath);
33+
return `${ibb(args ? `${k}(${args})` : k, o[1], p, false)}`;
3834
}
3935
if (k === '__alias') {
4036
const alias = Object.keys(o)[0];

src/TreeToTS/functions/new/resolvePath.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export const InternalArgsBuilt = (
9191
) => {
9292
const arb = (a: ZeusArgsType, p = '', root = true): string => {
9393
if (Array.isArray(a)) {
94-
return `[${a.map((arr) => arb(arr, p)).join(', ')}]`;
94+
return `[${a.map((arr) => arb(arr, p, false)).join(', ')}]`;
9595
}
9696
if (typeof a === 'string') {
9797
if (a.startsWith('$') && variables?.[a.slice(1)]) {

0 commit comments

Comments
 (0)