@@ -118,12 +118,19 @@ export default class VuexORMApollo {
118
118
const mutationName = `create${ upcaseFirstLetter ( model . singularName ) } ` ;
119
119
await this . mutate ( mutationName , args , dispatch , model , false ) ;
120
120
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
+ }
125
131
126
- return record ;
132
+ // TODO is this save?
133
+ return model . baseModel . getters ( 'query' ) ( ) . withAll ( ) . last ( ) ;
127
134
}
128
135
}
129
136
@@ -174,7 +181,6 @@ export default class VuexORMApollo {
174
181
const mutationName = `update${ upcaseFirstLetter ( model . singularName ) } ` ;
175
182
await this . mutate ( mutationName , args , dispatch , model , false ) ;
176
183
177
- // TODO is this really necessary?
178
184
return model . baseModel . getters ( 'find' ) ( data . id ) ;
179
185
}
180
186
}
@@ -218,7 +224,8 @@ export default class VuexORMApollo {
218
224
const newData = await this . apolloRequest ( model , query , variables , true ) ;
219
225
220
226
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!!
222
229
}
223
230
224
231
return true ;
0 commit comments