Skip to content

Commit 4dd1986

Browse files
committed
Bugfix: not every Id field is of type ID
1 parent e4e8424 commit 4dd1986

File tree

4 files changed

+51
-30
lines changed

4 files changed

+51
-30
lines changed

dist/vuex-orm-apollo.esm.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7754,7 +7754,7 @@ var QueryBuilder = /** @class */ (function () {
77547754
if (allowIdFields === void 0) { allowIdFields = false; }
77557755
model = this.getModel(model);
77567756
ignoreModels.push(model);
7757-
var params = this.buildArguments(args, false, multiple, allowIdFields);
7757+
var params = this.buildArguments(model, args, false, multiple, allowIdFields);
77587758
var fields = "\n " + model.getQueryFields().join(' ') + "\n " + this.buildRelationsQuery(model, ignoreModels) + "\n ";
77597759
if (multiple) {
77607760
return "\n " + (name ? name : model.pluralName) + params + " {\n nodes {\n " + fields + "\n }\n }\n ";
@@ -7791,7 +7791,7 @@ var QueryBuilder = /** @class */ (function () {
77917791
if (!name)
77927792
name = (multiple ? model.pluralName : model.singularName);
77937793
// build query
7794-
var query = type + " " + upcaseFirstLetter(name) + this.buildArguments(args, true, false) + " {\n" +
7794+
var query = type + " " + upcaseFirstLetter(name) + this.buildArguments(model, args, true, false) + " {\n" +
77957795
(" " + this.buildField(model, multiple, args, [], name, true) + "\n") +
77967796
"}";
77977797
return src(query);
@@ -7890,12 +7890,14 @@ var QueryBuilder = /** @class */ (function () {
78907890
* 4) Filter fields with variables (signature = false, filter = true)
78917891
* query users(filter: { active: $active })
78927892
*
7893+
* @param model
78937894
* @param {Arguments | undefined} args
78947895
* @param {boolean} signature When true, then this method generates a query signature instead of key/value pairs
7896+
* @param filter
78957897
* @param {boolean} allowIdFields If true, ID fields will be included in the arguments list
78967898
* @returns {String}
78977899
*/
7898-
QueryBuilder.prototype.buildArguments = function (args, signature, filter, allowIdFields) {
7900+
QueryBuilder.prototype.buildArguments = function (model, args, signature, filter, allowIdFields) {
78997901
if (signature === void 0) { signature = false; }
79007902
if (filter === void 0) { filter = false; }
79017903
if (allowIdFields === void 0) { allowIdFields = true; }
@@ -7906,15 +7908,17 @@ var QueryBuilder = /** @class */ (function () {
79067908
if (args) {
79077909
Object.keys(args).forEach(function (key) {
79087910
var value = args[key];
7911+
var isForeignKey = model.skipField(key);
7912+
var skipFieldDueId = (key === 'id' || isForeignKey) && !allowIdFields;
79097913
// Ignore null fields, ids and connections
7910-
if (value && !(value instanceof Array || (key === 'id' && !allowIdFields))) {
7914+
if (value && !(value instanceof Array || skipFieldDueId)) {
79117915
var typeOrValue = '';
79127916
if (signature) {
79137917
if (typeof value === 'object' && value.__type) {
79147918
// Case 2 (User!)
79157919
typeOrValue = value.__type + 'Input!';
79167920
}
7917-
else if (key === 'id' || key.endsWith('Id')) {
7921+
else if (key === 'id' || isForeignKey) {
79187922
// Case 1 (ID!)
79197923
typeOrValue = 'ID!';
79207924
}
@@ -8349,7 +8353,6 @@ var VuexORMApollo = /** @class */ (function () {
83498353
case 1:
83508354
_c.sent();
83518355
oldRecord = model.baseModel.getters('find')(id);
8352-
this.context.logger.log(oldRecord);
83538356
if (!(oldRecord && !oldRecord.$isPersisted)) return [3 /*break*/, 3];
83548357
// The server generated another ID, this is very likely to happen.
83558358
// in this case this.mutate has inserted a new record instead of updating the existing one.

src/queryBuilder.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export default class QueryBuilder {
6161
model = this.getModel(model);
6262
ignoreModels.push(model);
6363

64-
let params: string = this.buildArguments(args, false, multiple, allowIdFields);
64+
let params: string = this.buildArguments(model, args, false, multiple, allowIdFields);
6565

6666
const fields = `
6767
${model.getQueryFields().join(' ')}
@@ -116,7 +116,7 @@ export default class QueryBuilder {
116116

117117
// build query
118118
const query: string =
119-
`${type} ${upcaseFirstLetter(name)}${this.buildArguments(args, true, false)} {\n` +
119+
`${type} ${upcaseFirstLetter(name)}${this.buildArguments(model, args, true, false)} {\n` +
120120
` ${this.buildField(model, multiple, args, [], name, true)}\n` +
121121
`}`;
122122

@@ -220,12 +220,14 @@ export default class QueryBuilder {
220220
* 4) Filter fields with variables (signature = false, filter = true)
221221
* query users(filter: { active: $active })
222222
*
223+
* @param model
223224
* @param {Arguments | undefined} args
224225
* @param {boolean} signature When true, then this method generates a query signature instead of key/value pairs
226+
* @param filter
225227
* @param {boolean} allowIdFields If true, ID fields will be included in the arguments list
226228
* @returns {String}
227229
*/
228-
private buildArguments (args?: Arguments, signature: boolean = false, filter: boolean = false,
230+
private buildArguments (model: Model, args?: Arguments, signature: boolean = false, filter: boolean = false,
229231
allowIdFields: boolean = true): string {
230232
if (args === undefined) return '';
231233

@@ -236,15 +238,18 @@ export default class QueryBuilder {
236238
Object.keys(args).forEach((key: string) => {
237239
let value: any = args[key];
238240

241+
const isForeignKey = model.skipField(key);
242+
const skipFieldDueId = (key === 'id' || isForeignKey) && !allowIdFields;
243+
239244
// Ignore null fields, ids and connections
240-
if (value && !(value instanceof Array || (key === 'id' && !allowIdFields))) {
245+
if (value && !(value instanceof Array || skipFieldDueId)) {
241246
let typeOrValue: any = '';
242247

243248
if (signature) {
244249
if (typeof value === 'object' && value.__type) {
245250
// Case 2 (User!)
246251
typeOrValue = value.__type + 'Input!';
247-
} else if (key === 'id' || key.endsWith('Id')) { // FIXME not all 'xxxId' fields are of type ID!
252+
} else if (key === 'id' || isForeignKey) {
248253
// Case 1 (ID!)
249254
typeOrValue = 'ID!';
250255
} else {

src/vuex-orm-apollo.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ export default class VuexORMApollo {
120120

121121
const oldRecord = model.baseModel.getters('find')(id);
122122

123-
this.context.logger.log(oldRecord);
124123
if (oldRecord && !oldRecord.$isPersisted) {
125124
// The server generated another ID, this is very likely to happen.
126125
// in this case this.mutate has inserted a new record instead of updating the existing one.

test/unit/QueryBuilder.spec.js

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class Post extends ORMModel {
3333
id: this.increment(null),
3434
content: this.attr(''),
3535
title: this.attr(''),
36+
otherId: this.attr(0),
37+
userId: this.attr(0),
3638
user: this.belongsTo(User, 'userId'),
3739
comments: this.hasMany(Comment, 'postId')
3840
};
@@ -47,6 +49,8 @@ class Comment extends ORMModel {
4749
return {
4850
id: this.increment(null),
4951
content: this.attr(''),
52+
userId: this.attr(0),
53+
postId: this.attr(0),
5054
user: this.belongsTo(User, 'userId'),
5155
post: this.belongsTo(Post, 'postId')
5256
};
@@ -119,36 +123,42 @@ beforeEach(() => {
119123
describe('QueryBuilder', () => {
120124
describe('.buildArguments', () => {
121125
it('can generate signatures', () => {
122-
let args = queryBuilder.buildArguments({
123-
name: 'Foo Bar',
124-
125-
age: 32,
126+
const model = vuexOrmApollo.context.getModel('post');
127+
let args = queryBuilder.buildArguments(model, {
128+
content: 'Foo Bar',
129+
title: 'Example',
130+
otherId: 18,
131+
userId: 5,
126132
user: { __type: 'User' }
127-
}, true);
133+
}, true, false, false);
128134

129-
expect(args).toEqual('($name: String!, $email: String!, $age: Int!, $user: UserInput!)');
135+
expect(args).toEqual('($content: String!, $title: String!, $otherId: Int!, $user: UserInput!)');
130136
});
131137

132138
it('can generate fields with variables', () => {
133-
let args = queryBuilder.buildArguments({
134-
name: 'Foo Bar',
135-
136-
age: 32,
139+
const model = vuexOrmApollo.context.getModel('post');
140+
let args = queryBuilder.buildArguments(model, {
141+
content: 'Foo Bar',
142+
title: 'Example',
143+
otherId: 18,
144+
userId: 5,
137145
user: { __type: 'User' }
138-
}, false, false, true);
146+
}, false, false, false);
139147

140-
expect(args).toEqual('(name: $name, email: $email, age: $age, user: $user)');
148+
expect(args).toEqual('(content: $content, title: $title, otherId: $otherId, user: $user)');
141149
});
142150

143151
it('can generate filter field with variables', () => {
144-
let args = queryBuilder.buildArguments({
145-
name: 'Foo Bar',
146-
147-
age: 32,
152+
const model = vuexOrmApollo.context.getModel('post');
153+
let args = queryBuilder.buildArguments(model, {
154+
content: 'Foo Bar',
155+
title: 'Example',
156+
otherId: 18,
157+
userId: 5,
148158
user: { __type: 'User' }
149-
}, false, true, true);
159+
}, false, true, false);
150160

151-
expect(args).toEqual('(filter: { name: $name, email: $email, age: $age, user: $user })');
161+
expect(args).toEqual('(filter: { content: $content, title: $title, otherId: $otherId, user: $user })');
152162
});
153163
});
154164

@@ -331,6 +341,7 @@ query test {
331341
id
332342
content
333343
title
344+
otherId
334345
comments {
335346
nodes {
336347
id
@@ -380,6 +391,7 @@ query Posts {
380391
id
381392
content
382393
title
394+
otherId
383395
user {
384396
id
385397
name
@@ -409,6 +421,7 @@ mutation CreatePost($post: PostInput!) {
409421
id
410422
content
411423
title
424+
otherId
412425
user {
413426
id
414427
name
@@ -437,6 +450,7 @@ mutation UpdatePost($id: ID!, $post: PostInput!) {
437450
id
438451
content
439452
title
453+
otherId
440454
user {
441455
id
442456
name

0 commit comments

Comments
 (0)