Vulcan/packages/nova-posts/lib/mutations.js

83 lines
2.4 KiB
JavaScript
Raw Normal View History

2016-11-04 10:28:54 +09:00
import Telescope from 'meteor/nova:lib';
import Posts from './collection.js'
import Users from 'meteor/nova:users';
import Events from "meteor/nova:events";
/**
*
* Post Methods (and Mutations)
*
*/
// define GraphQL mutations
Telescope.graphQL.addMutation('postsNew(post: PostInput) : Post');
2016-11-04 14:38:31 +09:00
Telescope.graphQL.addMutation('postsEdit(postId: String, modifier: PostModifier) : Post');
2016-11-04 10:28:54 +09:00
Telescope.graphQL.addMutation('postsVote(postId: String, voteType: String) : Post');
// resolvers
Posts.mutations = {
postsNew(root, {post}, context) {
console.log("// postsNew")
console.log(post)
2016-11-04 14:38:31 +09:00
context.Posts.simpleSchema().namedContext("posts.new").validate(post);
2016-11-04 10:28:54 +09:00
2016-11-04 14:38:31 +09:00
// post = Telescope.callbacks.run("posts.new.method", post, Meteor.user());
2016-11-04 10:28:54 +09:00
if (Meteor.isServer && this.connection) {
post.userIP = this.connection.clientAddress;
post.userAgent = this.connection.httpHeaders["user-agent"];
}
post = Telescope.callbacks.run("posts.new.sync", post, context.currentUser);
post._id = context.Posts.insert(post);
// note: query for post to get fresh document with collection-hooks effects applied
Telescope.callbacks.runAsync("posts.new.async", context.Posts.findOne(post._id));
return post;
},
2016-11-04 14:38:31 +09:00
postsEdit(root, parameters, context) {
console.log("// postsEdit")
console.log(parameters)
let {postId, modifier} = parameters;
context.Posts.simpleSchema().namedContext("posts.edit").validate(modifier, {modifier: true});
check(postId, String);
let post = context.Posts.findOne(postId);
// modifier = Telescope.callbacks.run("posts.edit.method", modifier, post, Meteor.user());
if (typeof post === "undefined") {
post = context.Posts.findOne(postId);
}
modifier = Telescope.callbacks.run("posts.edit.sync", modifier, post);
context.Posts.update(postId, modifier);
Telescope.callbacks.runAsync("posts.edit.async", context.Posts.findOne(postId), post);
return context.Posts.findOne(postId);
},
2016-11-04 10:28:54 +09:00
postsVote(root, {postId, voteType}, context) {
Meteor._sleepForMs(2000); // wait 2 seconds for demonstration purpose
console.log("sleep done");
const post = Posts.findOne(postId);
return context.Users.canDo(context.currentUser, `posts.${voteType}`) ? Telescope.operateOnItem(context.Posts, post, context.currentUser, voteType) : false;
},
};
export default Posts.mutations;