2017-03-23 16:27:59 +09:00
|
|
|
import { GraphQLSchema } from 'meteor/vulcan:core';
|
2016-11-03 21:39:09 +09:00
|
|
|
|
2016-11-22 18:14:51 -05:00
|
|
|
// add these specific resolvers separately
|
|
|
|
const specificResolvers = {
|
2016-11-03 21:39:09 +09:00
|
|
|
Post: {
|
2017-04-15 16:30:31 +09:00
|
|
|
async categories(post, args, {currentUser, Users, Categories}) {
|
|
|
|
if (!post.categories) return [];
|
|
|
|
const categories = await Categories.loader.loadMany(post.categories);
|
|
|
|
return Users.restrictViewableFields(currentUser, Categories, categories);
|
2016-11-03 21:39:09 +09:00
|
|
|
},
|
|
|
|
},
|
2017-02-06 10:50:48 +09:00
|
|
|
Category: {
|
2017-04-15 16:30:31 +09:00
|
|
|
async parent(category, args, {currentUser, Users, Categories}) {
|
|
|
|
if (!category.parentId) return null;
|
|
|
|
const parent = await Categories.loader.load(category.parentId);
|
|
|
|
return Users.restrictViewableFields(currentUser, Categories, parent);
|
2017-02-06 10:50:48 +09:00
|
|
|
}
|
|
|
|
},
|
2016-11-22 18:14:51 -05:00
|
|
|
};
|
2016-12-12 10:41:50 +09:00
|
|
|
GraphQLSchema.addResolvers(specificResolvers);
|
2016-11-22 18:14:51 -05:00
|
|
|
|
|
|
|
// root resolvers: basic list, single, and total query resolvers
|
|
|
|
const resolvers = {
|
|
|
|
|
|
|
|
list: {
|
|
|
|
|
|
|
|
name: 'categoriesList',
|
|
|
|
|
2017-03-05 13:10:16 +00:00
|
|
|
resolver(root, {terms}, context, info) {
|
2017-01-05 15:53:41 +01:00
|
|
|
let {selector, options} = context.Categories.getParameters(terms);
|
|
|
|
|
2017-01-08 16:25:30 +09:00
|
|
|
options.limit = terms.limit;
|
|
|
|
options.skip = terms.offset;
|
2017-01-05 15:53:41 +01:00
|
|
|
options.fields = context.getViewableFields(context.currentUser, context.Categories);
|
|
|
|
|
2017-03-05 13:10:16 +00:00
|
|
|
return context.Categories.find(selector, options).fetch();
|
2016-11-03 21:39:09 +09:00
|
|
|
},
|
2016-11-22 18:14:51 -05:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
single: {
|
2016-12-08 23:48:16 +01:00
|
|
|
|
2016-11-22 18:14:51 -05:00
|
|
|
name: 'categoriesSingle',
|
2016-12-08 23:48:16 +01:00
|
|
|
|
2017-03-05 13:10:16 +00:00
|
|
|
resolver(root, {documentId, slug}, context) {
|
2017-01-13 14:59:19 +01:00
|
|
|
const selector = documentId ? {_id: documentId} : {slug: slug};
|
2017-03-05 13:10:16 +00:00
|
|
|
return context.Categories.findOne(selector, { fields: context.getViewableFields(context.currentUser, context.Categories) });
|
2016-11-03 21:39:09 +09:00
|
|
|
},
|
2016-12-08 23:48:16 +01:00
|
|
|
|
2016-11-03 21:39:09 +09:00
|
|
|
},
|
2016-11-22 18:14:51 -05:00
|
|
|
|
|
|
|
total: {
|
2016-12-08 23:48:16 +01:00
|
|
|
|
2016-11-22 18:14:51 -05:00
|
|
|
name: 'categoriesTotal',
|
2016-12-08 23:48:16 +01:00
|
|
|
|
2017-03-05 13:10:16 +00:00
|
|
|
resolver(root, args, context) {
|
|
|
|
return context.Categories.find().count();
|
2016-11-22 18:14:51 -05:00
|
|
|
},
|
2016-12-08 23:48:16 +01:00
|
|
|
|
2016-11-22 18:14:51 -05:00
|
|
|
}
|
2016-11-03 21:39:09 +09:00
|
|
|
};
|
|
|
|
|
2016-12-08 23:48:16 +01:00
|
|
|
export default resolvers;
|