@@ -118,12 +118,19 @@ export default class VuexORMApollo {
118118 const mutationName = `create${ upcaseFirstLetter ( model . singularName ) } ` ;
119119 await this . mutate ( mutationName , args , dispatch , model , false ) ;
120120
121- const record = model . baseModel . getters ( 'find' ) ( id ) ;
122-
123- record . $isPersisted = true ;
124- record . $dispatch ( 'update' , { where : record . id , data : record } ) ;
121+ const oldRecord = model . baseModel . getters ( 'find' ) ( id ) ;
122+
123+ this . context . logger . log ( oldRecord ) ;
124+ if ( oldRecord && ! oldRecord . $isPersisted ) {
125+ // The server generated another ID, this is very likely to happen.
126+ // in this case this.mutate has inserted a new record instead of updating the existing one.
127+ // We can see that because $isPersisted is still false then.
128+ this . context . logger . log ( 'Dropping deprecated record with ID' , oldRecord . id ) ;
129+ await model . baseModel . dispatch ( 'delete' , { where : oldRecord . id } ) ;
130+ }
125131
126- return record ;
132+ // TODO is this save?
133+ return model . baseModel . getters ( 'query' ) ( ) . withAll ( ) . last ( ) ;
127134 }
128135 }
129136
@@ -174,7 +181,6 @@ export default class VuexORMApollo {
174181 const mutationName = `update${ upcaseFirstLetter ( model . singularName ) } ` ;
175182 await this . mutate ( mutationName , args , dispatch , model , false ) ;
176183
177- // TODO is this really necessary?
178184 return model . baseModel . getters ( 'find' ) ( data . id ) ;
179185 }
180186 }
@@ -218,7 +224,8 @@ export default class VuexORMApollo {
218224 const newData = await this . apolloRequest ( model , query , variables , true ) ;
219225
220226 if ( name !== `delete${ upcaseFirstLetter ( model . singularName ) } ` ) {
221- return this . insertData ( newData , dispatch ) ;
227+ await this . insertData ( newData , dispatch ) ;
228+ return true ; // FIXME RETURN THE NEW RECORD!!
222229 }
223230
224231 return true ;
0 commit comments