@@ -31,66 +31,6 @@ export default class QueryBuilder {
31
31
return print ( parse ( query ) ) ;
32
32
}
33
33
34
- /**
35
- * Generates the arguments string for a graphql query based on a given map.
36
- *
37
- * There are three types of arguments:
38
- *
39
- * 1) Signatures with simple types (signature = true)
40
- * mutation createUser($name: String!)
41
- *
42
- * 2) Signatures with object types (signature = true, args = { user: { __type: 'User' }})
43
- * mutation createUser($user: UserInput!)
44
- *
45
- * 3) Fields with variables (signature = false, valuesAsVariables = true)
46
- * query user(id: $id)
47
- *
48
- * @param {Arguments | undefined } args
49
- * @param {boolean } signature When true, then this method generates a query signature instead of key/value pairs
50
- * @param {boolean } allowIdFields If true, ID fields will be included in the arguments list
51
- * @returns {String }
52
- */
53
- private buildArguments ( args ?: Arguments , signature : boolean = false , allowIdFields : boolean = true ) : string {
54
- if ( args === null ) return '' ;
55
-
56
- let returnValue : string = '' ;
57
- let first : boolean = true ;
58
-
59
- if ( args ) {
60
- Object . keys ( args ) . forEach ( ( key : string ) => {
61
- let value : any = args [ key ] ;
62
-
63
- // Ignore ids and connections
64
- if ( ! ( value instanceof Array || ( key === 'id' && ! allowIdFields ) ) ) {
65
- let typeOrValue : any = '' ;
66
-
67
- if ( signature ) {
68
- if ( typeof value === 'object' && value . __type ) {
69
- // Case 2 (User!)
70
- typeOrValue = value . __type + 'Input!' ;
71
- } else if ( key === 'id' ) {
72
- // Case 1 (ID!)
73
- typeOrValue = 'ID!' ;
74
- } else {
75
- // Case 1 (String!)
76
- typeOrValue = typeof value === 'number' ? 'Number!' : 'String!' ;
77
- }
78
- } else {
79
- // Case 3 (user: $user)
80
- typeOrValue = `$${ key } ` ;
81
- }
82
-
83
- returnValue = `${ returnValue } ${ first ? '' : ', ' } ${ ( signature ? '$' : '' ) + key } : ${ typeOrValue } ` ;
84
- first = false ;
85
- }
86
- } ) ;
87
-
88
- if ( ! first ) returnValue = `(${ returnValue } )` ;
89
- }
90
-
91
- return returnValue ;
92
- }
93
-
94
34
/**
95
35
* Builds a field for the GraphQL query and a specific model
96
36
*
@@ -134,32 +74,11 @@ export default class QueryBuilder {
134
74
}
135
75
}
136
76
137
- /**
138
- *
139
- * @param {Model } model
140
- * @param {Model } rootModel
141
- * @returns {Array<String> }
142
- */
143
- private buildRelationsQuery ( model : ( null | Model ) , rootModel ?: Model ) {
144
- if ( model === null ) return '' ;
145
-
146
- const relationQueries : Array < string > = [ ] ;
147
-
148
- model . getRelations ( ) . forEach ( ( field : Field , name : string ) => {
149
- if ( ! rootModel || ( name !== rootModel . singularName && name !== rootModel . pluralName ) ) {
150
- const multiple : boolean = field . constructor . name !== 'BelongsTo' ;
151
- relationQueries . push ( this . buildField ( name , multiple , undefined , rootModel || model ) ) ;
152
- }
153
- } ) ;
154
-
155
- return relationQueries ;
156
- }
157
-
158
77
public buildQuery ( type : string , name ?: string , args ?: Arguments , model ?: ( Model | null | string ) , fields ?: string , multiple ?: boolean ) {
159
78
model = model ? this . getModel ( model ) : null ;
160
79
161
80
args = args ? JSON . parse ( JSON . stringify ( args ) ) : { } ;
162
- if ( ! args ) throw new Error ( " args is undefined" ) ;
81
+ if ( ! args ) throw new Error ( ' args is undefined' ) ;
163
82
164
83
if ( model && args [ model . singularName ] && typeof args [ model . singularName ] === 'object' ) {
165
84
args [ model . singularName ] = { __type : upcaseFirstLetter ( model . singularName ) } ;
@@ -252,4 +171,85 @@ export default class QueryBuilder {
252
171
253
172
return result ;
254
173
}
174
+
175
+ /**
176
+ * Generates the arguments string for a graphql query based on a given map.
177
+ *
178
+ * There are three types of arguments:
179
+ *
180
+ * 1) Signatures with simple types (signature = true)
181
+ * mutation createUser($name: String!)
182
+ *
183
+ * 2) Signatures with object types (signature = true, args = { user: { __type: 'User' }})
184
+ * mutation createUser($user: UserInput!)
185
+ *
186
+ * 3) Fields with variables (signature = false, valuesAsVariables = true)
187
+ * query user(id: $id)
188
+ *
189
+ * @param {Arguments | undefined } args
190
+ * @param {boolean } signature When true, then this method generates a query signature instead of key/value pairs
191
+ * @param {boolean } allowIdFields If true, ID fields will be included in the arguments list
192
+ * @returns {String }
193
+ */
194
+ private buildArguments ( args ?: Arguments , signature : boolean = false , allowIdFields : boolean = true ) : string {
195
+ if ( args === undefined ) return '' ;
196
+
197
+ let returnValue : string = '' ;
198
+ let first : boolean = true ;
199
+
200
+ if ( args ) {
201
+ Object . keys ( args ) . forEach ( ( key : string ) => {
202
+ let value : any = args [ key ] ;
203
+
204
+ // Ignore ids and connections
205
+ if ( ! ( value instanceof Array || ( key === 'id' && ! allowIdFields ) ) ) {
206
+ let typeOrValue : any = '' ;
207
+
208
+ if ( signature ) {
209
+ if ( typeof value === 'object' && value . __type ) {
210
+ // Case 2 (User!)
211
+ typeOrValue = value . __type + 'Input!' ;
212
+ } else if ( key === 'id' ) {
213
+ // Case 1 (ID!)
214
+ typeOrValue = 'ID!' ;
215
+ } else {
216
+ // Case 1 (String!)
217
+ typeOrValue = typeof value === 'number' ? 'Number!' : 'String!' ;
218
+ }
219
+ } else {
220
+ // Case 3 (user: $user)
221
+ typeOrValue = `$${ key } ` ;
222
+ }
223
+
224
+ returnValue = `${ returnValue } ${ first ? '' : ', ' } ${ ( signature ? '$' : '' ) + key } : ${ typeOrValue } ` ;
225
+ first = false ;
226
+ }
227
+ } ) ;
228
+
229
+ if ( ! first ) returnValue = `(${ returnValue } )` ;
230
+ }
231
+
232
+ return returnValue ;
233
+ }
234
+
235
+ /**
236
+ *
237
+ * @param {Model } model
238
+ * @param {Model } rootModel
239
+ * @returns {Array<String> }
240
+ */
241
+ private buildRelationsQuery ( model : ( null | Model ) , rootModel ?: Model ) {
242
+ if ( model === null ) return '' ;
243
+
244
+ const relationQueries : Array < string > = [ ] ;
245
+
246
+ model . getRelations ( ) . forEach ( ( field : Field , name : string ) => {
247
+ if ( ! rootModel || ( name !== rootModel . singularName && name !== rootModel . pluralName ) ) {
248
+ const multiple : boolean = field . constructor . name !== 'BelongsTo' ;
249
+ relationQueries . push ( this . buildField ( name , multiple , undefined , rootModel || model ) ) ;
250
+ }
251
+ } ) ;
252
+
253
+ return relationQueries ;
254
+ }
255
255
}
0 commit comments