Skip to content

Commit

Permalink
Support typed fields
Browse files Browse the repository at this point in the history
  • Loading branch information
phortx committed Apr 25, 2018
1 parent bae051b commit 6344ad8
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 42 deletions.
44 changes: 36 additions & 8 deletions dist/vuex-orm-apollo.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -7898,6 +7898,7 @@ var QueryBuilder = /** @class */ (function () {
* @returns {String}
*/
QueryBuilder.prototype.buildArguments = function (model, args, signature, filter, allowIdFields) {
var _this = this;
if (signature === void 0) { signature = false; }
if (filter === void 0) { filter = false; }
if (allowIdFields === void 0) { allowIdFields = true; }
Expand All @@ -7924,12 +7925,7 @@ var QueryBuilder = /** @class */ (function () {
}
else {
// Case 1 (String!)
if (typeof value === 'number')
typeOrValue = 'Int';
if (typeof value === 'string')
typeOrValue = 'String';
if (typeof value === 'boolean')
typeOrValue = 'Boolean';
typeOrValue = _this.determineAttributeType(model, key, value);
typeOrValue = typeOrValue + '!';
}
}
Expand All @@ -7949,6 +7945,35 @@ var QueryBuilder = /** @class */ (function () {
}
return returnValue;
};
/**
* Determines the GraphQL primitive type of a field in the variables hash by the field type or (when
* the field type is generic attribute) by the variable type.
* @param {Model} model
* @param {string} key
* @param {string} value
* @returns {string}
*/
QueryBuilder.prototype.determineAttributeType = function (model, key, value) {
var field = model.fields.get(key);
if (field && field instanceof this.context.components.String) {
return 'String';
}
else if (field && field instanceof this.context.components.Number) {
return 'Int';
}
else if (field && field instanceof this.context.components.Boolean) {
return 'Boolean';
}
else {
if (typeof value === 'number')
return 'Int';
if (typeof value === 'string')
return 'String';
if (typeof value === 'boolean')
return 'Boolean';
}
throw new Error("Can't find suitable graphql type for variable " + key + " for model " + model.singularName);
};
/**
*
* @param {Model} model
Expand Down Expand Up @@ -8140,8 +8165,11 @@ var Model = /** @class */ (function () {
return relations;
};
Model.prototype.fieldIsAttribute = function (field) {
return field instanceof this.context.components.Attr ||
field instanceof this.context.components.Increment;
return field instanceof this.context.components.Increment ||
field instanceof this.context.components.Attr ||
field instanceof this.context.components.String ||
field instanceof this.context.components.Number ||
field instanceof this.context.components.Boolean;
};
return Model;
}());
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"@types/graphql": "^0.12.3",
"@types/inflection": "^1.5.28",
"@types/lodash-es": "^4.17.0",
"@vuex-orm/core": "^0.24.4",
"@vuex-orm/core": "^0.24.5",
"apollo-cache-inmemory": "^1.1.7",
"apollo-client": "^2.2.2",
"apollo-link": "^1.2.0",
Expand Down
9 changes: 6 additions & 3 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export default class Model {
public readonly singularName: string;
public readonly pluralName: string;
public readonly baseModel: ORMModel;
public readonly fields: Map<string, Field> = new Map<string, Field>();
private readonly context: Context;
private readonly fields: Map<string, Field> = new Map<string, Field>();

public constructor (baseModel: ORMModel, context: Context) {
this.baseModel = baseModel;
Expand Down Expand Up @@ -81,7 +81,10 @@ export default class Model {
}

private fieldIsAttribute (field: Field): boolean {
return field instanceof this.context.components.Attr ||
field instanceof this.context.components.Increment;
return field instanceof this.context.components.Increment ||
field instanceof this.context.components.Attr ||
field instanceof this.context.components.String ||
field instanceof this.context.components.Number ||
field instanceof this.context.components.Boolean;
}
}
31 changes: 27 additions & 4 deletions src/queryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,7 @@ export default class QueryBuilder {
typeOrValue = 'ID!';
} else {
// Case 1 (String!)
if (typeof value === 'number') typeOrValue = 'Int';
if (typeof value === 'string') typeOrValue = 'String';
if (typeof value === 'boolean') typeOrValue = 'Boolean';

typeOrValue = this.determineAttributeType(model, key, value);
typeOrValue = typeOrValue + '!';
}
} else {
Expand All @@ -280,6 +277,32 @@ export default class QueryBuilder {
return returnValue;
}

/**
* Determines the GraphQL primitive type of a field in the variables hash by the field type or (when
* the field type is generic attribute) by the variable type.
* @param {Model} model
* @param {string} key
* @param {string} value
* @returns {string}
*/
private determineAttributeType (model: Model, key: string, value: any): string {
const field: undefined | Field = model.fields.get(key);

if (field && field instanceof this.context.components.String) {
return 'String';
} else if (field && field instanceof this.context.components.Number) {
return 'Int';
} else if (field && field instanceof this.context.components.Boolean) {
return 'Boolean';
} else {
if (typeof value === 'number') return 'Int';
if (typeof value === 'string') return 'String';
if (typeof value === 'boolean') return 'Boolean';
}

throw new Error(`Can't find suitable graphql type for variable ${key} for model ${model.singularName}`);
}

/**
*
* @param {Model} model
Expand Down
16 changes: 8 additions & 8 deletions test/integration/VuexORMApollo.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class User extends ORMModel {
static fields () {
return {
id: this.increment(null),
name: this.attr(null),
name: this.string(null),
posts: this.hasMany(Post, 'userId'),
comments: this.hasMany(Comment, 'userId')
};
Expand All @@ -26,10 +26,10 @@ class Post extends ORMModel {
static fields () {
return {
id: this.increment(null),
content: this.attr(''),
title: this.attr(''),
userId: this.attr(null),
otherId: this.attr(0), // This is a field which ends with `Id` but doesn't belong to any relation
content: this.string(''),
title: this.string(''),
userId: this.number(null),
otherId: this.number(0), // This is a field which ends with `Id` but doesn't belong to any relation
user: this.belongsTo(User, 'userId'),
comments: this.hasMany(Comment, 'userId')
};
Expand All @@ -43,9 +43,9 @@ class Comment extends ORMModel {
static fields () {
return {
id: this.increment(null),
content: this.attr(''),
userId: this.attr(null),
postId: this.attr(null),
content: this.string(''),
userId: this.number(null),
postId: this.number(null),
user: this.belongsTo(User, 'userId'),
post: this.belongsTo(Post, 'postId')
};
Expand Down
14 changes: 7 additions & 7 deletions test/unit/Model.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ class User extends ORMModel {

static fields () {
return {
id: this.attr(null),
name: this.attr(null),
profile: this.hasOne(Profile, 'user_id')
id: this.increment(null),
name: this.string(null),
profile: this.hasOne(Profile, 'userId')
};
}
}
Expand All @@ -23,15 +23,15 @@ class Profile extends ORMModel {

static fields () {
return {
id: this.attr(null),
user_id: this.attr(null)
id: this.increment(null),
userId: this.number(null)
};
}
}

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

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

expect(relations.has('profile')).toEqual(true);
expect(relations.get('profile')).toEqual({
foreignKey: "user_id", localKey: "id", model: User, "related": Profile
foreignKey: "userId", localKey: "id", model: User, "related": Profile
});
});
});
Expand Down
16 changes: 8 additions & 8 deletions test/unit/QueryBuilder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class User extends ORMModel {
static fields () {
return {
id: this.increment(null),
name: this.attr(null),
name: this.string(null),
posts: this.hasMany(Post, 'userId'),
comments: this.hasMany(Comment, 'userId')
};
Expand All @@ -31,10 +31,10 @@ class Post extends ORMModel {
static fields () {
return {
id: this.increment(null),
content: this.attr(''),
title: this.attr(''),
otherId: this.attr(0),
userId: this.attr(0),
content: this.string(''),
title: this.string(''),
otherId: this.number(0),
userId: this.number(0),
user: this.belongsTo(User, 'userId'),
comments: this.hasMany(Comment, 'postId')
};
Expand All @@ -48,9 +48,9 @@ class Comment extends ORMModel {
static fields () {
return {
id: this.increment(null),
content: this.attr(''),
userId: this.attr(0),
postId: this.attr(0),
content: this.string(''),
userId: this.number(0),
postId: this.number(0),
user: this.belongsTo(User, 'userId'),
post: this.belongsTo(Post, 'postId')
};
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@
version "0.5.3"
resolved "https://registry.yarnpkg.com/@types/zen-observable/-/zen-observable-0.5.3.tgz#91b728599544efbb7386d8b6633693a3c2e7ade5"

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

abbrev@1:
version "1.1.1"
Expand Down

0 comments on commit 6344ad8

Please sign in to comment.