@@ -7763,6 +7763,10 @@ gql.disableFragmentWarnings = disableFragmentWarnings;
7763
7763
var src = gql ;
7764
7764
7765
7765
var inflection$1 = require ( 'inflection' ) ;
7766
+ /**
7767
+ * Contains all logic to build GraphQL queries and transform variables between the format Vuex-ORM requires and the
7768
+ * format of the GraphQL API.
7769
+ */
7766
7770
var QueryBuilder = /** @class */ ( function ( ) {
7767
7771
/**
7768
7772
* Constructor.
@@ -7774,7 +7778,7 @@ var QueryBuilder = /** @class */ (function () {
7774
7778
this . getModel = getModel ;
7775
7779
}
7776
7780
/**
7777
- * Takes a string with a graphql query and formats it
7781
+ * Takes a string with a graphql query and formats it. Useful for debug output and the tests.
7778
7782
* @param {string } query
7779
7783
* @returns {string }
7780
7784
*/
@@ -7784,13 +7788,17 @@ var QueryBuilder = /** @class */ (function () {
7784
7788
/**
7785
7789
* Builds a field for the GraphQL query and a specific model
7786
7790
*
7787
- * @param {Model|string } model
7788
- * @param {boolean } multiple
7789
- * @param {Arguments } args
7790
- * @param {Model } rootModel
7791
- * @param {string } name
7792
- * @param allowIdFields
7791
+ * @param {Model|string } model The model to use
7792
+ * @param {boolean } multiple Determines whether plural/nodes syntax or singular syntax is used.
7793
+ * @param {Arguments } args The args that will be passed to the query field ( user(role: $role) )
7794
+ * @param {Model } rootModel The model of the root query field. Used to avoid endless recursion
7795
+ * @param {string } name Optional name of the field. If not provided, this will be the model name
7796
+ * @param {boolean } allowIdFields Optional. Determines if id fields will be ignored for the argument generation.
7797
+ * See buildArguments
7793
7798
* @returns {string }
7799
+ *
7800
+ * @todo Do we need the allowIdFields param?
7801
+ * @todo There could be a endless recursion even with rootModel correctly set. We should track an array of models here probably?
7794
7802
*/
7795
7803
QueryBuilder . prototype . buildField = function ( model , multiple , args , rootModel , name , allowIdFields ) {
7796
7804
if ( multiple === void 0 ) { multiple = true ; }
@@ -7805,21 +7813,36 @@ var QueryBuilder = /** @class */ (function () {
7805
7813
return "\n " + ( name ? name : model . singularName ) + params + " {\n " + fields + "\n }\n " ;
7806
7814
}
7807
7815
} ;
7808
- QueryBuilder . prototype . buildQuery = function ( type , name , args , model , fields , multiple ) {
7809
- model = model ? this . getModel ( model ) : null ;
7816
+ /**
7817
+ * Generates a query.
7818
+ * Currently only one root field for the query is possible.
7819
+ * @param {string } type 'mutation' or 'query'
7820
+ * @param {Model | string } model The model this query or mutation affects. This mainly determines the query fields.
7821
+ * @param {string } name Optional name of the query/mutation. Will overwrite the name from the model.
7822
+ * @param {Arguments } args Arguments for the query
7823
+ * @param {boolean } multiple Determines if the root query field is a connection or not (will be passed to buildField)
7824
+ * @returns {any }
7825
+ */
7826
+ QueryBuilder . prototype . buildQuery = function ( type , model , name , args , multiple ) {
7827
+ // model
7828
+ model = this . getModel ( model ) ;
7829
+ if ( ! model )
7830
+ throw new Error ( 'No model provided to build the query!' ) ;
7831
+ // args
7810
7832
args = args ? JSON . parse ( JSON . stringify ( args ) ) : { } ;
7811
7833
if ( ! args )
7812
7834
throw new Error ( 'args is undefined' ) ;
7813
- if ( model && args [ model . singularName ] && typeof args [ model . singularName ] === 'object' ) {
7835
+ if ( args [ model . singularName ] && typeof args [ model . singularName ] === 'object' ) {
7814
7836
args [ model . singularName ] = { __type : upcaseFirstLetter ( model . singularName ) } ;
7815
7837
}
7838
+ // multiple
7816
7839
multiple = multiple === undefined ? ! args [ 'id' ] : multiple ;
7817
- if ( ! name && model )
7818
- name = ( multiple ? model . pluralName : model . singularName ) ;
7840
+ // name
7819
7841
if ( ! name )
7820
- throw new Error ( "Can't determine name for the query! Please provide either name or model" ) ;
7842
+ name = ( multiple ? model . pluralName : model . singularName ) ;
7843
+ // build query
7821
7844
var query = type + " " + upcaseFirstLetter ( name ) + this . buildArguments ( args , true ) + " {\n" +
7822
- ( " " + ( model ? this . buildField ( model , multiple , args , model , name , true ) : fields ) + "\n" ) +
7845
+ ( " " + this . buildField ( model , multiple , args , model , name , true ) + "\n" ) +
7823
7846
"}" ;
7824
7847
return src ( query ) ;
7825
7848
} ;
@@ -7905,7 +7928,7 @@ var QueryBuilder = /** @class */ (function () {
7905
7928
* 2) Signatures with object types (signature = true, args = { user: { __type: 'User' }})
7906
7929
* mutation createUser($user: UserInput!)
7907
7930
*
7908
- * 3) Fields with variables (signature = false, valuesAsVariables = true )
7931
+ * 3) Fields with variables (signature = false)
7909
7932
* query user(id: $id)
7910
7933
*
7911
7934
* @param {Arguments | undefined } args
@@ -8081,7 +8104,7 @@ var VuexORMApollo = /** @class */ (function () {
8081
8104
this . collectModels ( ) ;
8082
8105
this . setupMethods ( ) ;
8083
8106
this . httpLink = new HttpLink ( {
8084
- uri : '/graphql'
8107
+ uri : options . url ? options . url : '/graphql'
8085
8108
} ) ;
8086
8109
this . apolloClient = new ApolloClient ( {
8087
8110
link : this . httpLink ,
@@ -8115,7 +8138,7 @@ var VuexORMApollo = /** @class */ (function () {
8115
8138
} ) ;
8116
8139
} ;
8117
8140
/**
8118
- * This method will setup following Vuex action: fetch, persist, push, destroy
8141
+ * This method will setup following Vuex action: fetch, persist, push, destroy, mutate
8119
8142
*/
8120
8143
VuexORMApollo . prototype . setupMethods = function ( ) {
8121
8144
this . components . subActions . fetch = this . fetch . bind ( this ) ;
@@ -8128,9 +8151,9 @@ var VuexORMApollo = /** @class */ (function () {
8128
8151
/**
8129
8152
* Will be called, when dispatch('entities/something/fetch') is called.
8130
8153
*
8131
- * @param {Arguments } args
8132
- * @param {any } state
8133
- * @param {any } dispatch
8154
+ * @param {any } state The Vuex State from Vuex-ORM
8155
+ * @param {DispatchFunction } dispatch Vuex Dispatch method for the model
8156
+ * @param {ActionParams } filter Filter params for the query
8134
8157
* @returns {Promise<void> }
8135
8158
*/
8136
8159
VuexORMApollo . prototype . fetch = function ( _a , filter ) {
@@ -8143,7 +8166,7 @@ var VuexORMApollo = /** @class */ (function () {
8143
8166
multiple = ! ( filter && filter [ 'id' ] ) ;
8144
8167
model = this . getModel ( state . $name ) ;
8145
8168
name = "" + ( multiple ? model . pluralName : model . singularName ) ;
8146
- query = this . queryBuilder . buildQuery ( 'query' , name , filter , model . singularName ) ;
8169
+ query = this . queryBuilder . buildQuery ( 'query' , model , name , filter ) ;
8147
8170
return [ 4 /*yield*/ , this . apolloRequest ( query , filter ) ] ;
8148
8171
case 1 :
8149
8172
data = _b . sent ( ) ;
@@ -8160,9 +8183,9 @@ var VuexORMApollo = /** @class */ (function () {
8160
8183
/**
8161
8184
* Will be called, when dispatch('entities/something/persist') is called.
8162
8185
*
8163
- * @param {any } state
8164
- * @param {any } dispatch
8165
- * @param {any } id
8186
+ * @param {any } state The Vuex State from Vuex-ORM
8187
+ * @param {DispatchFunction } dispatch Vuex Dispatch method for the model
8188
+ * @param {string } id ID of the record to persist
8166
8189
* @returns {Promise<void> }
8167
8190
*/
8168
8191
VuexORMApollo . prototype . persist = function ( _a , _b ) {
@@ -8191,36 +8214,28 @@ var VuexORMApollo = /** @class */ (function () {
8191
8214
/**
8192
8215
* Will be called, when dispatch('entities/something/mutate') is called.
8193
8216
* For custom mutations.
8194
- *
8195
- * @param {any } state
8196
- * @param {any } dispatch
8197
- * @param {Data } data
8198
- * @returns {Promise<Data | {}> }
8217
+ * @param {any } state The Vuex State from Vuex-ORM
8218
+ * @param {DispatchFunction } dispatch Vuex Dispatch method for the model
8219
+ * @param {Arguments } args Arguments for the mutation. Must contain a 'mutation' field.
8220
+ * @returns {Promise<any> }
8199
8221
*/
8200
8222
VuexORMApollo . prototype . customMutation = function ( _a , args ) {
8201
8223
var state = _a . state , dispatch = _a . dispatch ;
8202
8224
return __awaiter ( this , void 0 , void 0 , function ( ) {
8203
8225
var name , model ;
8204
8226
return __generator ( this , function ( _b ) {
8205
- switch ( _b . label ) {
8206
- case 0 :
8207
- name = args [ 'mutation' ] ;
8208
- delete args [ 'mutation' ] ;
8209
- model = this . getModel ( state . $name ) ;
8210
- return [ 4 /*yield*/ , this . mutate ( name , args , dispatch , model ) ] ;
8211
- case 1 :
8212
- _b . sent ( ) ;
8213
- // TODO What would make sense here?
8214
- return [ 2 /*return*/ , true ] ;
8215
- }
8227
+ name = args [ 'mutation' ] ;
8228
+ delete args [ 'mutation' ] ;
8229
+ model = this . getModel ( state . $name ) ;
8230
+ return [ 2 /*return*/ , this . mutate ( name , args , dispatch , model ) ] ;
8216
8231
} ) ;
8217
8232
} ) ;
8218
8233
} ;
8219
8234
/**
8220
8235
* Will be called, when dispatch('entities/something/push') is called.
8221
- * @param {any } state
8222
- * @param {any } dispatch
8223
- * @param {Data } data
8236
+ * @param {any } state The Vuex State from Vuex-ORM
8237
+ * @param {DispatchFunction } dispatch Vuex Dispatch method for the model
8238
+ * @param {Arguments } data New data to save
8224
8239
* @returns {Promise<Data | {}> }
8225
8240
*/
8226
8241
VuexORMApollo . prototype . push = function ( _a , _b ) {
@@ -8250,9 +8265,9 @@ var VuexORMApollo = /** @class */ (function () {
8250
8265
/**
8251
8266
* Will be called, when dispatch('entities/something/destroy') is called.
8252
8267
*
8253
- * @param {any } state
8254
- * @param {any } dispatch
8255
- * @param {Data } id
8268
+ * @param {any } state The Vuex State from Vuex-ORM
8269
+ * @param {DispatchFunction } dispatch Vuex Dispatch method for the model
8270
+ * @param {string } id ID of the record to delete
8256
8271
* @returns {Promise<void> }
8257
8272
*/
8258
8273
VuexORMApollo . prototype . destroy = function ( _a , _b ) {
@@ -8261,52 +8276,50 @@ var VuexORMApollo = /** @class */ (function () {
8261
8276
return __awaiter ( this , void 0 , void 0 , function ( ) {
8262
8277
var model , mutationName ;
8263
8278
return __generator ( this , function ( _c ) {
8264
- switch ( _c . label ) {
8265
- case 0 :
8266
- if ( ! id ) return [ 3 /*break*/ , 2 ] ;
8267
- model = this . getModel ( state . $name ) ;
8268
- mutationName = "delete" + upcaseFirstLetter ( model . singularName ) ;
8269
- return [ 4 /*yield*/ , this . mutate ( mutationName , { id : id } , dispatch , model , false ) ] ;
8270
- case 1 :
8271
- _c . sent ( ) ;
8272
- // TODO what would make sense here?
8273
- return [ 2 /*return*/ , true ] ;
8274
- case 2 : return [ 2 /*return*/ ] ;
8279
+ if ( id ) {
8280
+ model = this . getModel ( state . $name ) ;
8281
+ mutationName = "delete" + upcaseFirstLetter ( model . singularName ) ;
8282
+ return [ 2 /*return*/ , this . mutate ( mutationName , { id : id } , dispatch , model , false ) ] ;
8275
8283
}
8284
+ return [ 2 /*return*/ ] ;
8276
8285
} ) ;
8277
8286
} ) ;
8278
8287
} ;
8279
8288
/**
8280
- * Contains the logic to save (persist or push) data .
8289
+ * Sends a mutation .
8281
8290
*
8282
- * @param {string } action
8283
- * @param {Data | undefined } data
8284
- * @param {Function } dispatch
8285
- * @param {Model } model
8291
+ * @param {string } name Name of the mutation like 'createUser'
8292
+ * @param {Data | undefined } variables Variables to send with the mutation
8293
+ * @param {Function } dispatch Vuex Dispatch method for the model
8294
+ * @param {Model } model The model this mutation affects.
8295
+ * @param {boolean } multiple See QueryBuilder.buildQuery()
8286
8296
* @returns {Promise<any> }
8287
8297
*/
8288
- VuexORMApollo . prototype . mutate = function ( action , variables , dispatch , model , multiple ) {
8298
+ VuexORMApollo . prototype . mutate = function ( name , variables , dispatch , model , multiple ) {
8289
8299
return __awaiter ( this , void 0 , void 0 , function ( ) {
8290
8300
var id , query , newData ;
8291
8301
return __generator ( this , function ( _a ) {
8292
8302
switch ( _a . label ) {
8293
8303
case 0 :
8294
8304
if ( ! variables ) return [ 3 /*break*/ , 2 ] ;
8295
8305
id = variables . id ? variables . id : undefined ;
8296
- query = this . queryBuilder . buildQuery ( 'mutation' , action , variables , model , undefined , multiple ) ;
8306
+ query = this . queryBuilder . buildQuery ( 'mutation' , model , name , variables , multiple ) ;
8297
8307
return [ 4 /*yield*/ , this . apolloRequest ( query , variables , true ) ] ;
8298
8308
case 1 :
8299
8309
newData = _a . sent ( ) ;
8300
- // TODO: What if there is no id?
8301
- return [ 2 /*return*/ , this . updateData ( newData , dispatch , id ) ] ;
8310
+ if ( id )
8311
+ return [ 2 /*return*/ , this . updateData ( newData , dispatch , id ) ] ;
8312
+ return [ 2 /*return*/ , null ] ;
8302
8313
case 2 : return [ 2 /*return*/ ] ;
8303
8314
}
8304
8315
} ) ;
8305
8316
} ) ;
8306
8317
} ;
8307
8318
/**
8308
8319
* Sends a request to the GraphQL API via apollo
8309
- * @param query
8320
+ * @param {any } query The query to send (result from gql())
8321
+ * @param {Arguments } variables Optional. The variables to send with the query
8322
+ * @param {boolean } mutation Optional. If this is a mutation (true) or a query (false, default)
8310
8323
* @returns {Promise<Data> }
8311
8324
*/
8312
8325
VuexORMApollo . prototype . apolloRequest = function ( query , variables , mutation ) {
@@ -8317,11 +8330,11 @@ var VuexORMApollo = /** @class */ (function () {
8317
8330
switch ( _a . label ) {
8318
8331
case 0 :
8319
8332
if ( ! mutation ) return [ 3 /*break*/ , 2 ] ;
8320
- return [ 4 /*yield*/ , ( this . apolloClient ) . mutate ( { mutation : query , variables : variables } ) ] ;
8333
+ return [ 4 /*yield*/ , this . apolloClient . mutate ( { mutation : query , variables : variables } ) ] ;
8321
8334
case 1 :
8322
8335
response = _a . sent ( ) ;
8323
8336
return [ 3 /*break*/ , 4 ] ;
8324
- case 2 : return [ 4 /*yield*/ , ( this . apolloClient ) . query ( { query : query , variables : variables } ) ] ;
8337
+ case 2 : return [ 4 /*yield*/ , this . apolloClient . query ( { query : query , variables : variables } ) ] ;
8325
8338
case 3 :
8326
8339
response = _a . sent ( ) ;
8327
8340
_a . label = 4 ;
@@ -8335,9 +8348,8 @@ var VuexORMApollo = /** @class */ (function () {
8335
8348
/**
8336
8349
* Inserts incoming data into the store.
8337
8350
*
8338
- * @param {Data } data
8339
- * @param {Function } dispatch
8340
- * @param {boolean } update
8351
+ * @param {Data } data New data to insert/update
8352
+ * @param {Function } dispatch Vuex Dispatch method for the model
8341
8353
*/
8342
8354
VuexORMApollo . prototype . insertData = function ( data , dispatch ) {
8343
8355
return __awaiter ( this , void 0 , void 0 , function ( ) {
@@ -8361,8 +8373,8 @@ var VuexORMApollo = /** @class */ (function () {
8361
8373
* Updates an existing record in the store with new data. This method can only update one single record, so
8362
8374
* it takes the first record of the first field from the data object!
8363
8375
* @param {Data } data
8364
- * @param {Function } dispatch
8365
- * @param id
8376
+ * @param {Function } dispatch Vuex Dispatch method for the model
8377
+ * @param { string|number } id ID of the record to update
8366
8378
*/
8367
8379
VuexORMApollo . prototype . updateData = function ( data , dispatch , id ) {
8368
8380
return __awaiter ( this , void 0 , void 0 , function ( ) {
0 commit comments