Skip to content

Commit a8ccf93

Browse files
committed
ignoreFields option
1 parent 811bb2d commit a8ccf93

File tree

3 files changed

+72
-10
lines changed

3 files changed

+72
-10
lines changed

README.md

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ query {
6060
}
6161
```
6262

63-
### Query with arguments
63+
### Query with arguments
6464

6565
```typescript
6666
import { jsonToGraphQLQuery } from 'json-to-graphql-query';
@@ -200,7 +200,7 @@ query {
200200
}
201201
```
202202

203-
### Query with Enum Values
203+
### Query with Enum Values
204204

205205
```typescript
206206
import { jsonToGraphQLQuery, EnumType } from 'json-to-graphql-query';
@@ -231,7 +231,7 @@ query {
231231
}
232232
```
233233

234-
### Query with variables
234+
### Query with variables
235235

236236
```typescript
237237
import { jsonToGraphQLQuery, VariableType } from 'json-to-graphql-query';
@@ -266,6 +266,41 @@ query ($variable1: String!, $variableWithDefault: String = "default_value") {
266266
}
267267
```
268268

269+
### Ignore fields of the initial object
270+
We sometimes want to ignore specific fields in the initial object, for instance __typename in Apollo queries.
271+
While we can put the field value to `false`, we might want to keep its value.
272+
You can specify those fields not to be put into the final GraphQL query though the `ignoreFields` option:
273+
274+
```typescript
275+
import { jsonToGraphQLQuery, VariableType } from 'json-to-graphql-query';
276+
277+
const query = {
278+
query: {
279+
Posts: {
280+
__ignore: {
281+
variable1: 'a value'
282+
},
283+
id: true,
284+
title: true,
285+
post_date: true
286+
}
287+
}
288+
};
289+
const graphql_query = jsonToGraphQLQuery(query, { pretty: true, ignoreFields: ['__ignore'] });
290+
```
291+
292+
Resulting `graphql_query`
293+
294+
```graphql
295+
query {
296+
Posts {
297+
id
298+
title
299+
post_date
300+
}
301+
}
302+
```
303+
269304
## TO-DO List
270305

271306
* Support Named Queries / Mutations

src/__tests__/jsonToGraphQLQuery.tests.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,4 +281,28 @@ describe('jsonToGraphQL()', () => {
281281
'query { Posts (a: false) { id } Lorem { id } }'
282282
);
283283
});
284+
285+
it('ignores a field that exists in the initial object', () => {
286+
const query = {
287+
query: {
288+
Posts: {
289+
__ignore: {
290+
test: 'a value'
291+
},
292+
id: true,
293+
title: true,
294+
post_date: true
295+
}
296+
}
297+
};
298+
expect(jsonToGraphQLQuery(query, { pretty: true, ignoreFields: ['__ignore'] })).to.equal(
299+
`query {
300+
Posts {
301+
id
302+
title
303+
post_date
304+
}
305+
}`);
306+
});
307+
284308
});

src/jsonToGraphQLQuery.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,18 @@ function getIndent(level: number): string {
4747
return Array((level * 4) + 1).join(' ');
4848
}
4949

50-
function filterNonConfigFields(fieldName: string) {
51-
return fieldName !== '__args' && fieldName !== '__alias' && fieldName !== '__variables';
50+
function filterNonConfigFields(fieldName: string, ignoreFields: string[]) {
51+
return fieldName !== '__args' && fieldName !== '__alias' && fieldName !== '__variables'
52+
&& ignoreFields.indexOf(fieldName) == -1;
5253
}
5354

54-
function convertQuery(node: any, level: number, output: Array<[ string, number ]>) {
55+
function convertQuery(node: any, level: number, ignoreFields: string[], output: Array<[ string, number ]>) {
5556
Object.keys(node)
56-
.filter(filterNonConfigFields)
57+
.filter((key) => filterNonConfigFields(key, ignoreFields))
5758
.forEach((key) => {
5859
if (typeof node[key] === 'object') {
59-
const fieldCount = Object.keys(node[key]).filter(filterNonConfigFields).length;
60+
const fieldCount = Object.keys(node[key])
61+
.filter((keyCount) => filterNonConfigFields(keyCount, ignoreFields)).length;
6062
const subFields = fieldCount > 0;
6163
let token: string;
6264

@@ -75,7 +77,7 @@ function convertQuery(node: any, level: number, output: Array<[ string, number ]
7577
}
7678

7779
output.push([ token + (fieldCount > 0 ? ' {' : ''), level ]);
78-
convertQuery(node[key], level + 1, output);
80+
convertQuery(node[key], level + 1, ignoreFields, output);
7981

8082
if (subFields) {
8183
output.push([ '}', level ]);
@@ -88,6 +90,7 @@ function convertQuery(node: any, level: number, output: Array<[ string, number ]
8890

8991
export interface IJsonToGraphQLOptions {
9092
pretty?: boolean;
93+
ignoreFields?: string[];
9194
}
9295

9396
export function jsonToGraphQLQuery(query: any, options: IJsonToGraphQLOptions = {}) {
@@ -99,7 +102,7 @@ export function jsonToGraphQLQuery(query: any, options: IJsonToGraphQLOptions =
99102
}
100103

101104
const queryLines: Array<[string, number]> = [];
102-
convertQuery(query, 0, queryLines);
105+
convertQuery(query, 0, options.ignoreFields || [], queryLines);
103106

104107
let output = '';
105108
queryLines.forEach(([line, level]) => {

0 commit comments

Comments
 (0)