Vulcan/packages/nova-posts/lib/resolvers.js

40 lines
1.7 KiB
JavaScript
Raw Normal View History

2016-11-03 21:39:09 +09:00
import Telescope from 'meteor/nova:lib';
2016-11-04 10:28:54 +09:00
import mutations from './mutations.js';
2016-11-03 21:39:09 +09:00
const resolvers = {
Post: {
user(post, args, context) {
2016-11-05 18:37:46 +09:00
return context.Users.findOne({ _id: post.userId }, { fields: context.getViewableFields(context.currentUser, context.Users) });
2016-11-03 21:39:09 +09:00
},
upvoters(post, args, context) {
2016-11-05 18:37:46 +09:00
return post.upvoters ? context.Users.find({_id: {$in: post.upvoters}}, { fields: context.getViewableFields(context.currentUser, context.Users) }).fetch() : [];
2016-11-03 21:39:09 +09:00
},
downvoters(post, args, context) {
2016-11-05 18:37:46 +09:00
return post.downvoters ? context.Users.find({_id: {$in: post.downvoters}}, { fields: context.getViewableFields(context.currentUser, context.Users) }).fetch() : [];
2016-11-03 21:39:09 +09:00
},
},
Query: {
posts(root, {terms, offset, limit}, context, info) {
2016-11-04 09:31:32 +09:00
// console.log("// context")
// console.log(context)
2016-11-03 21:39:09 +09:00
let {selector, options} = context.Posts.parameters.get(terms);
const protectedLimit = (limit < 1 || limit > 10) ? 10 : limit;
options.limit = protectedLimit;
options.skip = offset;
// keep only fields that should be viewable by current user
2016-11-05 18:37:46 +09:00
options.fields = context.getViewableFields(context.currentUser, context.Posts);
2016-11-03 21:39:09 +09:00
return context.Posts.find(selector, options).fetch();
},
postsViewTotal(root, {terms}, context) {
const {selector} = context.Posts.parameters.get(terms);
return context.Posts.find(selector).count();
},
post(root, args, context) {
// Meteor._sleepForMs(10000); // wait 2 seconds
2016-11-05 18:37:46 +09:00
return context.Posts.findOne({_id: args._id}, { fields: context.getViewableFields(context.currentUser, context.Posts) });
2016-11-03 21:39:09 +09:00
},
},
2016-11-04 10:28:54 +09:00
Mutation: mutations
2016-11-03 21:39:09 +09:00
};
Telescope.graphQL.addResolvers(resolvers);