@@ -8010,6 +8010,12 @@ var QueryBuilder = /** @class */ (function () {
80108010
80118011var Logger = /** @class */ ( function ( ) {
80128012 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+ ] ;
80138019 this . enabled = enabled ;
80148020 this . log ( 'Logging is enabled.' ) ;
80158021 }
@@ -8020,10 +8026,10 @@ var Logger = /** @class */ (function () {
80208026 }
80218027 if ( this . enabled ) {
80228028 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 ) ) ;
80248030 }
80258031 else {
8026- console . groupCollapsed . apply ( console , [ '[Vuex-ORM-Apollo]' ] . concat ( messages ) ) ;
8032+ console . groupCollapsed . apply ( console , this . PREFIX . concat ( messages ) ) ;
80278033 }
80288034 }
80298035 } ;
@@ -8037,19 +8043,21 @@ var Logger = /** @class */ (function () {
80378043 messages [ _i ] = arguments [ _i ] ;
80388044 }
80398045 if ( this . enabled ) {
8040- console . log . apply ( console , [ '[Vuex-ORM-Apollo]' ] . concat ( messages ) ) ;
8046+ console . log . apply ( console , this . PREFIX . concat ( messages ) ) ;
80418047 }
80428048 } ;
80438049 Logger . prototype . logQuery = function ( query , variables , fetchPolicy ) {
80448050 if ( this . enabled ) {
80458051 try {
8046- this . group ( 'Sending query:' ) ;
8052+ var prettified = '' ;
80478053 if ( typeof query === 'object' && query . loc ) {
8048- console . log ( QueryBuilder . prettify ( query . loc . source . body ) ) ;
8054+ prettified = QueryBuilder . prettify ( query . loc . source . body ) ;
80498055 }
80508056 else {
8051- console . log ( QueryBuilder . prettify ( query ) ) ;
8057+ prettified = QueryBuilder . prettify ( query ) ;
80528058 }
8059+ this . group ( 'Sending query:' , prettified . split ( '\n' ) [ 1 ] . replace ( '{' , '' ) . trim ( ) ) ;
8060+ console . log ( prettified ) ;
80538061 if ( variables )
80548062 console . log ( 'VARIABLES:' , variables ) ;
80558063 if ( fetchPolicy )
@@ -8327,11 +8335,11 @@ var VuexORMApollo = /** @class */ (function () {
83278335 var state = _a . state , dispatch = _a . dispatch ;
83288336 var id = _b . id , args = _b . args ;
83298337 return __awaiter ( this , void 0 , void 0 , function ( ) {
8330- var model , data , mutationName , record ;
8338+ var model , data , mutationName , oldRecord ;
83318339 return __generator ( this , function ( _c ) {
83328340 switch ( _c . label ) {
83338341 case 0 :
8334- if ( ! id ) return [ 3 /*break*/ , 2 ] ;
8342+ if ( ! id ) return [ 3 /*break*/ , 4 ] ;
83358343 model = this . context . getModel ( state . $name ) ;
83368344 data = model . baseModel . getters ( 'find' ) ( id ) ;
83378345 args = args || { } ;
@@ -8340,11 +8348,21 @@ var VuexORMApollo = /** @class */ (function () {
83408348 return [ 4 /*yield*/ , this . mutate ( mutationName , args , dispatch , model , false ) ] ;
83418349 case 1 :
83428350 _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*/ ] ;
83488366 }
83498367 } ) ;
83508368 } ) ;
@@ -8404,7 +8422,6 @@ var VuexORMApollo = /** @class */ (function () {
84048422 return [ 4 /*yield*/ , this . mutate ( mutationName , args , dispatch , model , false ) ] ;
84058423 case 1 :
84068424 _c . sent ( ) ;
8407- // TODO is this really necessary?
84088425 return [ 2 /*return*/ , model . baseModel . getters ( 'find' ) ( data . id ) ] ;
84098426 case 2 : return [ 2 /*return*/ ] ;
84108427 }
@@ -8452,17 +8469,19 @@ var VuexORMApollo = /** @class */ (function () {
84528469 return __generator ( this , function ( _a ) {
84538470 switch ( _a . label ) {
84548471 case 0 :
8455- if ( ! variables ) return [ 3 /*break*/ , 2 ] ;
8472+ if ( ! variables ) return [ 3 /*break*/ , 4 ] ;
84568473 id = variables . id ? variables . id : undefined ;
84578474 query = this . queryBuilder . buildQuery ( 'mutation' , model , name , variables , multiple ) ;
84588475 return [ 4 /*yield*/ , this . apolloRequest ( model , query , variables , true ) ] ;
84598476 case 1 :
84608477 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*/ ] ;
84668485 }
84678486 } ) ;
84688487 } ) ;
0 commit comments