@@ -32,38 +32,42 @@ function getIndent(level: number): string {
32
32
return Array ( ( level * 4 ) + 1 ) . join ( ' ' ) ;
33
33
}
34
34
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
+ }
42
38
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
51
45
52
- if ( typeof node [ key ] . __alias == 'string' ) {
53
- token = `${ node [ key ] . __alias } :${ token } ` ;
54
- }
46
+ let token : string
47
+ let subFields : boolean
55
48
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
65
55
}
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
+ } )
67
71
}
68
72
69
73
export interface IJsonToGraphQLOptions {
0 commit comments