@@ -38,7 +38,10 @@ export default class QueryBuilder {
3838 const context = Context . getInstance ( ) ;
3939 model = context . getModel ( model ) ;
4040
41- let params : string = this . buildArguments ( model , args , false , filter , allowIdFields ) ;
41+ name = name ? name : model . pluralName ;
42+ const field = context . schema ! . getMutation ( name , true ) || context . schema ! . getQuery ( name , true ) ;
43+
44+ let params : string = this . buildArguments ( model , args , false , filter , allowIdFields , field ) ;
4245 path = path . length === 0 ? [ model . singularName ] : path ;
4346
4447 const fields = `
@@ -47,7 +50,7 @@ export default class QueryBuilder {
4750 ` ;
4851
4952 if ( multiple ) {
50- const header : string = `${ name ? name : model . pluralName } ${ params } ` ;
53+ const header : string = `${ name } ${ params } ` ;
5154
5255 if ( context . connectionQueryMode === 'nodes' ) {
5356 return `
@@ -118,9 +121,12 @@ export default class QueryBuilder {
118121 // name
119122 if ( ! name ) name = ( multiple ? model . pluralName : model . singularName ) ;
120123
124+ // field
125+ const field = context . schema ! . getMutation ( name , true ) || context . schema ! . getQuery ( name , true ) ;
126+
121127 // build query
122128 const query : string =
123- `${ type } ${ upcaseFirstLetter ( name ) } ${ this . buildArguments ( model , args , true , filter ) } {\n` +
129+ `${ type } ${ upcaseFirstLetter ( name ) } ${ this . buildArguments ( model , args , true , filter , true , field ) } {\n` +
124130 ` ${ this . buildField ( model , multiple , args , [ ] , name , filter , true ) } \n` +
125131 `}` ;
126132
@@ -149,10 +155,11 @@ export default class QueryBuilder {
149155 * @param {boolean } signature When true, then this method generates a query signature instead of key/value pairs
150156 * @param filter
151157 * @param {boolean } allowIdFields If true, ID fields will be included in the arguments list
158+ * @param {GraphQLField } field Optional. The GraphQL mutation or query field
152159 * @returns {String }
153160 */
154161 private static buildArguments ( model : Model , args ?: Arguments , signature : boolean = false , filter : boolean = false ,
155- allowIdFields : boolean = true ) : string {
162+ allowIdFields : boolean = true , field : GraphQLField | null = null ) : string {
156163 if ( args === undefined ) return '' ;
157164
158165 let returnValue : string = '' ;
@@ -165,18 +172,23 @@ export default class QueryBuilder {
165172 const isForeignKey = model . skipField ( key ) ;
166173 const skipFieldDueId = ( key === 'id' || isForeignKey ) && ! allowIdFields ;
167174
175+ const schema = Context . getInstance ( ) . schema ! ;
176+ const type = schema . getType ( model . singularName + ( filter ? 'Filter' : '' ) ) ;
177+ const schemaField = ( filter ? type . inputFields ! : type . fields ! ) . find ( f => f . name === key ) ;
178+ const isConnectionField = schemaField && Schema . getTypeNameOfField ( schemaField ) . endsWith ( 'TypeConnection' ) ;
179+
168180 // Ignore null fields, ids and connections
169- if ( value && ! ( value instanceof Array || skipFieldDueId ) ) {
181+ if ( value && ! skipFieldDueId && ! isConnectionField ) {
170182 let typeOrValue : any = '' ;
171183
172184 if ( signature ) {
173- const schema = Context . getInstance ( ) . schema ! ;
174- const type = schema . getType ( model . singularName + ( filter ? 'Filter' : '' ) ) ;
175- const schemaField = ( filter ? type . inputFields ! : type . fields ! ) . find ( f => f . name === key ) ;
176-
177185 if ( _ . isObject ( value ) && value . __type ) {
178186 // Case 2 (User!)
179187 typeOrValue = value . __type + 'Input!' ;
188+ } else if ( _ . isArray ( value ) && field ) {
189+ const arg = field . args . find ( f => f . name === key ) ;
190+ if ( ! arg ) throw new Error ( "A argument is of type array but it's not possible to determine the type" ) ;
191+ typeOrValue = Schema . getTypeNameOfField ( arg ) + '!' ;
180192 } else if ( schemaField && Schema . getTypeNameOfField ( schemaField ) ) {
181193 // Case 1, 3 and 4
182194 typeOrValue = Schema . getTypeNameOfField ( schemaField ) + '!' ;
0 commit comments