Skip to content

Commit 6344ad8

Browse files
committed
Support typed fields
1 parent bae051b commit 6344ad8

File tree

8 files changed

+96
-42
lines changed

8 files changed

+96
-42
lines changed

dist/vuex-orm-apollo.esm.js

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7898,6 +7898,7 @@ var QueryBuilder = /** @class */ (function () {
78987898
* @returns {String}
78997899
*/
79007900
QueryBuilder.prototype.buildArguments = function (model, args, signature, filter, allowIdFields) {
7901+
var _this = this;
79017902
if (signature === void 0) { signature = false; }
79027903
if (filter === void 0) { filter = false; }
79037904
if (allowIdFields === void 0) { allowIdFields = true; }
@@ -7924,12 +7925,7 @@ var QueryBuilder = /** @class */ (function () {
79247925
}
79257926
else {
79267927
// Case 1 (String!)
7927-
if (typeof value === 'number')
7928-
typeOrValue = 'Int';
7929-
if (typeof value === 'string')
7930-
typeOrValue = 'String';
7931-
if (typeof value === 'boolean')
7932-
typeOrValue = 'Boolean';
7928+
typeOrValue = _this.determineAttributeType(model, key, value);
79337929
typeOrValue = typeOrValue + '!';
79347930
}
79357931
}
@@ -7949,6 +7945,35 @@ var QueryBuilder = /** @class */ (function () {
79497945
}
79507946
return returnValue;
79517947
};
7948+
/**
7949+
* Determines the GraphQL primitive type of a field in the variables hash by the field type or (when
7950+
* the field type is generic attribute) by the variable type.
7951+
* @param {Model} model
7952+
* @param {string} key
7953+
* @param {string} value
7954+
* @returns {string}
7955+
*/
7956+
QueryBuilder.prototype.determineAttributeType = function (model, key, value) {
7957+
var field = model.fields.get(key);
7958+
if (field && field instanceof this.context.components.String) {
7959+
return 'String';
7960+
}
7961+
else if (field && field instanceof this.context.components.Number) {
7962+
return 'Int';
7963+
}
7964+
else if (field && field instanceof this.context.components.Boolean) {
7965+
return 'Boolean';
7966+
}
7967+
else {
7968+
if (typeof value === 'number')
7969+
return 'Int';
7970+
if (typeof value === 'string')
7971+
return 'String';
7972+
if (typeof value === 'boolean')
7973+
return 'Boolean';
7974+
}
7975+
throw new Error("Can't find suitable graphql type for variable " + key + " for model " + model.singularName);
7976+
};
79527977
/**
79537978
*
79547979
* @param {Model} model
@@ -8140,8 +8165,11 @@ var Model = /** @class */ (function () {
81408165
return relations;
81418166
};
81428167
Model.prototype.fieldIsAttribute = function (field) {
8143-
return field instanceof this.context.components.Attr ||
8144-
field instanceof this.context.components.Increment;
8168+
return field instanceof this.context.components.Increment ||
8169+
field instanceof this.context.components.Attr ||
8170+
field instanceof this.context.components.String ||
8171+
field instanceof this.context.components.Number ||
8172+
field instanceof this.context.components.Boolean;
81458173
};
81468174
return Model;
81478175
}());

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"@types/graphql": "^0.12.3",
4545
"@types/inflection": "^1.5.28",
4646
"@types/lodash-es": "^4.17.0",
47-
"@vuex-orm/core": "^0.24.4",
47+
"@vuex-orm/core": "^0.24.5",
4848
"apollo-cache-inmemory": "^1.1.7",
4949
"apollo-client": "^2.2.2",
5050
"apollo-link": "^1.2.0",

src/model.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ export default class Model {
99
public readonly singularName: string;
1010
public readonly pluralName: string;
1111
public readonly baseModel: ORMModel;
12+
public readonly fields: Map<string, Field> = new Map<string, Field>();
1213
private readonly context: Context;
13-
private readonly fields: Map<string, Field> = new Map<string, Field>();
1414

1515
public constructor (baseModel: ORMModel, context: Context) {
1616
this.baseModel = baseModel;
@@ -81,7 +81,10 @@ export default class Model {
8181
}
8282

8383
private fieldIsAttribute (field: Field): boolean {
84-
return field instanceof this.context.components.Attr ||
85-
field instanceof this.context.components.Increment;
84+
return field instanceof this.context.components.Increment ||
85+
field instanceof this.context.components.Attr ||
86+
field instanceof this.context.components.String ||
87+
field instanceof this.context.components.Number ||
88+
field instanceof this.context.components.Boolean;
8689
}
8790
}

src/queryBuilder.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,7 @@ export default class QueryBuilder {
254254
typeOrValue = 'ID!';
255255
} else {
256256
// Case 1 (String!)
257-
if (typeof value === 'number') typeOrValue = 'Int';
258-
if (typeof value === 'string') typeOrValue = 'String';
259-
if (typeof value === 'boolean') typeOrValue = 'Boolean';
260-
257+
typeOrValue = this.determineAttributeType(model, key, value);
261258
typeOrValue = typeOrValue + '!';
262259
}
263260
} else {
@@ -280,6 +277,32 @@ export default class QueryBuilder {
280277
return returnValue;
281278
}
282279

280+
/**
281+
* Determines the GraphQL primitive type of a field in the variables hash by the field type or (when
282+
* the field type is generic attribute) by the variable type.
283+
* @param {Model} model
284+
* @param {string} key
285+
* @param {string} value
286+
* @returns {string}
287+
*/
288+
private determineAttributeType (model: Model, key: string, value: any): string {
289+
const field: undefined | Field = model.fields.get(key);
290+
291+
if (field && field instanceof this.context.components.String) {
292+
return 'String';
293+
} else if (field && field instanceof this.context.components.Number) {
294+
return 'Int';
295+
} else if (field && field instanceof this.context.components.Boolean) {
296+
return 'Boolean';
297+
} else {
298+
if (typeof value === 'number') return 'Int';
299+
if (typeof value === 'string') return 'String';
300+
if (typeof value === 'boolean') return 'Boolean';
301+
}
302+
303+
throw new Error(`Can't find suitable graphql type for variable ${key} for model ${model.singularName}`);
304+
}
305+
283306
/**
284307
*
285308
* @param {Model} model

test/integration/VuexORMApollo.spec.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class User extends ORMModel {
1212
static fields () {
1313
return {
1414
id: this.increment(null),
15-
name: this.attr(null),
15+
name: this.string(null),
1616
posts: this.hasMany(Post, 'userId'),
1717
comments: this.hasMany(Comment, 'userId')
1818
};
@@ -26,10 +26,10 @@ class Post extends ORMModel {
2626
static fields () {
2727
return {
2828
id: this.increment(null),
29-
content: this.attr(''),
30-
title: this.attr(''),
31-
userId: this.attr(null),
32-
otherId: this.attr(0), // This is a field which ends with `Id` but doesn't belong to any relation
29+
content: this.string(''),
30+
title: this.string(''),
31+
userId: this.number(null),
32+
otherId: this.number(0), // This is a field which ends with `Id` but doesn't belong to any relation
3333
user: this.belongsTo(User, 'userId'),
3434
comments: this.hasMany(Comment, 'userId')
3535
};
@@ -43,9 +43,9 @@ class Comment extends ORMModel {
4343
static fields () {
4444
return {
4545
id: this.increment(null),
46-
content: this.attr(''),
47-
userId: this.attr(null),
48-
postId: this.attr(null),
46+
content: this.string(''),
47+
userId: this.number(null),
48+
postId: this.number(null),
4949
user: this.belongsTo(User, 'userId'),
5050
post: this.belongsTo(Post, 'postId')
5151
};

test/unit/Model.spec.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ class User extends ORMModel {
1111

1212
static fields () {
1313
return {
14-
id: this.attr(null),
15-
name: this.attr(null),
16-
profile: this.hasOne(Profile, 'user_id')
14+
id: this.increment(null),
15+
name: this.string(null),
16+
profile: this.hasOne(Profile, 'userId')
1717
};
1818
}
1919
}
@@ -23,15 +23,15 @@ class Profile extends ORMModel {
2323

2424
static fields () {
2525
return {
26-
id: this.attr(null),
27-
user_id: this.attr(null)
26+
id: this.increment(null),
27+
userId: this.number(null)
2828
};
2929
}
3030
}
3131

3232
beforeEach(() => {
3333
[store, vuexOrmApollo] = createStore([{ model: User }, { model: Profile }]);
34-
store.dispatch('entities/profiles/insert', { data: { id: 1, user_id: 1 }});
34+
store.dispatch('entities/profiles/insert', { data: { id: 1, userId: 1 }});
3535
store.dispatch('entities/users/insert', { data: { id: 1, name: 'Foo Bar', profile: { id: 1 } }});
3636

3737
model = vuexOrmApollo.context.getModel('user');
@@ -68,7 +68,7 @@ describe('Model', () => {
6868

6969
expect(relations.has('profile')).toEqual(true);
7070
expect(relations.get('profile')).toEqual({
71-
foreignKey: "user_id", localKey: "id", model: User, "related": Profile
71+
foreignKey: "userId", localKey: "id", model: User, "related": Profile
7272
});
7373
});
7474
});

test/unit/QueryBuilder.spec.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class User extends ORMModel {
1717
static fields () {
1818
return {
1919
id: this.increment(null),
20-
name: this.attr(null),
20+
name: this.string(null),
2121
posts: this.hasMany(Post, 'userId'),
2222
comments: this.hasMany(Comment, 'userId')
2323
};
@@ -31,10 +31,10 @@ class Post extends ORMModel {
3131
static fields () {
3232
return {
3333
id: this.increment(null),
34-
content: this.attr(''),
35-
title: this.attr(''),
36-
otherId: this.attr(0),
37-
userId: this.attr(0),
34+
content: this.string(''),
35+
title: this.string(''),
36+
otherId: this.number(0),
37+
userId: this.number(0),
3838
user: this.belongsTo(User, 'userId'),
3939
comments: this.hasMany(Comment, 'postId')
4040
};
@@ -48,9 +48,9 @@ class Comment extends ORMModel {
4848
static fields () {
4949
return {
5050
id: this.increment(null),
51-
content: this.attr(''),
52-
userId: this.attr(0),
53-
postId: this.attr(0),
51+
content: this.string(''),
52+
userId: this.number(0),
53+
postId: this.number(0),
5454
user: this.belongsTo(User, 'userId'),
5555
post: this.belongsTo(Post, 'postId')
5656
};

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@
4848
version "0.5.3"
4949
resolved "https://registry.yarnpkg.com/@types/zen-observable/-/zen-observable-0.5.3.tgz#91b728599544efbb7386d8b6633693a3c2e7ade5"
5050

51-
"@vuex-orm/core@^0.24.4":
52-
version "0.24.4"
53-
resolved "https://registry.yarnpkg.com/@vuex-orm/core/-/core-0.24.4.tgz#5f29f2fb1a9351e7eff2019b8348b5bc47d258e1"
51+
"@vuex-orm/core@^0.24.5":
52+
version "0.24.5"
53+
resolved "https://registry.yarnpkg.com/@vuex-orm/core/-/core-0.24.5.tgz#ae92fc031c45bb98db68892022c787f92844f962"
5454

5555
abbrev@1:
5656
version "1.1.1"

0 commit comments

Comments
 (0)