Skip to content

Commit 7335915

Browse files
author
Jeniffer Lima Graf
committed
support multiple inline fragments
1 parent 3994934 commit 7335915

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/__tests__/jsonToGraphQLQuery.tests.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,27 @@ describe('jsonToGraphQL()', () => {
464464
);
465465
});
466466

467+
it('supports multiple inline fragments', () => {
468+
const query = {
469+
query: {
470+
Posts: {
471+
__on: [
472+
{
473+
__fragmentName: "ConfigurablePost",
474+
id: true
475+
},
476+
{
477+
__fragmentName: "UnconfigurablePost",
478+
name: true
479+
}]
480+
}
481+
}
482+
};
483+
expect(jsonToGraphQLQuery(query)).to.equal(
484+
'query { Posts { ... on ConfigurablePost { id } ... on UnconfigurablePost { name } } }'
485+
);
486+
});
487+
467488
it('we can ignore apollo __typename keys', () => {
468489
const query = {
469490
query: {

src/jsonToGraphQLQuery.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,13 @@ function convertQuery(node: any, level: number, output: Array<[ string, number ]
126126
convertQuery(node[key], level + 1, output, options);
127127

128128
if (inlineFragmentsExist) {
129-
let inlineFragment = node[key].__on;
130-
if (inlineFragment && inlineFragment.__fragmentName) {
131-
output.push([`... on ${inlineFragment.__fragmentName} {`, level + 1]);
129+
let inlineFragments: Array<{__fragmentName: string}> = node[key].__on instanceof Array ? node[key].__on : [node[key].__on];
130+
inlineFragments.forEach((inlineFragment) => {
131+
let name = inlineFragment.__fragmentName;
132+
output.push([`... on ${name} {`, level + 1]);
132133
convertQuery(inlineFragment, level + 2, output, options);
133134
output.push([ '}', level + 1 ]);
134-
}
135+
});
135136
}
136137

137138
if (subFields || inlineFragmentsExist) {

0 commit comments

Comments
 (0)