@@ -80,56 +80,65 @@ function convertQuery(node: any, level: number, output: Array<[string, number]>,
80
80
Object . keys ( node )
81
81
. filter ( ( key ) => filterNonConfigFields ( key , options . ignoreFields ) )
82
82
. forEach ( ( key ) => {
83
+ let value = node [ key ]
84
+ if ( typeof value === 'object' ) {
85
+ if ( Array . isArray ( value ) ) {
86
+ if ( value [ 0 ] && typeof value [ 0 ] === 'object' ) {
87
+ value = value [ 0 ]
88
+ }
89
+ else {
90
+ output . push ( [ `${ key } ` , level ] )
91
+ return
92
+ }
93
+ }
83
94
84
- if ( typeof node [ key ] === 'object' ) {
85
-
86
- const fieldCount = Object . keys ( node [ key ] )
95
+ const fieldCount = Object . keys ( value )
87
96
. filter ( ( keyCount ) => filterNonConfigFields ( keyCount , options . ignoreFields ) ) . length ;
88
97
const subFields = fieldCount > 0 ;
89
- const argsExist = typeof node [ key ] . __args === 'object' ;
90
- const directivesExist = typeof node [ key ] . __directives === 'object' ;
91
- const inlineFragmentsExist = typeof node [ key ] . __on === 'object' ;
98
+ const argsExist = typeof value . __args === 'object' ;
99
+ const directivesExist = typeof value . __directives === 'object' ;
100
+ const inlineFragmentsExist = typeof value . __on === 'object' ;
92
101
93
102
let token = `${ key } ` ;
94
103
95
- if ( typeof node [ key ] . __aliasFor === 'string' ) {
96
- token = `${ token } : ${ node [ key ] . __aliasFor } ` ;
104
+ if ( typeof value . __aliasFor === 'string' ) {
105
+ token = `${ token } : ${ value . __aliasFor } ` ;
97
106
}
98
107
99
- if ( typeof node [ key ] . __variables === 'object' ) {
100
- token = `${ token } (${ buildVariables ( node [ key ] . __variables ) } )` ;
108
+ if ( typeof value . __variables === 'object' ) {
109
+ token = `${ token } (${ buildVariables ( value . __variables ) } )` ;
101
110
}
102
111
else if ( argsExist || directivesExist ) {
103
112
let argsStr : string ;
104
113
let dirsStr : string ;
105
114
if ( directivesExist ) {
106
115
// TODO: Add support for multiple directives on one node.
107
- const numDirectives = Object . keys ( node [ key ] . __directives ) . length ;
116
+ const numDirectives = Object . keys ( value . __directives ) . length ;
108
117
if ( numDirectives > 1 ) {
109
118
throw new Error ( `Too many directives. The object/key ` +
110
- `'${ Object . keys ( node [ key ] ) [ 0 ] } ' had ${ numDirectives } directives, ` +
119
+ `'${ Object . keys ( value ) [ 0 ] } ' had ${ numDirectives } directives, ` +
111
120
`but only 1 directive per object/key is supported at this time.` ) ;
112
121
}
113
- dirsStr = `@${ buildDirectives ( node [ key ] . __directives ) } ` ;
122
+ dirsStr = `@${ buildDirectives ( value . __directives ) } ` ;
114
123
}
115
124
if ( argsExist ) {
116
- argsStr = `(${ buildArgs ( node [ key ] . __args ) } )` ;
125
+ argsStr = `(${ buildArgs ( value . __args ) } )` ;
117
126
}
118
127
const spacer = directivesExist && argsExist ? ' ' : '' ;
119
128
token = `${ token } ${ dirsStr ? dirsStr : '' } ${ spacer } ${ argsStr ? argsStr : '' } ` ;
120
129
}
121
130
122
131
// DEPRECATED: Should be removed in version 2.0.0
123
- if ( typeof node [ key ] . __alias === 'string' ) {
124
- token = `${ node [ key ] . __alias } : ${ token } ` ;
132
+ if ( typeof value . __alias === 'string' ) {
133
+ token = `${ value . __alias } : ${ token } ` ;
125
134
}
126
135
127
136
output . push ( [ token + ( subFields || inlineFragmentsExist ? ' {' : '' ) , level ] ) ;
128
- convertQuery ( node [ key ] , level + 1 , output , options ) ;
137
+ convertQuery ( value , level + 1 , output , options ) ;
129
138
130
139
if ( inlineFragmentsExist ) {
131
140
const inlineFragments : Array < { __typeName : string } >
132
- = node [ key ] . __on instanceof Array ? node [ key ] . __on : [ node [ key ] . __on ] ;
141
+ = value . __on instanceof Array ? value . __on : [ value . __on ] ;
133
142
inlineFragments . forEach ( ( inlineFragment ) => {
134
143
const name = inlineFragment . __typeName ;
135
144
output . push ( [ `... on ${ name } {` , level + 1 ] ) ;
@@ -142,7 +151,7 @@ function convertQuery(node: any, level: number, output: Array<[string, number]>,
142
151
output . push ( [ '}' , level ] ) ;
143
152
}
144
153
145
- } else if ( options . includeFalsyKeys === true || node [ key ] ) {
154
+ } else if ( options . includeFalsyKeys === true || value ) {
146
155
output . push ( [ `${ key } ` , level ] ) ;
147
156
}
148
157
} ) ;
0 commit comments