Vulcan/packages/nova-base-apollo/lib/resolvers.js
2016-10-29 16:02:43 +09:00

136 lines
No EOL
4.6 KiB
JavaScript

import Posts from 'meteor/nova:posts';
import Users from 'meteor/nova:users';
import Comments from 'meteor/nova:comments';
import Categories from 'meteor/nova:categories';
const resolvers = {
Post: {
user(post, args, context) {
return Users.findOne({_id: post.userId});
},
commenters(post, args, context) {
return post.commenters ? Users.find({_id: {$in: post.commenters}}).fetch() : [];
},
comments(post, args, context) {
return post.commentCount ? Comments.find({postId: post._id}).fetch() : [];
},
upvoters(post, args, context) {
return post.upvoters ? Users.find({_id: {$in: post.upvoters}}).fetch() : [];
},
downvoters(post, args, context) {
return post.downvoters ? Users.find({_id: {$in: post.downvoters}}).fetch() : [];
},
categories(post, args, context) {
return post.categories ? Categories.find({_id: {$in: post.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) {
return comment.parentCommentId ? Comments.findOne({_id: comment.parentCommentId}) : null;
},
topLevelComment(comment, args, context) {
return comment.topLevelCommentId ? Comments.findOne({_id: comment.topLevelCommentId}) : null;
},
post(comment, args, context) {
return Posts.findOne({_id: comment.postId});
},
user(comment, args, context) {
return Users.findOne({_id: comment.userId});
},
upvoters(comment, args, context) {
return comment.upvoters ? Users.find({_id: {$in: comment.upvoters}}).fetch() : [];
},
downvoters(comment, args, context) {
return comment.downvoters ? Users.find({_id: {$in: comment.downvoters}}).fetch() : [];
},
},
Category: {
parent(category, args, context) {
return category.parent ? Categories.findOne({_id: category.parent }) : null;
}
},
Query: {
posts(root, {terms, offset, limit}, context, info) {
let {selector, options} = 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
options.fields = Users.getViewableFields(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) {
const options = {
fields: Users.getViewableFields(context.currentUser, Posts)
}
return Posts.findOne({_id: args._id}, options);
},
users(root, args, context) {
const options = {
limit: 5,
fields: Users.getViewableFields(context.currentUser, Users)
}
return Users.find({}, {limit: 5}).fetch();
},
user(root, args, context) {
const options = {
fields: Users.getViewableFields(context.currentUser, Users)
}
return Users.findOne({$or: [{_id: args._id}, {'telescope.slug': args.slug}]}, options);
},
currentUser(root, args, context) {
return context && context.userId ? Meteor.users.findOne(context.userId) : null;
},
comments(root, args, context) {
const options = {
limit: 5,
fields: Users.getViewableFields(context.currentUser, Comments)
}
return Comments.find({}, options).fetch();
},
comment(root, args, context) {
const options = {
fields: Users.getViewableFields(context.currentUser, Comments)
}
return Comments.findOne({_id: args._id}, options);
},
categories(root, args, context) {
const options = {
limit: 5,
fields: Users.getViewableFields(context.currentUser, Categories)
};
return Categories.find({}, options).fetch();
},
category(root, args, context) {
const options = {
fields: Users.getViewableFields(context.currentUser, Categories)
}
return Categories.findOne({_id: args._id}, options);
},
},
};
export default resolvers;