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
|
|
|
|
2016-11-22 18:14:51 -05:00
|
|
|
const speficicResolvers = {
|
2016-11-03 21:39:09 +09:00
|
|
|
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
|
|
|
},
|
|
|
|
},
|
2016-11-22 18:14:51 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
Telescope.graphQL.addResolvers(speficicResolvers);
|
|
|
|
|
|
|
|
const resolvers = {
|
|
|
|
|
|
|
|
list: {
|
|
|
|
|
|
|
|
name: 'postsList',
|
|
|
|
|
2016-11-30 16:55:34 +09:00
|
|
|
check(user, documents, context) {
|
|
|
|
return true // TODO: write check function
|
|
|
|
},
|
|
|
|
|
2016-11-22 18:14:51 -05:00
|
|
|
resolver(root, {terms, offset, limit}, context, info) {
|
2016-11-30 16:55:34 +09:00
|
|
|
// TODO: call check function
|
2016-11-03 21:39:09 +09:00
|
|
|
let {selector, options} = context.Posts.parameters.get(terms);
|
2016-11-22 18:14:51 -05:00
|
|
|
options.limit = (limit < 1 || limit > 10) ? 10 : limit;
|
2016-11-03 21:39:09 +09:00
|
|
|
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();
|
|
|
|
},
|
2016-11-22 18:14:51 -05:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
single: {
|
|
|
|
|
|
|
|
name: 'postsSingle',
|
|
|
|
|
2016-11-30 16:55:34 +09:00
|
|
|
check(user, document, context) {
|
|
|
|
return true // TODO: write check function
|
|
|
|
const status = context.Posts.getStatusName(document);
|
|
|
|
return Users.isOwner(user, document) ? Users.canDo(`posts.view.${status}.own`) : Users.canDo(`posts.view.${status}.all`)
|
|
|
|
},
|
|
|
|
|
|
|
|
resolver(root, {documentId}, context) {
|
|
|
|
// TODO: call check function
|
|
|
|
return context.Posts.findOne({_id: documentId}, { fields: context.getViewableFields(context.currentUser, context.Posts) });
|
2016-11-03 21:39:09 +09:00
|
|
|
},
|
2016-11-22 18:14:51 -05:00
|
|
|
|
2016-11-03 21:39:09 +09:00
|
|
|
},
|
2016-11-08 15:04:17 +09:00
|
|
|
|
2016-11-22 18:14:51 -05:00
|
|
|
total: {
|
|
|
|
|
|
|
|
name: 'postsTotal',
|
|
|
|
|
|
|
|
resolver(root, {terms}, context) {
|
|
|
|
const {selector} = context.Posts.parameters.get(terms);
|
|
|
|
return context.Posts.find(selector).count();
|
|
|
|
},
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
2016-11-18 16:01:27 +09:00
|
|
|
|
2016-11-22 18:14:51 -05:00
|
|
|
export default resolvers;
|