Skip to content

Commit 3fa613a

Browse files
committed
Merge changes from master
2 parents 0bf175f + 30f3657 commit 3fa613a

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

src/__tests__/jsonToGraphQLQuery.tests.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,4 +368,31 @@ describe('jsonToGraphQL()', () => {
368368
'query { Posts (a: false) { id } Lorem { id } }'
369369
);
370370
});
371+
372+
it('ignores a field that exists in the initial object', () => {
373+
const query = {
374+
query: {
375+
Posts: {
376+
thisShouldBeIgnored: {
377+
test: 'a value'
378+
},
379+
id: true,
380+
title: true,
381+
post_date: true
382+
}
383+
}
384+
};
385+
expect(jsonToGraphQLQuery(query, {
386+
pretty: true,
387+
ignoreFields: ['thisShouldBeIgnored']
388+
})).to.equal(
389+
`query {
390+
Posts {
391+
id
392+
title
393+
post_date
394+
}
395+
}`);
396+
});
397+
371398
});

src/jsonToGraphQLQuery.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,18 @@ function getIndent(level: number): string {
6969
return Array((level * 4) + 1).join(' ');
7070
}
7171

72-
export function filterNonConfigFields(fieldName: string) {
72+
function filterNonConfigFields(fieldName: string, ignoreFields: string[]) {
7373
// Returns true if fieldName is not a 'configField'.
74-
return configFields.indexOf(fieldName) === -1; // Equivalent to: return !configFields.includes(fieldName);
74+
return configFields.indexOf(fieldName) == -1 && ignoreFields.indexOf(fieldName) == -1;
7575
}
7676

77-
function convertQuery(node: any, level: number, output: Array<[ string, number ]>) {
77+
function convertQuery(node: any, level: number, output: Array<[ string, number ]>, options: IJsonToGraphQLOptions) {
7878
Object.keys(node)
79-
.filter(filterNonConfigFields)
79+
.filter((key) => filterNonConfigFields(key, options.ignoreFields))
8080
.forEach((key) => {
8181
if (typeof node[key] === 'object') {
82-
const fieldCount = Object.keys(node[key]).filter(filterNonConfigFields).length;
82+
const fieldCount = Object.keys(node[key])
83+
.filter((keyCount) => filterNonConfigFields(keyCount, options.ignoreFields)).length;
8384
const subFields = fieldCount > 0;
8485
let token: string;
8586

@@ -108,7 +109,7 @@ function convertQuery(node: any, level: number, output: Array<[ string, number ]
108109
}
109110

110111
output.push([ token + (fieldCount > 0 ? ' {' : ''), level ]);
111-
convertQuery(node[key], level + 1, output);
112+
convertQuery(node[key], level + 1, output, options);
112113

113114
if (subFields) {
114115
output.push([ '}', level ]);
@@ -121,6 +122,7 @@ function convertQuery(node: any, level: number, output: Array<[ string, number ]
121122

122123
export interface IJsonToGraphQLOptions {
123124
pretty?: boolean;
125+
ignoreFields?: string[];
124126
}
125127

126128
export function jsonToGraphQLQuery(query: any, options: IJsonToGraphQLOptions = {}) {
@@ -130,9 +132,12 @@ export function jsonToGraphQLQuery(query: any, options: IJsonToGraphQLOptions =
130132
if (Object.keys(query).length == 0) {
131133
throw new Error('query object has no data');
132134
}
135+
if (!(options.ignoreFields instanceof Array)) {
136+
options.ignoreFields = [];
137+
}
133138

134139
const queryLines: Array<[string, number]> = [];
135-
convertQuery(query, 0, queryLines);
140+
convertQuery(query, 0, queryLines, options);
136141

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

0 commit comments

Comments
 (0)