@@ -8010,6 +8010,12 @@ var QueryBuilder = /** @class */ (function () {
8010
8010
8011
8011
var Logger = /** @class */ ( function ( ) {
8012
8012
function Logger ( enabled ) {
8013
+ this . PREFIX = process . env . NODE_ENV === 'test' ? [ '[Vuex-ORM-Apollo]' ] :
8014
+ [
8015
+ '%c Vuex-ORM: Apollo Plugin %c' ,
8016
+ 'background: #35495e; padding: 1px 0; border-radius: 3px; color: #eee;' ,
8017
+ 'background: transparent;'
8018
+ ] ;
8013
8019
this . enabled = enabled ;
8014
8020
this . log ( 'Logging is enabled.' ) ;
8015
8021
}
@@ -8020,10 +8026,10 @@ var Logger = /** @class */ (function () {
8020
8026
}
8021
8027
if ( this . enabled ) {
8022
8028
if ( process . env . NODE_ENV === 'test' ) {
8023
- console . group . apply ( console , [ '[Vuex-ORM-Apollo]' ] . concat ( messages ) ) ;
8029
+ console . group . apply ( console , this . PREFIX . concat ( messages ) ) ;
8024
8030
}
8025
8031
else {
8026
- console . groupCollapsed . apply ( console , [ '[Vuex-ORM-Apollo]' ] . concat ( messages ) ) ;
8032
+ console . groupCollapsed . apply ( console , this . PREFIX . concat ( messages ) ) ;
8027
8033
}
8028
8034
}
8029
8035
} ;
@@ -8037,19 +8043,21 @@ var Logger = /** @class */ (function () {
8037
8043
messages [ _i ] = arguments [ _i ] ;
8038
8044
}
8039
8045
if ( this . enabled ) {
8040
- console . log . apply ( console , [ '[Vuex-ORM-Apollo]' ] . concat ( messages ) ) ;
8046
+ console . log . apply ( console , this . PREFIX . concat ( messages ) ) ;
8041
8047
}
8042
8048
} ;
8043
8049
Logger . prototype . logQuery = function ( query , variables , fetchPolicy ) {
8044
8050
if ( this . enabled ) {
8045
8051
try {
8046
- this . group ( 'Sending query:' ) ;
8052
+ var prettified = '' ;
8047
8053
if ( typeof query === 'object' && query . loc ) {
8048
- console . log ( QueryBuilder . prettify ( query . loc . source . body ) ) ;
8054
+ prettified = QueryBuilder . prettify ( query . loc . source . body ) ;
8049
8055
}
8050
8056
else {
8051
- console . log ( QueryBuilder . prettify ( query ) ) ;
8057
+ prettified = QueryBuilder . prettify ( query ) ;
8052
8058
}
8059
+ this . group ( 'Sending query:' , prettified . split ( '\n' ) [ 1 ] . replace ( '{' , '' ) . trim ( ) ) ;
8060
+ console . log ( prettified ) ;
8053
8061
if ( variables )
8054
8062
console . log ( 'VARIABLES:' , variables ) ;
8055
8063
if ( fetchPolicy )
@@ -8327,11 +8335,11 @@ var VuexORMApollo = /** @class */ (function () {
8327
8335
var state = _a . state , dispatch = _a . dispatch ;
8328
8336
var id = _b . id , args = _b . args ;
8329
8337
return __awaiter ( this , void 0 , void 0 , function ( ) {
8330
- var model , data , mutationName , record ;
8338
+ var model , data , mutationName , oldRecord ;
8331
8339
return __generator ( this , function ( _c ) {
8332
8340
switch ( _c . label ) {
8333
8341
case 0 :
8334
- if ( ! id ) return [ 3 /*break*/ , 2 ] ;
8342
+ if ( ! id ) return [ 3 /*break*/ , 4 ] ;
8335
8343
model = this . context . getModel ( state . $name ) ;
8336
8344
data = model . baseModel . getters ( 'find' ) ( id ) ;
8337
8345
args = args || { } ;
@@ -8340,11 +8348,21 @@ var VuexORMApollo = /** @class */ (function () {
8340
8348
return [ 4 /*yield*/ , this . mutate ( mutationName , args , dispatch , model , false ) ] ;
8341
8349
case 1 :
8342
8350
_c . sent ( ) ;
8343
- record = model . baseModel . getters ( 'find' ) ( id ) ;
8344
- record . $isPersisted = true ;
8345
- record . $dispatch ( 'update' , { where : record . id , data : record } ) ;
8346
- return [ 2 /*return*/ , record ] ;
8347
- case 2 : return [ 2 /*return*/ ] ;
8351
+ oldRecord = model . baseModel . getters ( 'find' ) ( id ) ;
8352
+ this . context . logger . log ( oldRecord ) ;
8353
+ if ( ! ( oldRecord && ! oldRecord . $isPersisted ) ) return [ 3 /*break*/ , 3 ] ;
8354
+ // The server generated another ID, this is very likely to happen.
8355
+ // in this case this.mutate has inserted a new record instead of updating the existing one.
8356
+ // We can see that because $isPersisted is still false then.
8357
+ this . context . logger . log ( 'Dropping deprecated record with ID' , oldRecord . id ) ;
8358
+ return [ 4 /*yield*/ , model . baseModel . dispatch ( 'delete' , { where : oldRecord . id } ) ] ;
8359
+ case 2 :
8360
+ _c . sent ( ) ;
8361
+ _c . label = 3 ;
8362
+ case 3 :
8363
+ // TODO is this save?
8364
+ return [ 2 /*return*/ , model . baseModel . getters ( 'query' ) ( ) . withAll ( ) . last ( ) ] ;
8365
+ case 4 : return [ 2 /*return*/ ] ;
8348
8366
}
8349
8367
} ) ;
8350
8368
} ) ;
@@ -8404,7 +8422,6 @@ var VuexORMApollo = /** @class */ (function () {
8404
8422
return [ 4 /*yield*/ , this . mutate ( mutationName , args , dispatch , model , false ) ] ;
8405
8423
case 1 :
8406
8424
_c . sent ( ) ;
8407
- // TODO is this really necessary?
8408
8425
return [ 2 /*return*/ , model . baseModel . getters ( 'find' ) ( data . id ) ] ;
8409
8426
case 2 : return [ 2 /*return*/ ] ;
8410
8427
}
@@ -8452,17 +8469,19 @@ var VuexORMApollo = /** @class */ (function () {
8452
8469
return __generator ( this , function ( _a ) {
8453
8470
switch ( _a . label ) {
8454
8471
case 0 :
8455
- if ( ! variables ) return [ 3 /*break*/ , 2 ] ;
8472
+ if ( ! variables ) return [ 3 /*break*/ , 4 ] ;
8456
8473
id = variables . id ? variables . id : undefined ;
8457
8474
query = this . queryBuilder . buildQuery ( 'mutation' , model , name , variables , multiple ) ;
8458
8475
return [ 4 /*yield*/ , this . apolloRequest ( model , query , variables , true ) ] ;
8459
8476
case 1 :
8460
8477
newData = _a . sent ( ) ;
8461
- if ( name !== "delete" + upcaseFirstLetter ( model . singularName ) ) {
8462
- return [ 2 /*return*/ , this . insertData ( newData , dispatch ) ] ;
8463
- }
8464
- return [ 2 /*return*/ , true ] ;
8465
- case 2 : return [ 2 /*return*/ ] ;
8478
+ if ( ! ( name !== "delete" + upcaseFirstLetter ( model . singularName ) ) ) return [ 3 /*break*/ , 3 ] ;
8479
+ return [ 4 /*yield*/ , this . insertData ( newData , dispatch ) ] ;
8480
+ case 2 :
8481
+ _a . sent ( ) ;
8482
+ return [ 2 /*return*/ , true ] ; // FIXME RETURN THE NEW RECORD!!
8483
+ case 3 : return [ 2 /*return*/ , true ] ;
8484
+ case 4 : return [ 2 /*return*/ ] ;
8466
8485
}
8467
8486
} ) ;
8468
8487
} ) ;
0 commit comments