@@ -8083,6 +8083,16 @@ gql.disableFragmentWarnings = disableFragmentWarnings;
8083
8083
8084
8084
var src = gql ;
8085
8085
8086
+ /**
8087
+ * Capitalizes the first letter of the given string.
8088
+ *
8089
+ * @param {string } input
8090
+ * @returns {string }
8091
+ */
8092
+ function capitalizeFirstLetter ( input ) {
8093
+ return input . charAt ( 0 ) . toUpperCase ( ) + input . slice ( 1 ) ;
8094
+ }
8095
+
8086
8096
var inflection$1 = require ( 'inflection' ) ;
8087
8097
/**
8088
8098
* This class takes care of everything GraphQL query related, especially the generation of queries out of models
@@ -8288,10 +8298,29 @@ var QueryBuilder = /** @class */ (function () {
8288
8298
* @returns {any }
8289
8299
*/
8290
8300
QueryBuilder . prototype . buildQuery = function ( modelName , args ) {
8301
+ // Ignore empty args
8302
+ if ( args && Object . keys ( args ) . length === 0 )
8303
+ args = undefined ;
8291
8304
var multiple = ! ( args && args . get ( 'id' ) ) ;
8292
8305
var query = "{ " + this . buildField ( modelName , multiple , args ) + " }" ;
8293
8306
return src ( query ) ;
8294
8307
} ;
8308
+ /**
8309
+ * Generates a mutation query for a model.
8310
+ *
8311
+ * @param {Model } model
8312
+ * @param {string }prefix
8313
+ * @returns {any }
8314
+ */
8315
+ QueryBuilder . prototype . buildMutation = function ( model , prefix ) {
8316
+ if ( prefix === void 0 ) { prefix = 'create' ; }
8317
+ var name = "" + prefix + capitalizeFirstLetter ( model . singularName ) ;
8318
+ // Send the request to the GraphQL API
8319
+ var signature = this . buildArguments ( { contract : { __type : 'Contract' } } , true ) ;
8320
+ var field = this . buildField ( model , false , { contract : { __type : 'Contract' } } , true , undefined , name ) ;
8321
+ var query = "\n mutation " + name + signature + " {\n " + field + "\n }\n " ;
8322
+ return src ( query ) ;
8323
+ } ;
8295
8324
return QueryBuilder ;
8296
8325
} ( ) ) ;
8297
8326
@@ -8341,16 +8370,6 @@ var Logger = /** @class */ (function () {
8341
8370
return Logger ;
8342
8371
} ( ) ) ;
8343
8372
8344
- /**
8345
- * Capitalizes the first letter of the given string.
8346
- *
8347
- * @param {string } input
8348
- * @returns {string }
8349
- */
8350
- function capitalizeFirstLetter ( input ) {
8351
- return input . charAt ( 0 ) . toUpperCase ( ) + input . slice ( 1 ) ;
8352
- }
8353
-
8354
8373
var __awaiter = ( undefined && undefined . __awaiter ) || function ( thisArg , _arguments , P , generator ) {
8355
8374
return new ( P || ( P = Promise ) ) ( function ( resolve , reject ) {
8356
8375
function fulfilled ( value ) { try { step ( generator . next ( value ) ) ; } catch ( e ) { reject ( e ) ; } }
@@ -8420,17 +8439,6 @@ var VuexORMApollo = /** @class */ (function () {
8420
8439
} ) ;
8421
8440
this . queryBuilder = new QueryBuilder ( this . logger , this . getModel . bind ( this ) ) ;
8422
8441
}
8423
- /**
8424
- * The install method will be called when the plugin should be installed. We create a new instance of the Plugin class
8425
- * here.
8426
- *
8427
- * @param components
8428
- * @param options
8429
- * @returns {VuexORMApollo }
8430
- */
8431
- VuexORMApollo . install = function ( components , options ) {
8432
- return new VuexORMApollo ( components , options ) ;
8433
- } ;
8434
8442
/**
8435
8443
* Returns a model by name
8436
8444
*
@@ -8480,9 +8488,6 @@ var VuexORMApollo = /** @class */ (function () {
8480
8488
return __generator ( this , function ( _b ) {
8481
8489
switch ( _b . label ) {
8482
8490
case 0 :
8483
- // Ignore empty filters
8484
- if ( filter && Object . keys ( filter ) . length === 0 )
8485
- filter = undefined ;
8486
8491
query = this . queryBuilder . buildQuery ( state . $name , filter ) ;
8487
8492
return [ 4 /*yield*/ , this . apolloRequest ( query ) ] ;
8488
8493
case 1 :
@@ -8504,41 +8509,60 @@ var VuexORMApollo = /** @class */ (function () {
8504
8509
*/
8505
8510
VuexORMApollo . prototype . persist = function ( _a , _b ) {
8506
8511
var state = _a . state , dispatch = _a . dispatch ;
8507
- var id = _b . id ;
8512
+ var data = _b . data ;
8513
+ return __awaiter ( this , void 0 , void 0 , function ( ) {
8514
+ return __generator ( this , function ( _c ) {
8515
+ return [ 2 /*return*/ , this . mutate ( 'create' , data , dispatch , this . getModel ( state . $name ) ) ] ;
8516
+ } ) ;
8517
+ } ) ;
8518
+ } ;
8519
+ /**
8520
+ * Will be called, when dispatch('entities/something/push') is called.
8521
+ * @param {any } state
8522
+ * @param {any } dispatch
8523
+ * @param {Data } data
8524
+ * @returns {Promise<Data | {}> }
8525
+ */
8526
+ VuexORMApollo . prototype . push = function ( _a , _b ) {
8527
+ var state = _a . state , dispatch = _a . dispatch ;
8528
+ var data = _b . data ;
8508
8529
return __awaiter ( this , void 0 , void 0 , function ( ) {
8509
- var model , name , data , signature , query , response , newData , _c ;
8510
- return __generator ( this , function ( _d ) {
8511
- switch ( _d . label ) {
8530
+ return __generator ( this , function ( _c ) {
8531
+ return [ 2 /*return*/ , this . mutate ( 'update' , data , dispatch , this . getModel ( state . $name ) ) ] ;
8532
+ } ) ;
8533
+ } ) ;
8534
+ } ;
8535
+ /**
8536
+ * Contains the logic to save (persist or push) data.
8537
+ *
8538
+ * @param {string } action
8539
+ * @param {Data | undefined } data
8540
+ * @param {Function } dispatch
8541
+ * @param {Model } model
8542
+ * @returns {Promise<any> }
8543
+ */
8544
+ VuexORMApollo . prototype . mutate = function ( action , data , dispatch , model ) {
8545
+ return __awaiter ( this , void 0 , void 0 , function ( ) {
8546
+ var query , response , newData , _a ;
8547
+ return __generator ( this , function ( _b ) {
8548
+ switch ( _b . label ) {
8512
8549
case 0 :
8513
- model = this . getModel ( state . $name ) ;
8514
- name = "create" + capitalizeFirstLetter ( model . singularName ) ;
8515
- data = model . baseModel . getters ( 'find' , { id : id } ) ( ) ;
8516
- signature = this . queryBuilder . buildArguments ( { contract : { __type : 'Contract' } } , true ) ;
8517
- query = "\n mutation " + name + signature + " {\n " + this . queryBuilder . buildField ( model , false , { contract : { __type : 'Contract' } } , true , undefined , name ) + "\n }\n " ;
8518
- this . logger . logQuery ( query ) ;
8519
- delete data . id ;
8550
+ if ( ! data ) return [ 3 /*break*/ , 2 ] ;
8551
+ query = this . queryBuilder . buildMutation ( model , action ) ;
8520
8552
return [ 4 /*yield*/ , this . apolloClient . mutate ( {
8521
- 'mutation' : src ( query ) ,
8522
- 'variables' : ( _c = { } , _c [ model . singularName ] = this . queryBuilder . transformOutgoingData ( data ) , _c )
8553
+ 'mutation' : query ,
8554
+ 'variables' : ( _a = { } , _a [ model . singularName ] = this . queryBuilder . transformOutgoingData ( data ) , _a )
8523
8555
} ) ] ;
8524
8556
case 1 :
8525
- response = _d . sent ( ) ;
8557
+ response = _b . sent ( ) ;
8526
8558
newData = this . queryBuilder . transformIncomingData ( response . data ) ;
8527
8559
this . storeData ( newData , dispatch ) ;
8528
8560
return [ 2 /*return*/ , newData ] ;
8561
+ case 2 : return [ 2 /*return*/ , { } ] ;
8529
8562
}
8530
8563
} ) ;
8531
8564
} ) ;
8532
8565
} ;
8533
- VuexORMApollo . prototype . push = function ( _a , _b ) {
8534
- var state = _a . state , dispatch = _a . dispatch ;
8535
- var id = _b . id ;
8536
- return __awaiter ( this , void 0 , void 0 , function ( ) {
8537
- return __generator ( this , function ( _c ) {
8538
- return [ 2 /*return*/ ] ;
8539
- } ) ;
8540
- } ) ;
8541
- } ;
8542
8566
/**
8543
8567
* Sends a query to the GraphQL API via apollo
8544
8568
* @param query
@@ -8566,10 +8590,19 @@ var VuexORMApollo = /** @class */ (function () {
8566
8590
*/
8567
8591
VuexORMApollo . prototype . storeData = function ( data , dispatch ) {
8568
8592
Object . keys ( data ) . forEach ( function ( key ) {
8569
- dispatch ( 'create ' , { data : data [ key ] } ) ;
8593
+ dispatch ( 'insert ' , { data : data [ key ] } ) ;
8570
8594
} ) ;
8571
8595
} ;
8572
8596
return VuexORMApollo ;
8573
8597
} ( ) ) ;
8574
8598
8575
- export default VuexORMApollo ;
8599
+ var VuexORMApolloPlugin = /** @class */ ( function ( ) {
8600
+ function VuexORMApolloPlugin ( ) {
8601
+ }
8602
+ VuexORMApolloPlugin . install = function ( components , options ) {
8603
+ return new VuexORMApollo ( components , options ) ;
8604
+ } ;
8605
+ return VuexORMApolloPlugin ;
8606
+ } ( ) ) ;
8607
+
8608
+ export default VuexORMApolloPlugin ;
0 commit comments