2017-01-09 15:42:24 +09:00
|
|
|
import { GraphQLSchema, Utils } from 'meteor/nova:core';
|
2016-12-12 16:43:23 +09:00
|
|
|
import { operateOnItem } from './vote.js';
|
2016-11-22 18:39:50 -05:00
|
|
|
|
|
|
|
const voteSchema = `
|
|
|
|
type Vote {
|
|
|
|
itemId: String
|
|
|
|
power: Float
|
|
|
|
votedAt: String
|
|
|
|
}
|
2017-01-09 15:42:24 +09:00
|
|
|
type VoteResult {
|
|
|
|
_id: String
|
|
|
|
upvoters: [User]
|
|
|
|
upvotes: Float
|
|
|
|
downvoters: [User]
|
|
|
|
downvotes: Float
|
|
|
|
baseScore: Float
|
|
|
|
}
|
2016-11-22 18:39:50 -05:00
|
|
|
`;
|
|
|
|
|
2016-12-12 10:41:50 +09:00
|
|
|
GraphQLSchema.addSchema(voteSchema);
|
2016-11-22 18:39:50 -05:00
|
|
|
|
2017-01-09 15:42:24 +09:00
|
|
|
/*
|
|
|
|
|
|
|
|
Note: although returning a VoteResult object should in theory work,
|
|
|
|
this currently messes the automatic store update on the client.
|
|
|
|
So return a Post for now.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
// GraphQLSchema.addMutation('vote(documentId: String, voteType: String, collectionName: String) : VoteResult');
|
|
|
|
GraphQLSchema.addMutation('vote(documentId: String, voteType: String, collectionName: String) : Post');
|
2016-11-22 18:39:50 -05:00
|
|
|
|
|
|
|
const voteResolver = {
|
|
|
|
Mutation: {
|
2017-01-09 15:42:24 +09:00
|
|
|
vote(root, {documentId, voteType, collectionName}, context) {
|
|
|
|
const collection = context[Utils.capitalize(collectionName)];
|
|
|
|
const document = collection.findOne(documentId);
|
2017-01-10 11:38:42 +01:00
|
|
|
return context.Users.canDo(context.currentUser, `${collectionName.toLowerCase()}.${voteType}`) ? operateOnItem(collection, document, context.currentUser, voteType, false) : false;
|
2016-11-22 18:39:50 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2017-01-10 11:38:42 +01:00
|
|
|
GraphQLSchema.addResolvers(voteResolver);
|