Skip to content

Commit 9acad1a

Browse files
committed
Tweak to support multiple "full" fragments
1 parent f180b57 commit 9acad1a

File tree

4 files changed

+53
-14
lines changed

4 files changed

+53
-14
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,5 @@
4040

4141
## 2.0.0
4242

43-
* Update to TypeScript 3.7
43+
* Update to TypeScript 3.7
44+
* Support for full inline fragments (thanks @ConnorWhite)

README.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Supported Options:
3333
* Support for variables via [`__variables`](#query-with-variables)
3434
* Support for simple directives (such as `@client`) via [`__directives`](#query-with-directives)
3535
* Support for one or more inline fragments via [`__on.__typeName`](#query-with-inline-fragments)
36+
* Support for full fragments via [`__all_on`](#query-with-inline-fragments)
3637

3738
## Recent Changes
3839

@@ -347,13 +348,46 @@ query {
347348

348349
### Query with Inline Fragments
349350

351+
Full inline fragments
352+
350353
```typescript
351354
import { jsonToGraphQLQuery } from 'json-to-graphql-query';
352355

353356
const query = {
354357
query: {
355358
Posts: {
356-
title: true
359+
title: true,
360+
__all_on: [
361+
"ConfigurablePost",
362+
"PageInfo"
363+
]
364+
}
365+
}
366+
};
367+
const graphql_query = jsonToGraphQLQuery(query, { pretty: true });
368+
```
369+
370+
Resulting `graphql_query`
371+
372+
```graphql
373+
query {
374+
Posts {
375+
title
376+
...ConfigurablePost
377+
...PageInfo
378+
}
379+
}
380+
```
381+
382+
Partial inline fragments
383+
384+
```typescript
385+
import { jsonToGraphQLQuery } from 'json-to-graphql-query';
386+
387+
const query = {
388+
query: {
389+
Posts: {
390+
title: true,
357391
__on: {
358392
__typeName: "ConfigurablePost",
359393
id: true

src/__tests__/fragments.tests.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,15 @@ describe('jsonToGraphQLQuery() - fragments', () => {
6262
const query = {
6363
query: {
6464
Posts: {
65-
__on: 'ConfigurablePost'
65+
__all_on: [
66+
'ConfigurablePost',
67+
'PageInfo'
68+
]
6669
}
6770
}
6871
};
6972
expect(jsonToGraphQLQuery(query)).to.equal(
70-
'query { Posts { ... ConfigurablePost } }'
73+
'query { Posts { ...ConfigurablePost ...PageInfo } }'
7174
);
7275
});
7376

src/jsonToGraphQLQuery.ts

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

44
export const configFields = [
5-
'__args', '__alias', '__aliasFor', '__variables', '__directives', '__on', '__typeName'
5+
'__args', '__alias', '__aliasFor', '__variables', '__directives', '__on', '__all_on', '__typeName'
66
];
77

88
function stringify(obj_from_json: any): string {
@@ -95,7 +95,8 @@ function convertQuery(node: any, level: number, output: [string, number][], opti
9595
const subFields = fieldCount > 0;
9696
const argsExist = typeof value.__args === 'object';
9797
const directivesExist = typeof value.__directives === 'object';
98-
const inlineFragmentsExist = typeof value.__on === 'object' || typeof value.__on === 'string';
98+
const fullFragmentsExist = value.__all_on instanceof Array;
99+
const partialFragmentsExist = typeof value.__on === 'object';
99100

100101
let token = `${key}`;
101102

@@ -131,11 +132,15 @@ function convertQuery(node: any, level: number, output: [string, number][], opti
131132
token = `${value.__alias}: ${token}`;
132133
}
133134

134-
output.push([token + (subFields || inlineFragmentsExist ? ' {' : ''), level]);
135+
output.push([token + (subFields || partialFragmentsExist || fullFragmentsExist ? ' {' : ''), level]);
135136
convertQuery(value, level + 1, output, options);
136137

137-
if (inlineFragmentsExist) {
138-
if (typeof value.__on === 'object') {
138+
if (fullFragmentsExist) {
139+
value.__all_on.forEach((fullFragment: string) => {
140+
output.push([`...${fullFragment}`, level + 1]);
141+
});
142+
}
143+
if (partialFragmentsExist) {
139144
const inlineFragments: { __typeName: string }[]
140145
= value.__on instanceof Array ? value.__on : [value.__on];
141146
inlineFragments.forEach((inlineFragment) => {
@@ -144,13 +149,9 @@ function convertQuery(node: any, level: number, output: [string, number][], opti
144149
convertQuery(inlineFragment, level + 2, output, options);
145150
output.push(['}', level + 1]);
146151
});
147-
} else if (typeof value.__on === 'string') {
148-
const inlineFragment: string = value.__on;
149-
output.push([`... ${inlineFragment}`, level + 1]);
150-
}
151152
}
152153

153-
if (subFields || inlineFragmentsExist) {
154+
if (subFields || partialFragmentsExist || fullFragmentsExist) {
154155
output.push(['}', level]);
155156
}
156157

0 commit comments

Comments
 (0)