Skip to content

Commit 0e87e3d

Browse files
author
Jeniffer Lima Graf
committed
adds support for inline fragments
1 parent 857693e commit 0e87e3d

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

src/__tests__/jsonToGraphQLQuery.tests.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,39 @@ describe('jsonToGraphQL()', () => {
431431
}`);
432432
});
433433

434+
it('supports inline fragments', () => {
435+
const query = {
436+
query: {
437+
lorem: {
438+
__on: {
439+
__fragmentName: "subQuery",
440+
id: true
441+
}
442+
},
443+
}
444+
};
445+
expect(jsonToGraphQLQuery(query)).to.equal(
446+
'query { lorem { ... on subQuery { id } } }'
447+
);
448+
});
449+
450+
it('supports inline fragments with subfields on same level', () => {
451+
const query = {
452+
query: {
453+
lorem: {
454+
__on: {
455+
__fragmentName: "subQuery",
456+
id: true
457+
},
458+
title: true
459+
},
460+
}
461+
};
462+
expect(jsonToGraphQLQuery(query)).to.equal(
463+
'query { lorem { title ... on subQuery { id } } }'
464+
);
465+
});
466+
434467
it('we can ignore apollo __typename keys', () => {
435468
const query = {
436469
query: {

src/jsonToGraphQLQuery.ts

Lines changed: 13 additions & 3 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', '__aliasFor', '__variables', '__directives'];
4+
export const configFields = ['__args', '__alias', '__aliasFor', '__variables', '__directives', '__on', '__fragmentName'];
55

66
function stringify(obj_from_json: any): string {
77
if (obj_from_json instanceof EnumType) {
@@ -86,6 +86,7 @@ function convertQuery(node: any, level: number, output: Array<[ string, number ]
8686
const subFields = fieldCount > 0;
8787
const argsExist = typeof node[key].__args === 'object';
8888
const directivesExist = typeof node[key].__directives === 'object';
89+
const inlineFragmentsExist = typeof node[key].__on === 'object';
8990

9091
let token = `${key}`;
9192

@@ -121,10 +122,19 @@ function convertQuery(node: any, level: number, output: Array<[ string, number ]
121122
token = `${node[key].__alias}: ${token}`;
122123
}
123124

124-
output.push([ token + (fieldCount > 0 ? ' {' : ''), level ]);
125+
output.push([ token + (subFields || inlineFragmentsExist ? ' {' : ''), level ]);
125126
convertQuery(node[key], level + 1, output, options);
126127

127-
if (subFields) {
128+
if (inlineFragmentsExist) {
129+
let inlineFragment = node[key].__on;
130+
if (inlineFragment && inlineFragment.__fragmentName) {
131+
output.push([`... on ${inlineFragment.__fragmentName} {`, level + 1]);
132+
convertQuery(inlineFragment, level + 2, output, options);
133+
output.push([ '}', level + 1 ]);
134+
}
135+
}
136+
137+
if (subFields || inlineFragmentsExist) {
128138
output.push([ '}', level ]);
129139
}
130140

0 commit comments

Comments
 (0)