Skip to content

Commit e72e38d

Browse files
fixed: mutations with args but no selected fields
1 parent 06dbb61 commit e72e38d

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

src/jsonToGraphQLQuery.ts

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,38 +32,42 @@ function getIndent(level: number): string {
3232
return Array((level * 4) + 1).join(' ');
3333
}
3434

35-
function convertQuery(node: any, level: number, output: Array<[string, number]>) {
36-
for (const key in node) {
37-
if (key != '__args' && key != '__alias') {
38-
if (typeof node[key] == 'object') {
39-
const fieldCount = Object.keys(node[key]).length;
40-
let token: string;
41-
let subFields: boolean;
35+
function filterNonConfigFields(it) {
36+
return it !== '__args' && it !== '__alias'
37+
}
4238

43-
if (typeof node[key].__args == 'object') {
44-
token = `${key} (${buildArgs(node[key].__args)})`;
45-
subFields = fieldCount > 1;
46-
}
47-
else {
48-
token = `${key}`;
49-
subFields = fieldCount > 0;
50-
}
39+
function convertQuery(node: any, level: number, output: Array<[ string, number ]>) {
40+
Object.keys(node)
41+
.filter(filterNonConfigFields)
42+
.forEach(key => {
43+
if (typeof node[ key ] === 'object') {
44+
const fieldCount = Object.keys(node[ key ]).filter(filterNonConfigFields).length
5145

52-
if (typeof node[key].__alias == 'string') {
53-
token = `${node[key].__alias}:${token}`;
54-
}
46+
let token: string
47+
let subFields: boolean
5548

56-
output.push([token + (subFields ? ' {' : ''), level]);
57-
convertQuery(node[key], level + 1, output);
58-
if (subFields) {
59-
output.push(['}', level]);
60-
}
61-
}
62-
else if (node[key] !== false) {
63-
output.push([`${key}`, level]);
64-
}
49+
if (typeof node[ key ].__args === 'object') {
50+
token = `${key} (${buildArgs(node[ key ].__args)})`
51+
subFields = fieldCount > 1
52+
} else {
53+
token = `${key}`
54+
subFields = fieldCount > 0
6555
}
66-
}
56+
57+
if (typeof node[ key ].__alias === 'string') {
58+
token = `${node[ key ].__alias}:${token}`
59+
}
60+
61+
output.push([ token + (subFields ? ' {' : ''), level ])
62+
convertQuery(node[ key ], level + 1, output)
63+
64+
if (subFields) {
65+
output.push([ '}', level ])
66+
}
67+
} else if (node[ key ]) {
68+
output.push([ `${key}`, level ])
69+
}
70+
})
6771
}
6872

6973
export interface IJsonToGraphQLOptions {

0 commit comments

Comments
 (0)