Skip to content

Commit e709727

Browse files
authored
Merge pull request #271 from META-DREAMER/fix-multiple-aliases
Fix multiple aliases when generating query
2 parents 634dc95 + a8912ca commit e709727

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,40 @@ describe('Test generated function buildQuery', () => {
266266
}
267267
}`);
268268
});
269+
test('Query with multiple aliases', () => {
270+
const matchExact = replSpace(
271+
builder('query', {
272+
__alias: {
273+
play: {
274+
cards: {
275+
name: true,
276+
age: true,
277+
bio: true,
278+
},
279+
},
280+
shuffle: {
281+
cards: {
282+
name: true,
283+
age: true,
284+
bio: true,
285+
},
286+
},
287+
},
288+
}),
289+
);
290+
matchExact(`query{
291+
play:cards{
292+
name
293+
age
294+
bio
295+
}
296+
shuffle:cards{
297+
name
298+
age
299+
bio
300+
}
301+
}`);
302+
});
269303
test('Simple query with enums', () => {
270304
const enum Status {
271305
CREATED = 'CREATED',

src/TreeToTS/functions/new/buildQuery.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,18 @@ export const InternalsBuildQuery = (
3333
return `${ibb(args ? `${k}(${args})` : k, o[1], p, false)}`;
3434
}
3535
if (k === '__alias') {
36-
const alias = Object.keys(o)[0];
37-
const objectUnderAlias = o[alias];
38-
if (typeof objectUnderAlias !== 'object' || Array.isArray(objectUnderAlias)) {
39-
throw new Error('Invalid alias it should be __alias:{ YOUR_ALIAS_NAME: { OPERATION_NAME: { ...selectors }}}');
40-
}
41-
const operationName = Object.keys(objectUnderAlias)[0];
42-
const operation = objectUnderAlias[operationName];
43-
return ibb(`${alias}:${operationName}`, operation, p, false);
36+
return Object.entries(o)
37+
.map(([alias, objectUnderAlias]) => {
38+
if (typeof objectUnderAlias !== 'object' || Array.isArray(objectUnderAlias)) {
39+
throw new Error(
40+
'Invalid alias it should be __alias:{ YOUR_ALIAS_NAME: { OPERATION_NAME: { ...selectors }}}',
41+
);
42+
}
43+
const operationName = Object.keys(objectUnderAlias)[0];
44+
const operation = objectUnderAlias[operationName];
45+
return ibb(`${alias}:${operationName}`, operation, p, false);
46+
})
47+
.join('\n');
4448
}
4549
const hasOperationName = root && options?.operationName ? ' ' + options.operationName : '';
4650
const hasVariables = root && options?.variables?.$params ? `(${options.variables?.$params})` : '';

0 commit comments

Comments
 (0)