2016-12-12 10:41:50 +09:00
|
|
|
import { GraphQLSchema } from 'meteor/nova:core';
|
2016-11-03 21:39:09 +09:00
|
|
|
|
2017-01-16 12:24:59 +09:00
|
|
|
const specificResolvers = {
|
2016-11-03 21:39:09 +09:00
|
|
|
Post: {
|
2017-03-05 13:10:16 +00:00
|
|
|
user(post, args, context) {
|
|
|
|
return context.Users.findOne({ _id: post.userId }, { fields: context.getViewableFields(context.currentUser, context.Users) });
|
2016-11-03 21:39:09 +09:00
|
|
|
},
|
|
|
|
},
|
2017-02-01 16:37:06 +01:00
|
|
|
Mutation: {
|
2017-03-05 13:10:16 +00:00
|
|
|
increasePostViewCount(root, { postId }, context) {
|
|
|
|
return context.Posts.update({_id: postId}, { $inc: { viewCount: 1 }});
|
2017-02-01 16:37:06 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-22 18:14:51 -05:00
|
|
|
};
|
|
|
|
|
2017-01-16 12:24:59 +09:00
|
|
|
GraphQLSchema.addResolvers(specificResolvers);
|
2016-11-22 18:14:51 -05:00
|
|
|
|
|
|
|
const resolvers = {
|
|
|
|
|
|
|
|
list: {
|
|
|
|
|
|
|
|
name: 'postsList',
|
|
|
|
|
2017-03-05 13:10:16 +00:00
|
|
|
resolver(root, {terms}, context, info) {
|
|
|
|
let {selector, options} = context.Posts.getParameters(terms);
|
|
|
|
options.limit = (terms.limit < 1 || terms.limit > 100) ? 100 : terms.limit;
|
|
|
|
options.skip = terms.offset;
|
|
|
|
options.fields = context.getViewableFields(context.currentUser, context.Posts);
|
|
|
|
return context.Posts.find(selector, options).fetch();
|
2016-11-03 21:39:09 +09:00
|
|
|
},
|
2016-11-22 18:14:51 -05:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
single: {
|
|
|
|
|
|
|
|
name: 'postsSingle',
|
2016-11-30 16:55:34 +09:00
|
|
|
|
2017-03-05 13:10:16 +00:00
|
|
|
resolver(root, {documentId, slug}, context) {
|
|
|
|
const selector = documentId ? {_id: documentId} : {'slug': slug};
|
|
|
|
const post = context.Posts.findOne(selector);
|
|
|
|
return context.Users.keepViewableFields(context.currentUser, context.Posts, post);
|
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',
|
|
|
|
|
2017-03-05 13:10:16 +00:00
|
|
|
resolver(root, {terms}, context) {
|
|
|
|
const {selector} = context.Posts.getParameters(terms);
|
|
|
|
return context.Posts.find(selector).count();
|
2016-11-22 18:14:51 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
2016-11-18 16:01:27 +09:00
|
|
|
|
2017-01-05 15:53:41 +01:00
|
|
|
export default resolvers;
|