Skip to content

Commit eaacc26

Browse files
committed
Return the correct record after persisting
1 parent 930a39b commit eaacc26

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

src/vuex-orm-apollo.ts

+14-7
Original file line numberDiff line numberDiff line change
@@ -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;

test/integration/VuexORMApollo.spec.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -385,12 +385,13 @@ mutation SignupUser($user: UserInput!, $captchaToken: String!) {
385385
});
386386

387387
it('is true for persisted records', async () => {
388+
let user = await store.dispatch('entities/users/insert', { data: { name: 'Snoopy' }} );
388389
const response = {
389390
data: {
390391
createUser: {
391392
__typename: 'user',
392-
id: 1,
393-
name: 'Charlie Brown',
393+
id: 15,
394+
name: 'Snoopy',
394395
posts: {
395396
__typename: 'post',
396397
nodes: []
@@ -399,11 +400,12 @@ mutation SignupUser($user: UserInput!, $captchaToken: String!) {
399400
}
400401
};
401402

403+
expect(user.$isPersisted).toBeFalsy();
404+
402405
await sendWithMockFetch(response, async () => {
403-
await store.dispatch('entities/users/persist', { id: 1 });
406+
user = await store.dispatch('entities/users/persist', { id: 1 });
404407
});
405408

406-
const user = store.getters['entities/users/find'](1);
407409
expect(user.$isPersisted).toBeTruthy();
408410
});
409411

0 commit comments

Comments
 (0)