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

39 lines
1.2 KiB
JavaScript
Raw Normal View History

2018-01-25 15:03:03 -06:00
import { addCallback, addGraphQLSchema, addGraphQLResolvers, addGraphQLMutation } from 'meteor/vulcan:core';
import { performVoteServer } from '../modules/vote.js';
import { VoteableCollections } from '../modules/make_voteable.js';
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);
addGraphQLMutation('vote(documentId: String, voteType: String, collectionName: String, voteId: String) : Voteable');
const voteResolver = {
Mutation: {
async vote(root, {documentId, voteType, 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
const document = await performVoteServer({documentId, voteType, collection, voteId, user: currentUser});
return document;
2017-09-25 22:09:09 +02:00
},
},
};
2017-07-13 17:37:59 +09:00
addGraphQLResolvers(voteResolver);