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-08 14:56:39 +09:00
|
|
|
export default resolvers = {
|
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
|
|
|
},
|
|
|
|
},
|
|
|
|
Query: {
|
2016-11-19 20:01:17 +01:00
|
|
|
postsList(root, {terms, offset, limit}, context, info) {
|
2016-11-04 09:31:32 +09:00
|
|
|
// console.log("// context")
|
|
|
|
// console.log(context)
|
2016-11-03 21:39:09 +09:00
|
|
|
let {selector, options} = context.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
|
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-13 13:43:36 +01:00
|
|
|
postsListTotal(root, {terms}, context) {
|
2016-11-03 21:39:09 +09:00
|
|
|
const {selector} = context.Posts.parameters.get(terms);
|
|
|
|
return context.Posts.find(selector).count();
|
|
|
|
},
|
|
|
|
post(root, args, context) {
|
2016-11-04 15:49:42 +09:00
|
|
|
// Meteor._sleepForMs(10000); // wait 2 seconds
|
2016-11-05 18:37:46 +09:00
|
|
|
return context.Posts.findOne({_id: args._id}, { fields: context.getViewableFields(context.currentUser, context.Posts) });
|
2016-11-03 21:39:09 +09:00
|
|
|
},
|
|
|
|
},
|
2016-11-04 10:28:54 +09:00
|
|
|
Mutation: mutations
|
2016-11-03 21:39:09 +09:00
|
|
|
};
|
2016-11-08 15:04:17 +09:00
|
|
|
|
|
|
|
Telescope.graphQL.addResolvers(resolvers);
|
2016-11-18 16:01:27 +09:00
|
|
|
|
|
|
|
Telescope.graphQL.addQuery(`
|
2016-11-19 20:01:17 +01:00
|
|
|
postsList(terms: Terms, offset: Int, limit: Int): [Post]
|
2016-11-18 16:01:27 +09:00
|
|
|
postsListTotal(terms: Terms): Int
|
|
|
|
post(_id: String): Post
|
|
|
|
`);
|