Vulcan/packages/vulcan-voting/lib/server/graphql.js

51 lines
1.6 KiB
JavaScript
Raw Normal View History

2017-09-25 22:09:09 +02:00
import { addCallback, addGraphQLSchema, addGraphQLResolvers, addGraphQLMutation, Utils, registerSetting, getSetting } from 'meteor/vulcan:core';
2017-09-27 17:15:49 +02:00
import { performVoteOperation } from '../modules/vote.js';
import { VoteableCollections } from '../modules/make_voteable.js';
import { createError } from 'apollo-errors';
function CreateVoteableUnionType() {
2017-09-25 22:09:09 +02:00
const voteableSchema = VoteableCollections.length ? `union Voteable = ${VoteableCollections.map(collection => collection.typeName).join(' | ')}` : '';
addGraphQLSchema(voteableSchema);
return {}
}
addCallback('graphql.init.before', CreateVoteableUnionType);
const resolverMap = {
Voteable: {
__resolveType(obj, context, info){
return obj.__typename;
},
},
};
2017-07-13 17:37:59 +09:00
addGraphQLResolvers(resolverMap);
2017-09-27 17:15:49 +02:00
addGraphQLMutation('vote(documentId: String, operationType: String, collectionName: String, voteId: String) : Voteable');
const voteResolver = {
Mutation: {
2017-09-27 17:15:49 +02:00
async vote(root, {documentId, operationType, collectionName, voteId}, context) {
2017-09-25 22:09:09 +02:00
const { currentUser } = context;
2017-09-27 17:15:49 +02:00
const collection = context[collectionName];
2017-09-25 22:09:09 +02:00
if (context.Users.canDo(currentUser, `${collectionName.toLowerCase()}.${operationType}`)) {
2017-09-27 17:15:49 +02:00
performVoteOperation({documentId, operationType, collection, voteId, currentUser});
2017-09-25 22:09:09 +02:00
2017-09-27 17:15:49 +02:00
const document = collection.findOne(documentId);
document.__typename = collection.options.typeName;
return document;
} else {
2017-09-27 17:15:49 +02:00
const VoteError = createError('voting.cannot_vote', {message: 'voting.cannot_vote'});
throw new VoteError();
}
},
},
};
2017-07-13 17:37:59 +09:00
addGraphQLResolvers(voteResolver);