2017-03-23 16:27:59 +09:00
|
|
|
import { GraphQLSchema } from 'meteor/vulcan:core';
|
2016-11-03 21:39:09 +09:00
|
|
|
|
2016-11-22 18:14:51 -05:00
|
|
|
const specificResolvers = {
|
2016-11-03 21:39:09 +09:00
|
|
|
Post: {
|
2017-04-15 16:30:31 +09:00
|
|
|
async commenters(post, args, {currentUser, Users}) {
|
|
|
|
if (!post.commenters) return [];
|
|
|
|
const commenters = await Users.loader.loadMany(post.commenters);
|
|
|
|
return Users.restrictViewableFields(currentUser, Users, commenters);
|
2016-11-03 21:39:09 +09:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Comment: {
|
2017-04-15 16:30:31 +09:00
|
|
|
async parentComment(comment, args, {currentUser, Users, Comments}) {
|
|
|
|
if (!comment.parentCommentId) return null;
|
|
|
|
const parentComment = await Comments.loader.load(comment.parentCommentId);
|
|
|
|
return Users.restrictViewableFields(currentUser, Users, parentComment);
|
2016-11-03 21:39:09 +09:00
|
|
|
},
|
2017-04-15 16:30:31 +09:00
|
|
|
async topLevelComment(comment, args, {currentUser, Users, Comments}) {
|
|
|
|
if (!comment.topLevelCommentId) return null;
|
|
|
|
const topLevelComment = await Comments.loader.load(comment.topLevelCommentId);
|
|
|
|
return Users.restrictViewableFields(currentUser, Users, topLevelComment);
|
2016-11-03 21:39:09 +09:00
|
|
|
},
|
2017-04-15 16:30:31 +09:00
|
|
|
async post(comment, args, {currentUser, Users, Posts}) {
|
|
|
|
if (!comment.postId) return null;
|
|
|
|
const post = await Posts.loader.load(comment.postId);
|
|
|
|
return Users.restrictViewableFields(currentUser, Users, post);
|
2016-11-03 21:39:09 +09:00
|
|
|
},
|
2017-04-15 16:30:31 +09:00
|
|
|
async user(comment, args, {currentUser, Users}) {
|
|
|
|
if (!comment.userId) return null;
|
|
|
|
const user = await Users.loader.load(comment.userId);
|
|
|
|
return Users.restrictViewableFields(currentUser, Users, user);
|
2016-11-03 21:39:09 +09:00
|
|
|
},
|
|
|
|
},
|
2016-11-22 18:14:51 -05:00
|
|
|
};
|
|
|
|
|
2016-12-12 10:41:50 +09:00
|
|
|
GraphQLSchema.addResolvers(specificResolvers);
|
2016-11-22 18:14:51 -05:00
|
|
|
|
|
|
|
// root resolvers: basic list, single, and total query resolvers
|
|
|
|
const resolvers = {
|
|
|
|
|
|
|
|
list: {
|
|
|
|
|
|
|
|
name: 'commentsList',
|
|
|
|
|
2017-04-15 16:30:31 +09:00
|
|
|
resolver(root, {terms}, {currentUser, Users, Comments}) {
|
2016-11-19 20:01:17 +01:00
|
|
|
|
2017-04-15 16:30:31 +09:00
|
|
|
// get selector and options from terms and perform Mongo query
|
|
|
|
let {selector, options} = Comments.getParameters(terms);
|
2017-02-17 19:02:33 +01:00
|
|
|
options.limit = (terms.limit < 1 || terms.limit > 100) ? 100 : terms.limit;
|
2017-01-08 16:25:30 +09:00
|
|
|
options.skip = terms.offset;
|
2017-04-15 16:30:31 +09:00
|
|
|
const comments = Comments.find(selector, options).fetch();
|
|
|
|
|
|
|
|
// restrict documents fields
|
|
|
|
const restrictedComments = Users.restrictViewableFields(currentUser, Comments, comments);
|
|
|
|
|
|
|
|
// prime the cache
|
|
|
|
restrictedComments.forEach(comment => Comments.loader.prime(comment._id, comment));
|
2017-01-05 15:53:41 +01:00
|
|
|
|
2017-04-15 16:30:31 +09:00
|
|
|
return restrictedComments;
|
2016-11-13 13:43:36 +01:00
|
|
|
},
|
2016-11-22 18:14:51 -05:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
single: {
|
|
|
|
|
|
|
|
name: 'commentsSingle',
|
|
|
|
|
2017-04-15 16:30:31 +09:00
|
|
|
async resolver(root, {documentId}, {currentUser, Users, Comments}) {
|
|
|
|
const comment = await Comments.loader.load(documentId);
|
|
|
|
return Users.restrictViewableFields(currentUser, Comments, comment);
|
2016-11-03 21:39:09 +09:00
|
|
|
},
|
2016-11-22 18:14:51 -05:00
|
|
|
|
2016-11-05 18:37:46 +09:00
|
|
|
},
|
2016-11-22 18:14:51 -05:00
|
|
|
|
|
|
|
total: {
|
|
|
|
|
|
|
|
name: 'commentsTotal',
|
2016-12-02 13:43:26 +01:00
|
|
|
// broken because it doesn't take any arguments in the query
|
2017-03-05 13:10:16 +00:00
|
|
|
resolver(root, {terms}, context) {
|
|
|
|
return context.Comments.find({postId: terms.postId}).count();
|
2016-11-22 18:14:51 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
}
|
2016-11-03 21:39:09 +09:00
|
|
|
};
|
|
|
|
|
2017-01-05 15:53:41 +01:00
|
|
|
export default resolvers;
|