Skip to content

Commit f512027

Browse files
authored
Merge pull request #12 from jeniffer9/master
adds aliasFor
2 parents d69235a + 0f1ee14 commit f512027

File tree

2 files changed

+44
-20
lines changed

2 files changed

+44
-20
lines changed

src/__tests__/jsonToGraphQLQuery.tests.ts

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -344,20 +344,44 @@ describe('jsonToGraphQL()', () => {
344344
});
345345

346346
it('uses aliases for fields', () => {
347-
const query = {
348-
query: {
349-
Posts: {
350-
__alias: 'lorem',
351-
__args: {
352-
arg1: 20,
347+
const query = {
348+
query: {
349+
Posts: {
350+
__alias: 'lorem',
351+
__args: {
352+
arg1: 20,
353+
},
354+
id: true
355+
}
356+
}
357+
};
358+
expect(jsonToGraphQLQuery(query)).to.equal(
359+
'query { lorem: Posts (arg1: 20) { id } }'
360+
);
361+
});
362+
363+
it('supports multiple aliases for one field', () => {
364+
const query = {
365+
query: {
366+
lorem: {
367+
__aliasFor: 'Posts',
368+
__args: {
369+
arg1: 20,
370+
},
371+
id: true
353372
},
354-
id: true
373+
larem: {
374+
__aliasFor: 'Posts',
375+
__args: {
376+
arg2: 10,
377+
},
378+
id: true
379+
}
355380
}
356-
}
357-
};
358-
expect(jsonToGraphQLQuery(query)).to.equal(
359-
'query { lorem: Posts (arg1: 20) { id } }'
360-
);
381+
};
382+
expect(jsonToGraphQLQuery(query)).to.equal(
383+
'query { lorem: Posts (arg1: 20) { id } larem: Posts (arg2: 10) { id } }'
384+
);
361385
});
362386

363387
it('does not include fields which value is false', () => {

src/jsonToGraphQLQuery.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { EnumType } from './types/EnumType';
22
import { VariableType } from './types/VariableType';
33

4-
export const configFields = ['__args', '__alias', '__variables', '__directives'];
4+
export const configFields = ['__args', '__alias', '__aliasFor', '__variables', '__directives'];
55

66
function stringify(obj_from_json: any): string {
77
if (obj_from_json instanceof EnumType) {
@@ -84,9 +84,12 @@ function convertQuery(node: any, level: number, output: Array<[ string, number ]
8484
const subFields = fieldCount > 0;
8585
const argsExist = typeof node[key].__args === 'object';
8686
const directivesExist = typeof node[key].__directives === 'object';
87-
let token: string;
87+
let token = `${key}`;
88+
if (typeof node[key].__aliasFor === 'string') {
89+
token = `${token}: ${node[key].__aliasFor}`;
90+
}
8891
if (typeof node[key].__variables === 'object') {
89-
token = `${key} (${buildVariables(node[key].__variables)})`;
92+
token = `${token} (${buildVariables(node[key].__variables)})`;
9093
}
9194
else if (argsExist || directivesExist) {
9295
let argsStr: string;
@@ -107,12 +110,9 @@ function convertQuery(node: any, level: number, output: Array<[ string, number ]
107110
argsStr = `(${buildArgs(node[key].__args)})`;
108111
}
109112
const spacer = directivesExist && argsExist ? ' ' : '';
110-
token = `${key} ${dirsStr ? dirsStr : ''}${spacer}${argsStr ? argsStr : ''}`;
111-
}
112-
else {
113-
token = `${key}`;
113+
token = `${token} ${dirsStr ? dirsStr : ''}${spacer}${argsStr ? argsStr : ''}`;
114114
}
115-
115+
//Should be removed in version 2.0.0
116116
if (typeof node[key].__alias === 'string') {
117117
token = `${node[key].__alias}: ${token}`;
118118
}

0 commit comments

Comments
 (0)