Skip to content

Commit 7578c74

Browse files
committed
Fix ignored id fields
1 parent 7fad057 commit 7578c74

File tree

4 files changed

+122
-106
lines changed

4 files changed

+122
-106
lines changed

dist/vuex-orm-apollo.esm.js

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7811,7 +7811,7 @@ var QueryBuilder = /** @class */ (function () {
78117811
Object.keys(data).forEach(function (key) {
78127812
var value = data[key];
78137813
// Ignore IDs and connections and empty fields
7814-
if (!relations.has(key) && key !== 'id' && value !== null) {
7814+
if (!relations.has(key) && !model.skipField(key) && key !== 'id' && value !== null) {
78157815
returnValue[key] = value;
78167816
}
78177817
});
@@ -8013,7 +8013,12 @@ var Logger = /** @class */ (function () {
80138013
messages[_i] = arguments[_i];
80148014
}
80158015
if (this.enabled) {
8016-
console.group.apply(console, ['[Vuex-ORM-Apollo]'].concat(messages));
8016+
if (process.env.NODE_ENV === 'test') {
8017+
console.group.apply(console, ['[Vuex-ORM-Apollo]'].concat(messages));
8018+
}
8019+
else {
8020+
console.groupCollapsed.apply(console, ['[Vuex-ORM-Apollo]'].concat(messages));
8021+
}
80178022
}
80188023
};
80198024
Logger.prototype.groupEnd = function () {
@@ -8077,12 +8082,32 @@ var Model = /** @class */ (function () {
80778082
var _this = this;
80788083
var fields = [];
80798084
this.fields.forEach(function (field, name) {
8080-
if (_this.fieldIsAttribute(field) && !name.endsWith('Id')) {
8085+
if (_this.fieldIsAttribute(field) && !_this.skipField(name)) {
80818086
fields.push(name);
80828087
}
80838088
});
80848089
return fields;
80858090
};
8091+
/**
8092+
* Tells if a field should be ignored. This is true for fields that start with a `$` and all foreign keys
8093+
* @param {string} field
8094+
* @returns {boolean}
8095+
*/
8096+
Model.prototype.skipField = function (field) {
8097+
var _this = this;
8098+
if (field.startsWith('$'))
8099+
return true;
8100+
var shouldSkipField = false;
8101+
this.getRelations().forEach(function (relation) {
8102+
if ((relation instanceof _this.context.components.BelongsTo || relation instanceof _this.context.components.HasOne) &&
8103+
relation.foreignKey === field) {
8104+
shouldSkipField = true;
8105+
return false;
8106+
}
8107+
return true;
8108+
});
8109+
return shouldSkipField;
8110+
};
80868111
/**
80878112
* @returns {Map<string, Field>} all relations of the model which should be queried
80888113
*/
@@ -8463,9 +8488,13 @@ var VuexORMApollo = /** @class */ (function () {
84638488
var _this = this;
84648489
return __generator(this, function (_a) {
84658490
Object.keys(data).forEach(function (key) { return __awaiter(_this, void 0, void 0, function () {
8491+
var value;
84668492
return __generator(this, function (_a) {
84678493
switch (_a.label) {
8468-
case 0: return [4 /*yield*/, dispatch('insertOrUpdate', { data: data[key] })];
8494+
case 0:
8495+
value = data[key];
8496+
this.context.logger.log('Inserting records', value);
8497+
return [4 /*yield*/, dispatch('insertOrUpdate', { data: value })];
84698498
case 1:
84708499
_a.sent();
84718500
return [2 /*return*/];

src/model.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,38 @@ export default class Model {
3333
const fields: Array<string> = [];
3434

3535
this.fields.forEach((field: Field, name: string) => {
36-
if (this.fieldIsAttribute(field) && !name.endsWith('Id')) {
36+
if (this.fieldIsAttribute(field) && !this.skipField(name)) {
3737
fields.push(name);
3838
}
3939
});
4040

4141
return fields;
4242
}
4343

44+
/**
45+
* Tells if a field should be ignored. This is true for fields that start with a `$` and all foreign keys
46+
* @param {string} field
47+
* @returns {boolean}
48+
*/
49+
public skipField (field: string) {
50+
if (field.startsWith('$')) return true;
51+
52+
let shouldSkipField: boolean = false;
53+
54+
this.getRelations().forEach((relation: Field) => {
55+
if (
56+
(relation instanceof this.context.components.BelongsTo || relation instanceof this.context.components.HasOne) &&
57+
relation.foreignKey === field
58+
) {
59+
shouldSkipField = true;
60+
return false;
61+
}
62+
return true;
63+
});
64+
65+
return shouldSkipField;
66+
}
67+
4468
/**
4569
* @returns {Map<string, Field>} all relations of the model which should be queried
4670
*/

src/queryBuilder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export default class QueryBuilder {
140140
const value = data[key];
141141

142142
// Ignore IDs and connections and empty fields
143-
if (!relations.has(key) && key !== 'id' && value !== null) {
143+
if (!relations.has(key) && !model.skipField(key) && key !== 'id' && value !== null) {
144144
returnValue[key] = value;
145145
}
146146
});

test/integration/VuexORMApollo.spec.js

Lines changed: 63 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class Post extends ORMModel {
2929
content: this.attr(''),
3030
title: this.attr(''),
3131
userId: this.attr(null),
32+
otherId: this.attr(0), // This is a field which ends with `Id` but doesn't belong to any relation
3233
user: this.belongsTo(User, 'userId'),
3334
comments: this.hasMany(Comment, 'userId')
3435
};
@@ -64,33 +65,68 @@ describe('VuexORMApollo', () => {
6465
});
6566

6667
describe('fetch', () => {
68+
it('also requests the otherId field', async () => {
69+
const response = {
70+
data: {
71+
post: {
72+
__typename: 'post',
73+
id: 1,
74+
otherId: 13548,
75+
title: 'Example Post 1',
76+
content: 'Foo',
77+
comments: {
78+
__typename: 'comment',
79+
nodes: []
80+
},
81+
user: {
82+
__typename: 'user',
83+
id: 1,
84+
name: 'Johnny Imba',
85+
}
86+
}
87+
}
88+
};
89+
90+
let request = await sendWithMockFetch(response, async () => {
91+
await store.dispatch('entities/posts/fetch', { filter: { id: 1 } });
92+
});
93+
expect(request).not.toEqual(null);
94+
95+
expect(request.query).toEqual(`
96+
query Post($id: ID!) {
97+
post(id: $id) {
98+
id
99+
content
100+
title
101+
otherId
102+
user {
103+
id
104+
name
105+
__typename
106+
}
107+
comments {
108+
nodes {
109+
id
110+
content
111+
__typename
112+
}
113+
__typename
114+
}
115+
__typename
116+
}
117+
}
118+
`.trim() + "\n");
119+
});
120+
121+
67122
describe('with ID', () => {
68123
it("doesn't cache when bypassCache = true", async () => {
69124
const response = {
70125
data: {
71126
user: {
72127
__typename: 'user',
73128
id: 1,
74-
name: 'Johnny Imba',
75-
posts: {
76-
__typename: 'post',
77-
nodes: [
78-
{
79-
__typename: 'post',
80-
id: 1,
81-
userId: 1,
82-
title: 'Example Post 1',
83-
content: 'Foo'
84-
},
85-
{
86-
__typename: 'post',
87-
id: 2,
88-
userId: 1,
89-
title: 'Example Post 2',
90-
content: 'Bar'
91-
}
92-
]
93-
}
129+
name: 'Johnny Imba'
94130
}
95131
}
96132
};
@@ -117,26 +153,7 @@ describe('VuexORMApollo', () => {
117153
user: {
118154
__typename: 'user',
119155
id: 1,
120-
name: 'Johnny Imba',
121-
posts: {
122-
__typename: 'post',
123-
nodes: [
124-
{
125-
__typename: 'post',
126-
id: 1,
127-
userId: 1,
128-
title: 'Example Post 1',
129-
content: 'Foo'
130-
},
131-
{
132-
__typename: 'post',
133-
id: 2,
134-
userId: 1,
135-
title: 'Example Post 2',
136-
content: 'Bar'
137-
}
138-
]
139-
}
156+
name: 'Johnny Imba'
140157
}
141158
}
142159
};
@@ -168,26 +185,7 @@ query User($id: ID!) {
168185
{
169186
__typename: 'user',
170187
id: 1,
171-
name: 'Charlie Brown',
172-
posts: {
173-
__typename: 'post',
174-
nodes: [
175-
{
176-
__typename: 'post',
177-
id: 1,
178-
userId: 1,
179-
title: 'Example Post 1',
180-
content: 'Foo'
181-
},
182-
{
183-
__typename: 'post',
184-
id: 2,
185-
userId: 1,
186-
title: 'Example Post 2',
187-
content: 'Bar'
188-
}
189-
]
190-
}
188+
name: 'Charlie Brown'
191189
}
192190
]
193191
}
@@ -224,26 +222,7 @@ query Users($active: Boolean!) {
224222
{
225223
__typename: 'user',
226224
id: 1,
227-
name: 'Charlie Brown',
228-
posts: {
229-
__typename: 'post',
230-
nodes: [
231-
{
232-
__typename: 'post',
233-
id: 1,
234-
userId: 1,
235-
title: 'Example Post 1',
236-
content: 'Foo'
237-
},
238-
{
239-
__typename: 'post',
240-
id: 2,
241-
userId: 1,
242-
title: 'Example Post 2',
243-
content: 'Bar'
244-
}
245-
]
246-
}
225+
name: 'Charlie Brown'
247226
}
248227
]
249228
}
@@ -279,11 +258,7 @@ query Users {
279258
createUser: {
280259
__typename: 'user',
281260
id: 1,
282-
name: 'Charlie Brown',
283-
posts: {
284-
__typename: 'post',
285-
nodes: []
286-
}
261+
name: 'Charlie Brown'
287262
}
288263
}
289264
};
@@ -313,11 +288,7 @@ mutation CreateUser($user: UserInput!) {
313288
updateUser: {
314289
__typename: 'user',
315290
id: 1,
316-
name: 'Snoopy',
317-
posts: {
318-
__typename: 'post',
319-
nodes: []
320-
}
291+
name: 'Snoopy'
321292
}
322293
}
323294
};
@@ -350,11 +321,7 @@ mutation UpdateUser($id: ID!, $user: UserInput!) {
350321
deleteUser: {
351322
__typename: 'user',
352323
id: 1,
353-
name: 'Charlie Brown',
354-
posts: {
355-
__typename: 'post',
356-
nodes: []
357-
}
324+
name: 'Charlie Brown'
358325
}
359326
}
360327
};
@@ -385,11 +352,7 @@ mutation DeleteUser($id: ID!) {
385352
signupUser: {
386353
__typename: 'user',
387354
id: 1,
388-
name: 'Charlie Brown',
389-
posts: {
390-
__typename: 'post',
391-
nodes: []
392-
}
355+
name: 'Charlie Brown'
393356
}
394357
}
395358
};

0 commit comments

Comments
 (0)