@@ -38,7 +38,10 @@ export default class QueryBuilder {
38
38
const context = Context . getInstance ( ) ;
39
39
model = context . getModel ( model ) ;
40
40
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 ) ;
42
45
path = path . length === 0 ? [ model . singularName ] : path ;
43
46
44
47
const fields = `
@@ -47,7 +50,7 @@ export default class QueryBuilder {
47
50
` ;
48
51
49
52
if ( multiple ) {
50
- const header : string = `${ name ? name : model . pluralName } ${ params } ` ;
53
+ const header : string = `${ name } ${ params } ` ;
51
54
52
55
if ( context . connectionQueryMode === 'nodes' ) {
53
56
return `
@@ -118,9 +121,12 @@ export default class QueryBuilder {
118
121
// name
119
122
if ( ! name ) name = ( multiple ? model . pluralName : model . singularName ) ;
120
123
124
+ // field
125
+ const field = context . schema ! . getMutation ( name , true ) || context . schema ! . getQuery ( name , true ) ;
126
+
121
127
// build query
122
128
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` +
124
130
` ${ this . buildField ( model , multiple , args , [ ] , name , filter , true ) } \n` +
125
131
`}` ;
126
132
@@ -149,10 +155,11 @@ export default class QueryBuilder {
149
155
* @param {boolean } signature When true, then this method generates a query signature instead of key/value pairs
150
156
* @param filter
151
157
* @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
152
159
* @returns {String }
153
160
*/
154
161
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 {
156
163
if ( args === undefined ) return '' ;
157
164
158
165
let returnValue : string = '' ;
@@ -165,18 +172,23 @@ export default class QueryBuilder {
165
172
const isForeignKey = model . skipField ( key ) ;
166
173
const skipFieldDueId = ( key === 'id' || isForeignKey ) && ! allowIdFields ;
167
174
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
+
168
180
// Ignore null fields, ids and connections
169
- if ( value && ! ( value instanceof Array || skipFieldDueId ) ) {
181
+ if ( value && ! skipFieldDueId && ! isConnectionField ) {
170
182
let typeOrValue : any = '' ;
171
183
172
184
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
-
177
185
if ( _ . isObject ( value ) && value . __type ) {
178
186
// Case 2 (User!)
179
187
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 ) + '!' ;
180
192
} else if ( schemaField && Schema . getTypeNameOfField ( schemaField ) ) {
181
193
// Case 1, 3 and 4
182
194
typeOrValue = Schema . getTypeNameOfField ( schemaField ) + '!' ;
0 commit comments