Skip to content

Commit

Permalink
fix(App): Error on ReferencesMany relationship
Browse files Browse the repository at this point in the history
  • Loading branch information
Tallyb committed Oct 16, 2016
1 parent 7733f45 commit 81f0821
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 8 deletions.
4 changes: 2 additions & 2 deletions common/models/author.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
"foreignKey": ""
},
"friends": {
"type": "hasMany",
"type": "referencesMany",
"model": "Author",
"foreignKey": ""
"foreignKey": "friendIds"
}
},
"acls": [],
Expand Down
4 changes: 2 additions & 2 deletions data.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
"4": "{\"first_name\":\"John\",\"last_name\":\"Steinback\",\"Genre\":\"HUMOR\",\"birth_date\":\"2016-10-12T00:00:00.000Z\",\"id\":4}",
"5": "{\"first_name\":\"Tony\",\"last_name\":\"Morrison\",\"birth_date\":\"1943-12-31T22:00:00.000Z\",\"id\":5}",
"6": "{\"first_name\":\"Virginia\",\"last_name\":\"Wolf\",\"birth_date\":\"2016-10-15T08:54:15.889Z\",\"id\":6}",
"7": "{\"first_name\":\"Mark\",\"last_name\":\"Twain\",\"Genre\":\"HUMOR\",\"birth_date\":\"1789-10-15T00:00:00.000Z\",\"authorId\":4,\"id\":7}",
"8": "{\"first_name\":\"Jane\",\"last_name\":\"Austin\",\"Genre\":\"ROMANCE\",\"birth_date\":\"1883-10-15T00:00:00.000Z\",\"authorId\":4,\"id\":8}"
"7": "{\"first_name\":\"Mark\",\"last_name\":\"Twain\",\"Genre\":\"HUMOR\",\"birth_date\":\"1789-10-15T00:00:00.000Z\",\"id\":7,\"friendIds\":[8,6]}",
"8": "{\"first_name\":\"Jane\",\"last_name\":\"Austin\",\"Genre\":\"ROMANCE\",\"birth_date\":\"1883-10-15T00:00:00.000Z\",\"id\":8,\"friendIds\":[5,7]}"
}
}
}
2 changes: 1 addition & 1 deletion lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const typesMapping = {

function validProps(model) {
return _.pickBy(model.definition.properties, p => {
return !p.deprecated;
return !p.deprecated && !_.isArray(p.type);
});
}

Expand Down
10 changes: 7 additions & 3 deletions lib/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@ function generateModelResolvers(models) {
_.forEach(helper.validRelations(m), r => {
resolver[r.name] = (obj, args, context) => {
if (r.multiple) {
let query = {};
query[r.keyTo] = obj[r.keyFrom];
return r.modelTo.find({ where: query, skip: args.after, limit: args.first });
if (_.isArray(obj[r.keyFrom])) {
return r.modelTo.findByIds(obj[r.keyFrom]);
} else {
let query = {};
query[r.keyTo] = obj[r.keyFrom];
return r.modelTo.find({ where: query, skip: args.after, limit: args.first });
}
} else {
return r.modelTo.findById(obj[r.keyFrom]);
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"test": "mocha --reporter spec --timeout 10000",
"start": "node server/server.js",
"nodemon": "nodemon server/server.js",
"debug": "nodemon server/server.js --debug-brk=5858",
"mocha:coverage": "istanbul cover --root . --include-all-sources --dir ./coverage --report text --report text-summary --report lcov --print none _mocha -- test/**/*.spec.js --reporter spec --timeout 10000",
"test:watch": "mocha -R nyan -w --timeout 10000",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
Expand Down

0 comments on commit 81f0821

Please sign in to comment.