2016-11-22 18:39:50 -05:00
|
|
|
import Telescope from 'meteor/nova:lib';
|
2016-11-29 12:27:26 +01:00
|
|
|
import Posts from 'meteor/nova:posts';
|
2016-12-12 10:41:50 +09:00
|
|
|
import { GraphQLSchema } from 'meteor/nova:core';
|
2016-11-22 18:39:50 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @summary Vote schema
|
|
|
|
* @type {SimpleSchema}
|
|
|
|
*/
|
|
|
|
Telescope.schemas.votes = new SimpleSchema({
|
|
|
|
itemId: {
|
|
|
|
type: String
|
|
|
|
},
|
|
|
|
power: {
|
|
|
|
type: Number,
|
|
|
|
optional: true
|
|
|
|
},
|
|
|
|
votedAt: {
|
|
|
|
type: Date,
|
|
|
|
optional: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const voteSchema = `
|
|
|
|
type Vote {
|
|
|
|
itemId: String
|
|
|
|
power: Float
|
|
|
|
votedAt: String
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2016-12-12 10:41:50 +09:00
|
|
|
GraphQLSchema.addSchema(voteSchema);
|
2016-11-22 18:39:50 -05:00
|
|
|
|
2016-12-12 10:41:50 +09:00
|
|
|
GraphQLSchema.addMutation('postsVote(documentId: String, voteType: String) : Post');
|
2016-11-22 18:39:50 -05:00
|
|
|
|
|
|
|
const voteResolver = {
|
|
|
|
Mutation: {
|
|
|
|
postsVote(root, {documentId, voteType}, context) {
|
|
|
|
Meteor._sleepForMs(2000); // wait 2 seconds for demonstration purpose
|
|
|
|
console.log("sleep done");
|
|
|
|
const post = Posts.findOne(documentId);
|
|
|
|
return context.Users.canDo(context.currentUser, `posts.${voteType}`) ? Telescope.operateOnItem(context.Posts, post, context.currentUser, voteType) : false;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2016-12-12 10:41:50 +09:00
|
|
|
GraphQLSchema.addResolvers(voteResolver);
|