@@ -78,54 +78,59 @@ function convertQuery(node: any, level: number, output: Array<[ string, number ]
78
78
Object . keys ( node )
79
79
. filter ( ( key ) => filterNonConfigFields ( key , options . ignoreFields ) )
80
80
. forEach ( ( key ) => {
81
- if ( typeof node [ key ] === 'object' ) {
82
- const fieldCount = Object . keys ( node [ key ] )
83
- . filter ( ( keyCount ) => filterNonConfigFields ( keyCount , options . ignoreFields ) ) . length ;
84
- const subFields = fieldCount > 0 ;
85
- const argsExist = typeof node [ key ] . __args === 'object' ;
86
- const directivesExist = typeof node [ key ] . __directives === 'object' ;
87
- let token = `${ key } ` ;
88
- if ( typeof node [ key ] . __aliasFor === 'string' ) {
89
- token = `${ token } : ${ node [ key ] . __aliasFor } ` ;
90
- }
91
- if ( typeof node [ key ] . __variables === 'object' ) {
92
- token = `${ token } (${ buildVariables ( node [ key ] . __variables ) } )` ;
93
- }
94
- else if ( argsExist || directivesExist ) {
95
- let argsStr : string ;
96
- let dirsStr : string ;
97
- if ( directivesExist ) {
98
- // TODO: Add support for multiple directives on one node. Then condense blocks into terniary lines.
99
- // const directives = Object.keys(node[key].__directives);
100
- const numDirectives = Object . keys ( node [ key ] . __directives ) . length ;
101
- if ( numDirectives > 1 ) {
102
- throw new Error ( `Too many directives. The object/key ` +
103
- `'${ Object . keys ( node [ key ] ) [ 0 ] } ' had ${ numDirectives } directives, ` +
104
- `but only 1 directive per object/key is supported at this time.` ) ;
81
+
82
+ if ( typeof node [ key ] === 'object' ) {
83
+
84
+ const fieldCount = Object . keys ( node [ key ] )
85
+ . filter ( ( keyCount ) => filterNonConfigFields ( keyCount , options . ignoreFields ) ) . length ;
86
+ const subFields = fieldCount > 0 ;
87
+ const argsExist = typeof node [ key ] . __args === 'object' ;
88
+ const directivesExist = typeof node [ key ] . __directives === 'object' ;
89
+
90
+ let token = `${ key } ` ;
91
+
92
+ if ( typeof node [ key ] . __aliasFor === 'string' ) {
93
+ token = `${ token } : ${ node [ key ] . __aliasFor } ` ;
94
+ }
95
+
96
+ if ( typeof node [ key ] . __variables === 'object' ) {
97
+ token = `${ token } (${ buildVariables ( node [ key ] . __variables ) } )` ;
98
+ }
99
+ else if ( argsExist || directivesExist ) {
100
+ let argsStr : string ;
101
+ let dirsStr : string ;
102
+ if ( directivesExist ) {
103
+ // TODO: Add support for multiple directives on one node.
104
+ const numDirectives = Object . keys ( node [ key ] . __directives ) . length ;
105
+ if ( numDirectives > 1 ) {
106
+ throw new Error ( `Too many directives. The object/key ` +
107
+ `'${ Object . keys ( node [ key ] ) [ 0 ] } ' had ${ numDirectives } directives, ` +
108
+ `but only 1 directive per object/key is supported at this time.` ) ;
109
+ }
110
+ dirsStr = `@${ buildDirectives ( node [ key ] . __directives ) } ` ;
111
+ }
112
+ if ( argsExist ) {
113
+ argsStr = `(${ buildArgs ( node [ key ] . __args ) } )` ;
105
114
}
106
- // directives.map(((x) => { dirsStr += ` @${buildDirectives(node[key].__directives)}`; })) ;
107
- dirsStr = `@ ${ buildDirectives ( node [ key ] . __directives ) } ` ;
115
+ const spacer = directivesExist && argsExist ? ' ' : '' ;
116
+ token = `${ token } ${ dirsStr ? dirsStr : '' } ${ spacer } ${ argsStr ? argsStr : '' } ` ;
108
117
}
109
- if ( argsExist ) {
110
- argsStr = `(${ buildArgs ( node [ key ] . __args ) } )` ;
118
+
119
+ // DEPRECATED: Should be removed in version 2.0.0
120
+ if ( typeof node [ key ] . __alias === 'string' ) {
121
+ token = `${ node [ key ] . __alias } : ${ token } ` ;
111
122
}
112
- const spacer = directivesExist && argsExist ? ' ' : '' ;
113
- token = `${ token } ${ dirsStr ? dirsStr : '' } ${ spacer } ${ argsStr ? argsStr : '' } ` ;
114
- }
115
- //Should be removed in version 2.0.0
116
- if ( typeof node [ key ] . __alias === 'string' ) {
117
- token = `${ node [ key ] . __alias } : ${ token } ` ;
118
- }
119
123
120
- output . push ( [ token + ( fieldCount > 0 ? ' {' : '' ) , level ] ) ;
121
- convertQuery ( node [ key ] , level + 1 , output , options ) ;
124
+ output . push ( [ token + ( fieldCount > 0 ? ' {' : '' ) , level ] ) ;
125
+ convertQuery ( node [ key ] , level + 1 , output , options ) ;
126
+
127
+ if ( subFields ) {
128
+ output . push ( [ '}' , level ] ) ;
129
+ }
122
130
123
- if ( subFields ) {
124
- output . push ( [ '}' , level ] ) ;
131
+ } else if ( node [ key ] ) {
132
+ output . push ( [ ` ${ key } ` , level ] ) ;
125
133
}
126
- } else if ( node [ key ] ) {
127
- output . push ( [ `${ key } ` , level ] ) ;
128
- }
129
134
} ) ;
130
135
}
131
136
0 commit comments