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

48 lines
2.3 KiB
JavaScript
Raw Normal View History

2016-11-03 21:39:09 +09:00
import Telescope from 'meteor/nova:lib';
2016-11-05 18:37:46 +09:00
import mutations from './mutations.js';
2016-11-03 21:39:09 +09:00
const resolvers = {
Post: {
commenters(post, args, context) {
return post.commenters ? context.Users.find({_id: {$in: post.commenters}}, { fields: context.getViewableFields(context.currentUser, context.Users) }).fetch() : [];
2016-11-03 21:39:09 +09:00
},
comments(post, args, context) {
return post.commentCount ? context.Comments.find({postId: post._id}, { fields: context.getViewableFields(context.currentUser, context.Comments) }).fetch() : [];
2016-11-03 21:39:09 +09:00
},
},
Comment: {
parentComment(comment, args, context) {
2016-11-04 09:31:32 +09:00
return comment.parentCommentId ? context.Comments.findOne({_id: comment.parentCommentId}, { fields: context.getViewableFields(context.currentUser, context.Comments) }) : null;
2016-11-03 21:39:09 +09:00
},
topLevelComment(comment, args, context) {
2016-11-04 09:31:32 +09:00
return comment.topLevelCommentId ? context.Comments.findOne({_id: comment.topLevelCommentId}, { fields: context.getViewableFields(context.currentUser, context.Comments) }) : null;
2016-11-03 21:39:09 +09:00
},
post(comment, args, context) {
2016-11-04 09:31:32 +09:00
return context.Posts.findOne({_id: comment.postId}, { fields: context.getViewableFields(context.currentUser, context.Posts) });
2016-11-03 21:39:09 +09:00
},
user(comment, args, context) {
return context.Users.findOne({_id: comment.userId}, { fields: context.getViewableFields(context.currentUser, context.Users) });
2016-11-03 21:39:09 +09:00
},
upvoters(comment, args, context) {
2016-11-04 09:31:32 +09:00
return comment.upvoters ? context.Users.find({_id: {$in: comment.upvoters}}, { fields: context.getViewableFields(context.currentUser, context.Users) }).fetch() : [];
2016-11-03 21:39:09 +09:00
},
downvoters(comment, args, context) {
2016-11-04 09:31:32 +09:00
return comment.downvoters ? context.Users.find({_id: {$in: comment.downvoters}}, { fields: context.getViewableFields(context.currentUser, context.Users) }).fetch() : [];
2016-11-03 21:39:09 +09:00
},
},
Query: {
2016-11-07 23:46:12 +09:00
comments(root, {postId}, context) {
2016-11-03 21:39:09 +09:00
const options = {
limit: 5,
fields: context.getViewableFields(context.currentUser, context.Comments)
2016-11-03 21:39:09 +09:00
}
2016-11-07 23:46:12 +09:00
return context.Comments.find({postId: postId}, options).fetch();
2016-11-03 21:39:09 +09:00
},
comment(root, args, context) {
return context.Comments.findOne({_id: args._id}, { fields: context.getViewableFields(context.currentUser, context.Comments) });
2016-11-03 21:39:09 +09:00
},
2016-11-05 18:37:46 +09:00
},
Mutation: mutations
2016-11-03 21:39:09 +09:00
};
Telescope.graphQL.addResolvers(resolvers);