Vulcan/packages/nova-base-apollo/lib/resolvers.js

127 lines
5 KiB
JavaScript
Raw Normal View History

import Posts from 'meteor/nova:posts';
import Users from 'meteor/nova:users';
import Comments from 'meteor/nova:comments';
import Categories from 'meteor/nova:categories';
2016-10-29 16:17:14 +09:00
// shortcut
const gVF = Users.getViewableFields;
const resolvers = {
Post: {
user(post, args, context) {
2016-10-29 16:17:14 +09:00
return Users.findOne({ _id: post.userId }, { fields: gVF(context.currentUser, Users) });
},
commenters(post, args, context) {
2016-10-29 16:17:14 +09:00
return post.commenters ? Users.find({_id: {$in: post.commenters}}, { fields: gVF(context.currentUser, Users) }).fetch() : [];
},
comments(post, args, context) {
2016-10-29 16:17:14 +09:00
return post.commentCount ? Comments.find({postId: post._id}, { fields: gVF(context.currentUser, Comments) }).fetch() : [];
},
upvoters(post, args, context) {
2016-10-29 16:17:14 +09:00
return post.upvoters ? Users.find({_id: {$in: post.upvoters}}, { fields: gVF(context.currentUser, Users) }).fetch() : [];
},
downvoters(post, args, context) {
2016-10-29 16:17:14 +09:00
return post.downvoters ? Users.find({_id: {$in: post.downvoters}}, { fields: gVF(context.currentUser, Users) }).fetch() : [];
},
categories(post, args, context) {
2016-10-29 16:17:14 +09:00
return post.categories ? Categories.find({_id: {$in: post.categories}}, { fields: gVF(context.currentUser, Categories) }).fetch() : [];
},
},
User: {
telescope(user, args, context) {
return user.telescope;
},
},
UserTelescope: {
downvotedComments(telescope, args, context) {
return telescope.downvotedComments ? telescope.downvotedComments : []
},
downvotedPosts(telescope, args, context) {
return telescope.downvotedPosts ? telescope.downvotedPosts : []
},
upvotedComments(telescope, args, context) {
return telescope.upvotedComments ? telescope.upvotedComments : []
},
upvotedPosts(telescope, args, context) {
return telescope.upvotedPosts ? telescope.upvotedPosts : [];
},
},
Comment: {
parentComment(comment, args, context) {
2016-10-29 16:17:14 +09:00
return comment.parentCommentId ? Comments.findOne({_id: comment.parentCommentId}, { fields: gVF(context.currentUser, Comments) }) : null;
},
topLevelComment(comment, args, context) {
2016-10-29 16:17:14 +09:00
return comment.topLevelCommentId ? Comments.findOne({_id: comment.topLevelCommentId}, { fields: gVF(context.currentUser, Comments) }) : null;
},
post(comment, args, context) {
2016-10-29 16:17:14 +09:00
return Posts.findOne({_id: comment.postId}, { fields: gVF(context.currentUser, Posts) });
},
user(comment, args, context) {
2016-10-29 16:17:14 +09:00
return Users.findOne({_id: comment.userId}, { fields: gVF(context.currentUser, Posts) });
},
upvoters(comment, args, context) {
2016-10-29 16:17:14 +09:00
return comment.upvoters ? Users.find({_id: {$in: comment.upvoters}}, { fields: gVF(context.currentUser, Users) }).fetch() : [];
},
downvoters(comment, args, context) {
2016-10-29 16:17:14 +09:00
return comment.downvoters ? Users.find({_id: {$in: comment.downvoters}}, { fields: gVF(context.currentUser, Users) }).fetch() : [];
},
},
Category: {
parent(category, args, context) {
2016-10-29 16:17:14 +09:00
return category.parent ? Categories.findOne({_id: category.parent }, { fields: gVF(context.currentUser, Categories) }) : null;
}
},
Query: {
posts(root, {terms, offset, limit}, context, info) {
let {selector, options} = Posts.parameters.get(terms);
2016-10-25 16:24:03 +09:00
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-10-29 16:17:14 +09:00
options.fields = gVF(context.currentUser, Posts);
return Posts.find(selector, options).fetch();
},
postsViewTotal(root, {terms}, context) {
const {selector} = Posts.parameters.get(terms);
return Posts.find(selector).count();
},
post(root, args, context) {
2016-10-29 16:17:14 +09:00
return Posts.findOne({_id: args._id}, { fields: gVF(context.currentUser, Posts) });
},
users(root, args, context) {
2016-10-29 16:02:43 +09:00
const options = {
limit: 5,
2016-10-29 16:17:14 +09:00
fields: gVF(context.currentUser, Users)
2016-10-29 16:02:43 +09:00
}
return Users.find({}, {limit: 5}).fetch();
},
user(root, args, context) {
2016-10-29 16:17:14 +09:00
return Users.findOne({$or: [{_id: args._id}, {'telescope.slug': args.slug}]}, { fields: gVF(context.currentUser, Users) });
},
currentUser(root, args, context) {
return context && context.userId ? Meteor.users.findOne(context.userId) : null;
},
comments(root, args, context) {
2016-10-29 16:02:43 +09:00
const options = {
limit: 5,
fields: Users.getViewableFields(context.currentUser, Comments)
}
return Comments.find({}, options).fetch();
},
comment(root, args, context) {
2016-10-29 16:17:14 +09:00
return Comments.findOne({_id: args._id}, { fields: gVF(context.currentUser, Comments) });
},
categories(root, args, context) {
2016-10-29 16:02:43 +09:00
const options = {
limit: 5,
fields: Users.getViewableFields(context.currentUser, Categories)
};
return Categories.find({}, options).fetch();
},
category(root, args, context) {
2016-10-29 16:17:14 +09:00
return Categories.findOne({_id: args._id}, { fields: gVF(context.currentUser, Categories) });
},
},
};
export default resolvers;