Skip to content

Commit 5e92a6b

Browse files
committed
Include belongsTo related objects into persist
1 parent b916726 commit 5e92a6b

File tree

6 files changed

+88
-23
lines changed

6 files changed

+88
-23
lines changed

dist/vuex-orm-apollo.esm.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9347,12 +9347,17 @@ var QueryBuilder = /** @class */ (function () {
93479347
var returnValue = {};
93489348
Object.keys(data).forEach(function (key) {
93499349
var value = data[key];
9350-
// Ignore connections and empty fields
9351-
if (!relations.has(key) && !key.startsWith('$') && value !== null) {
9350+
// Ignore hasMany/One connections, empty fields and internal fields ($)
9351+
if ((!relations.has(key) || relations.get(key) instanceof _this.context.components.BelongsTo) &&
9352+
!key.startsWith('$') && value !== null) {
93529353
if (value instanceof Array) {
93539354
var arrayModel_1 = _this.getModel(inflection.singularize(key));
93549355
returnValue[key] = value.map(function (v) { return _this.transformOutgoingData(arrayModel_1 || model, v); });
93559356
}
9357+
else if (relations.get(key) instanceof _this.context.components.BelongsTo) {
9358+
var relatedModel = _this.getModel(inflection.singularize(key));
9359+
returnValue[key] = _this.transformOutgoingData(relatedModel || model, value);
9360+
}
93569361
else {
93579362
returnValue[key] = value;
93589363
}
@@ -9744,6 +9749,9 @@ var Model = /** @class */ (function () {
97449749
});
97459750
return found;
97469751
};
9752+
Model.prototype.getRecordWithId = function (id) {
9753+
return this.baseModel.getters('query')().withAllRecursive().where('id', id).first();
9754+
};
97479755
Model.prototype.fieldIsNumber = function (field) {
97489756
if (!field)
97499757
return false;
@@ -9962,14 +9970,14 @@ var VuexORMApollo = /** @class */ (function () {
99629970
case 0:
99639971
if (!id) return [3 /*break*/, 4];
99649972
model = this.context.getModel(state.$name);
9965-
data = model.baseModel.getters('find')(id);
9973+
data = model.getRecordWithId(id);
99669974
args = args || {};
99679975
args[model.singularName] = this.queryBuilder.transformOutgoingData(model, data);
99689976
mutationName = "create" + upcaseFirstLetter(model.singularName);
99699977
return [4 /*yield*/, this.mutate(mutationName, args, dispatch, model, false)];
99709978
case 1:
99719979
newRecord = _c.sent();
9972-
oldRecord = model.baseModel.getters('find')(id);
9980+
oldRecord = model.getRecordWithId(id);
99739981
if (!(oldRecord && !oldRecord.$isPersisted)) return [3 /*break*/, 3];
99749982
// The server generated another ID, this is very likely to happen.
99759983
// in this case this.mutate has inserted a new record instead of updating the existing one.

docs/.vuepress/dist/404.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<link rel="stylesheet" href="/vuex-orm-apollo/assets/css/1.styles.9c82df48.css">
1212
</head>
1313
<body>
14-
<div id="app" data-server-rendered="true"><div class="theme-container"><div class="content"><h1>404</h1><blockquote>There's nothing here.</blockquote></div></div></div>
14+
<div id="app" data-server-rendered="true"><div class="theme-container"><div class="content"><h1>404</h1><blockquote>Looks like we've got some broken links.</blockquote></div></div></div>
1515
<script src="/vuex-orm-apollo/assets/js/app.98a767d9.js" defer></script>
1616
</body>
1717
</html>

src/model.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ export default class Model {
117117
return found;
118118
}
119119

120+
public getRecordWithId (id: number) {
121+
return this.baseModel.getters('query')().withAllRecursive().where('id', id).first();
122+
}
123+
120124
public fieldIsNumber (field: Field | undefined): boolean {
121125
if (!field) return false;
122126
return field instanceof this.context.components.Number ||

src/queryBuilder.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,16 @@ export default class QueryBuilder {
138138
Object.keys(data).forEach((key) => {
139139
const value = data[key];
140140

141-
// Ignore connections and empty fields
142-
if (!relations.has(key) && !key.startsWith('$') && value !== null) {
141+
// Ignore hasMany/One connections, empty fields and internal fields ($)
142+
if ((!relations.has(key) || relations.get(key) instanceof this.context.components.BelongsTo) &&
143+
!key.startsWith('$') && value !== null) {
144+
143145
if (value instanceof Array) {
144146
const arrayModel = this.getModel(inflection.singularize(key));
145147
returnValue[key] = value.map((v) => this.transformOutgoingData(arrayModel || model, v));
148+
} else if (relations.get(key) instanceof this.context.components.BelongsTo) {
149+
const relatedModel = this.getModel(inflection.singularize(key));
150+
returnValue[key] = this.transformOutgoingData(relatedModel || model, value);
146151
} else {
147152
returnValue[key] = value;
148153
}

src/vuex-orm-apollo.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,15 @@ export default class VuexORMApollo {
112112
private async persist ({ state, dispatch }: ActionParams, { id, args }: ActionParams): Promise<any> {
113113
if (id) {
114114
const model = this.context.getModel(state.$name);
115-
const data = model.baseModel.getters('find')(id);
115+
const data = model.getRecordWithId(id);
116116

117117
args = args || {};
118118
args[model.singularName] = this.queryBuilder.transformOutgoingData(model, data);
119119

120120
const mutationName = `create${upcaseFirstLetter(model.singularName)}`;
121121
const newRecord = await this.mutate(mutationName, args, dispatch, model, false);
122122

123-
const oldRecord = model.baseModel.getters('find')(id);
123+
const oldRecord = model.getRecordWithId(id);
124124

125125
if (oldRecord && !oldRecord.$isPersisted) {
126126
// The server generated another ID, this is very likely to happen.

test/integration/VuexORMApollo.spec.js

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ describe('VuexORMApollo', () => {
7676

7777
store.dispatch('entities/users/insert', { data: { id: 1, name: 'Charlie Brown' }});
7878
store.dispatch('entities/users/insert', { data: { id: 2, name: 'Peppermint Patty' }});
79-
store.dispatch('entities/posts/insert', { data: { id: 1, userId: 1, title: 'Example post 1', content: 'Foo' }});
80-
store.dispatch('entities/posts/insert', { data: { id: 1, userId: 1, title: 'Example post 2', content: 'Bar' }});
81-
store.dispatch('entities/videos/insert', { data: { id: 1, userId: 1, title: 'Example video', content: 'Video' }});
79+
store.dispatch('entities/posts/insert', { data: { id: 1, otherId: 9, userId: 1, title: 'Example post 1', content: 'Foo' }});
80+
store.dispatch('entities/posts/insert', { data: { id: 2, otherId: 10, userId: 1, title: 'Example post 2', content: 'Bar' }});
81+
store.dispatch('entities/videos/insert', { data: { id: 1, otherId: 11, userId: 1, title: 'Example video', content: 'Video' }});
8282
store.dispatch('entities/comments/insert', { data: { id: 1, userId: 1, subjectId: 1, subjectType: 'videos', content: 'Example comment 1' }});
83-
store.dispatch('entities/comments/insert', { data: { id: 1, userId: 2, subjectId: 1, subjectType: 'posts', content: 'Example comment 2' }});
84-
store.dispatch('entities/comments/insert', { data: { id: 1, userId: 2, subjectId: 2, subjectType: 'posts', content: 'Example comment 3' }});
83+
store.dispatch('entities/comments/insert', { data: { id: 2, userId: 2, subjectId: 1, subjectType: 'posts', content: 'Example comment 2' }});
84+
store.dispatch('entities/comments/insert', { data: { id: 3, userId: 2, subjectId: 2, subjectType: 'posts', content: 'Example comment 3' }});
8585
});
8686

8787
describe('fetch', () => {
@@ -288,24 +288,72 @@ query Users {
288288
it('sends the correct query to the API', async () => {
289289
const response = {
290290
data: {
291-
createUser: {
292-
__typename: 'user',
293-
id: 1,
294-
name: 'Charlie Brown'
291+
createPost: {
292+
__typename: 'post',
293+
id: 42,
294+
otherId: 13548,
295+
title: 'Example post 1',
296+
content: 'Foo',
297+
comments: {
298+
__typename: 'comment',
299+
nodes: [{
300+
__typename: 'comment',
301+
id: 15,
302+
content: 'Works!',
303+
subjectId: 42,
304+
subjectType: 'Post'
305+
}]
306+
},
307+
user: {
308+
__typename: 'user',
309+
id: 1,
310+
name: 'Charlie Brown',
311+
}
295312
}
296313
}
297314
};
298315

299316
const request = await sendWithMockFetch(response, async () => {
300-
await store.dispatch('entities/users/persist', { id: 1 });
317+
await store.dispatch('entities/posts/persist', { id: 1 });
301318
});
302319

303-
expect(request.variables).toEqual({ user: { id: 1, name: 'Charlie Brown' } });
320+
expect(request.variables).toEqual({
321+
post: {
322+
content: "Foo",
323+
id: 1,
324+
otherId: 9,
325+
title: "Example post 1",
326+
userId: 1,
327+
user: {
328+
id: 1,
329+
name: 'Charlie Brown'
330+
}
331+
}
332+
});
333+
334+
304335
expect(request.query).toEqual(`
305-
mutation CreateUser($user: UserInput!) {
306-
createUser(user: $user) {
336+
mutation CreatePost($post: PostInput!) {
337+
createPost(post: $post) {
307338
id
308-
name
339+
content
340+
title
341+
otherId
342+
user {
343+
id
344+
name
345+
__typename
346+
}
347+
comments {
348+
nodes {
349+
id
350+
content
351+
subjectId
352+
subjectType
353+
__typename
354+
}
355+
__typename
356+
}
309357
__typename
310358
}
311359
}

0 commit comments

Comments
 (0)