@@ -52,13 +52,13 @@ function filterNonConfigFields(fieldName: string, ignoreFields: string[]) {
52
52
&& ignoreFields . indexOf ( fieldName ) == - 1 ;
53
53
}
54
54
55
- function convertQuery ( node : any , level : number , ignoreFields : string [ ] , output : Array < [ string , number ] > ) {
55
+ function convertQuery ( node : any , level : number , output : Array < [ string , number ] > , options : IJsonToGraphQLOptions ) {
56
56
Object . keys ( node )
57
- . filter ( ( key ) => filterNonConfigFields ( key , ignoreFields ) )
57
+ . filter ( ( key ) => filterNonConfigFields ( key , options . ignoreFields ) )
58
58
. forEach ( ( key ) => {
59
59
if ( typeof node [ key ] === 'object' ) {
60
60
const fieldCount = Object . keys ( node [ key ] )
61
- . filter ( ( keyCount ) => filterNonConfigFields ( keyCount , ignoreFields ) ) . length ;
61
+ . filter ( ( keyCount ) => filterNonConfigFields ( keyCount , options . ignoreFields ) ) . length ;
62
62
const subFields = fieldCount > 0 ;
63
63
let token : string ;
64
64
@@ -77,7 +77,7 @@ function convertQuery(node: any, level: number, ignoreFields: string[], output:
77
77
}
78
78
79
79
output . push ( [ token + ( fieldCount > 0 ? ' {' : '' ) , level ] ) ;
80
- convertQuery ( node [ key ] , level + 1 , ignoreFields , output ) ;
80
+ convertQuery ( node [ key ] , level + 1 , output , options ) ;
81
81
82
82
if ( subFields ) {
83
83
output . push ( [ '}' , level ] ) ;
@@ -100,9 +100,12 @@ export function jsonToGraphQLQuery(query: any, options: IJsonToGraphQLOptions =
100
100
if ( Object . keys ( query ) . length == 0 ) {
101
101
throw new Error ( 'query object has no data' ) ;
102
102
}
103
+ if ( ! ( options . ignoreFields instanceof Array ) ) {
104
+ options . ignoreFields = [ ] ;
105
+ }
103
106
104
107
const queryLines : Array < [ string , number ] > = [ ] ;
105
- convertQuery ( query , 0 , options . ignoreFields || [ ] , queryLines ) ;
108
+ convertQuery ( query , 0 , queryLines , options ) ;
106
109
107
110
let output = '' ;
108
111
queryLines . forEach ( ( [ line , level ] ) => {
0 commit comments