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 multiple aliases when generating query #271

Merged
merged 1 commit into from
Apr 20, 2022
Merged
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
34 changes: 34 additions & 0 deletions src/TreeToTS/functions/new/buildQuery.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,40 @@ describe('Test generated function buildQuery', () => {
}
}`);
});
test('Query with multiple aliases', () => {
const matchExact = replSpace(
builder('query', {
__alias: {
play: {
cards: {
name: true,
age: true,
bio: true,
},
},
shuffle: {
cards: {
name: true,
age: true,
bio: true,
},
},
},
}),
);
matchExact(`query{
play:cards{
name
age
bio
}
shuffle:cards{
name
age
bio
}
}`);
});
test('Simple query with enums', () => {
const enum Status {
CREATED = 'CREATED',
Expand Down
20 changes: 12 additions & 8 deletions src/TreeToTS/functions/new/buildQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,18 @@ export const InternalsBuildQuery = (
return `${ibb(args ? `${k}(${args})` : k, o[1], p, false)}`;
}
if (k === '__alias') {
const alias = Object.keys(o)[0];
const objectUnderAlias = o[alias];
if (typeof objectUnderAlias !== 'object' || Array.isArray(objectUnderAlias)) {
throw new Error('Invalid alias it should be __alias:{ YOUR_ALIAS_NAME: { OPERATION_NAME: { ...selectors }}}');
}
const operationName = Object.keys(objectUnderAlias)[0];
const operation = objectUnderAlias[operationName];
return ibb(`${alias}:${operationName}`, operation, p, false);
return Object.entries(o)
.map(([alias, objectUnderAlias]) => {
if (typeof objectUnderAlias !== 'object' || Array.isArray(objectUnderAlias)) {
throw new Error(
'Invalid alias it should be __alias:{ YOUR_ALIAS_NAME: { OPERATION_NAME: { ...selectors }}}',
);
}
const operationName = Object.keys(objectUnderAlias)[0];
const operation = objectUnderAlias[operationName];
return ibb(`${alias}:${operationName}`, operation, p, false);
})
.join('\n');
}
const hasOperationName = root && options?.operationName ? ' ' + options.operationName : '';
const hasVariables = root && options?.variables?.$params ? `(${options.variables?.$params})` : '';
Expand Down