mirror of
https://github.com/vale981/Vulcan
synced 2025-03-06 01:51:40 -05:00
finish cleaning up resolvers
This commit is contained in:
parent
56bcfba245
commit
b006db3a78
7 changed files with 48 additions and 72 deletions
|
@ -9,8 +9,8 @@ import typeDefs from './schema';
|
|||
|
||||
// import resolvers from './resolvers';
|
||||
|
||||
console.log("// Telescope.graphQL.resolvers")
|
||||
console.log(Telescope.graphQL.resolvers)
|
||||
// console.log("// Telescope.graphQL.resolvers")
|
||||
// console.log(Telescope.graphQL.resolvers)
|
||||
|
||||
const schema = makeExecutableSchema({
|
||||
typeDefs,
|
||||
|
|
|
@ -1,32 +1,24 @@
|
|||
// not used anymore
|
||||
|
||||
import Posts from 'meteor/nova:posts';
|
||||
import Users from 'meteor/nova:users';
|
||||
import Comments from 'meteor/nova:comments';
|
||||
import Categories from 'meteor/nova:categories';
|
||||
|
||||
// shortcut
|
||||
const gVF = Users.getViewableFields;
|
||||
|
||||
const resolvers = {
|
||||
Post: {
|
||||
user(post, args, context) {
|
||||
return Users.findOne({ _id: post.userId }, { fields: gVF(context.currentUser, Users) });
|
||||
return Users.findOne({ _id: post.userId }, { fields: context.getViewableFields(context.currentUser, Users) });
|
||||
},
|
||||
commenters(post, args, context) {
|
||||
return post.commenters ? Users.find({_id: {$in: post.commenters}}, { fields: gVF(context.currentUser, Users) }).fetch() : [];
|
||||
return post.commenters ? Users.find({_id: {$in: post.commenters}}, { fields: context.getViewableFields(context.currentUser, Users) }).fetch() : [];
|
||||
},
|
||||
comments(post, args, context) {
|
||||
return post.commentCount ? Comments.find({postId: post._id}, { fields: gVF(context.currentUser, Comments) }).fetch() : [];
|
||||
return post.commentCount ? Comments.find({postId: post._id}, { fields: context.getViewableFields(context.currentUser, Comments) }).fetch() : [];
|
||||
},
|
||||
upvoters(post, args, context) {
|
||||
return post.upvoters ? Users.find({_id: {$in: post.upvoters}}, { fields: gVF(context.currentUser, Users) }).fetch() : [];
|
||||
return post.upvoters ? Users.find({_id: {$in: post.upvoters}}, { fields: context.getViewableFields(context.currentUser, Users) }).fetch() : [];
|
||||
},
|
||||
downvoters(post, args, context) {
|
||||
return post.downvoters ? Users.find({_id: {$in: post.downvoters}}, { fields: gVF(context.currentUser, Users) }).fetch() : [];
|
||||
return post.downvoters ? Users.find({_id: {$in: post.downvoters}}, { fields: context.getViewableFields(context.currentUser, Users) }).fetch() : [];
|
||||
},
|
||||
categories(post, args, context) {
|
||||
return post.categories ? Categories.find({_id: {$in: post.categories}}, { fields: gVF(context.currentUser, Categories) }).fetch() : [];
|
||||
return post.categories ? Categories.find({_id: {$in: post.categories}}, { fields: context.getViewableFields(context.currentUser, Categories) }).fetch() : [];
|
||||
},
|
||||
},
|
||||
User: {
|
||||
|
@ -50,27 +42,27 @@ const resolvers = {
|
|||
},
|
||||
Comment: {
|
||||
parentComment(comment, args, context) {
|
||||
return comment.parentCommentId ? Comments.findOne({_id: comment.parentCommentId}, { fields: gVF(context.currentUser, Comments) }) : null;
|
||||
return comment.parentCommentId ? Comments.findOne({_id: comment.parentCommentId}, { fields: context.getViewableFields(context.currentUser, Comments) }) : null;
|
||||
},
|
||||
topLevelComment(comment, args, context) {
|
||||
return comment.topLevelCommentId ? Comments.findOne({_id: comment.topLevelCommentId}, { fields: gVF(context.currentUser, Comments) }) : null;
|
||||
return comment.topLevelCommentId ? Comments.findOne({_id: comment.topLevelCommentId}, { fields: context.getViewableFields(context.currentUser, Comments) }) : null;
|
||||
},
|
||||
post(comment, args, context) {
|
||||
return Posts.findOne({_id: comment.postId}, { fields: gVF(context.currentUser, Posts) });
|
||||
return Posts.findOne({_id: comment.postId}, { fields: context.getViewableFields(context.currentUser, Posts) });
|
||||
},
|
||||
user(comment, args, context) {
|
||||
return Users.findOne({_id: comment.userId}, { fields: gVF(context.currentUser, Posts) });
|
||||
return Users.findOne({_id: comment.userId}, { fields: context.getViewableFields(context.currentUser, Posts) });
|
||||
},
|
||||
upvoters(comment, args, context) {
|
||||
return comment.upvoters ? Users.find({_id: {$in: comment.upvoters}}, { fields: gVF(context.currentUser, Users) }).fetch() : [];
|
||||
return comment.upvoters ? Users.find({_id: {$in: comment.upvoters}}, { fields: context.getViewableFields(context.currentUser, Users) }).fetch() : [];
|
||||
},
|
||||
downvoters(comment, args, context) {
|
||||
return comment.downvoters ? Users.find({_id: {$in: comment.downvoters}}, { fields: gVF(context.currentUser, Users) }).fetch() : [];
|
||||
return comment.downvoters ? Users.find({_id: {$in: comment.downvoters}}, { fields: context.getViewableFields(context.currentUser, Users) }).fetch() : [];
|
||||
},
|
||||
},
|
||||
Category: {
|
||||
parent(category, args, context) {
|
||||
return category.parent ? Categories.findOne({_id: category.parent }, { fields: gVF(context.currentUser, Categories) }) : null;
|
||||
return category.parent ? Categories.findOne({_id: category.parent }, { fields: context.getViewableFields(context.currentUser, Categories) }) : null;
|
||||
}
|
||||
},
|
||||
Query: {
|
||||
|
@ -80,7 +72,7 @@ const resolvers = {
|
|||
options.limit = protectedLimit;
|
||||
options.skip = offset;
|
||||
// keep only fields that should be viewable by current user
|
||||
options.fields = gVF(context.currentUser, Posts);
|
||||
options.fields = context.getViewableFields(context.currentUser, Posts);
|
||||
return Posts.find(selector, options).fetch();
|
||||
},
|
||||
postsViewTotal(root, {terms}, context) {
|
||||
|
@ -89,17 +81,17 @@ const resolvers = {
|
|||
},
|
||||
post(root, args, context) {
|
||||
Meteor._sleepForMs(2000); // wait 2 seconds
|
||||
return Posts.findOne({_id: args._id}, { fields: gVF(context.currentUser, Posts) });
|
||||
return Posts.findOne({_id: args._id}, { fields: context.getViewableFields(context.currentUser, Posts) });
|
||||
},
|
||||
users(root, args, context) {
|
||||
const options = {
|
||||
limit: 5,
|
||||
fields: gVF(context.currentUser, Users)
|
||||
fields: context.getViewableFields(context.currentUser, Users)
|
||||
}
|
||||
return Users.find({}, {limit: 5}).fetch();
|
||||
},
|
||||
user(root, args, context) {
|
||||
return Users.findOne({$or: [{_id: args._id}, {'telescope.slug': args.slug}]}, { fields: gVF(context.currentUser, Users) });
|
||||
return Users.findOne({$or: [{_id: args._id}, {'telescope.slug': args.slug}]}, { fields: context.getViewableFields(context.currentUser, Users) });
|
||||
},
|
||||
currentUser(root, args, context) {
|
||||
return context && context.userId ? Meteor.users.findOne(context.userId) : null;
|
||||
|
@ -107,22 +99,22 @@ const resolvers = {
|
|||
comments(root, args, context) {
|
||||
const options = {
|
||||
limit: 5,
|
||||
fields: gVF(context.currentUser, Comments)
|
||||
fields: context.getViewableFields(context.currentUser, Comments)
|
||||
}
|
||||
return Comments.find({}, options).fetch();
|
||||
},
|
||||
comment(root, args, context) {
|
||||
return Comments.findOne({_id: args._id}, { fields: gVF(context.currentUser, Comments) });
|
||||
return Comments.findOne({_id: args._id}, { fields: context.getViewableFields(context.currentUser, Comments) });
|
||||
},
|
||||
categories(root, args, context) {
|
||||
const options = {
|
||||
limit: 5,
|
||||
fields: gVF(context.currentUser, Categories)
|
||||
fields: context.getViewableFields(context.currentUser, Categories)
|
||||
};
|
||||
return Categories.find({}, options).fetch();
|
||||
},
|
||||
category(root, args, context) {
|
||||
return Categories.findOne({_id: args._id}, { fields: gVF(context.currentUser, Categories) });
|
||||
return Categories.findOne({_id: args._id}, { fields: context.getViewableFields(context.currentUser, Categories) });
|
||||
},
|
||||
},
|
||||
Mutation: {
|
||||
|
|
|
@ -90,6 +90,7 @@ export const createApolloServer = (givenOptions = {}, givenConfig = {}) => {
|
|||
|
||||
options.context.userId = user._id;
|
||||
options.context.currentUser = Users.findOne(user._id);
|
||||
options.context.getViewableFields = Users.getViewableFields;
|
||||
|
||||
options.context = deepmerge(options.context, Telescope.graphQL.context);
|
||||
}
|
||||
|
|
|
@ -1,30 +1,26 @@
|
|||
import Telescope from 'meteor/nova:lib';
|
||||
import Users from 'meteor/nova:users';
|
||||
|
||||
// shortcut
|
||||
const gVF = Users.getViewableFields;
|
||||
|
||||
const resolvers = {
|
||||
Post: {
|
||||
categories(post, args, context) {
|
||||
return post.categories ? context.Categories.find({_id: {$in: post.categories}}, { fields: gVF(context.currentUser, context.Categories) }).fetch() : [];
|
||||
return post.categories ? context.Categories.find({_id: {$in: post.categories}}, { fields: context.getViewableFields(context.currentUser, context.Categories) }).fetch() : [];
|
||||
},
|
||||
},
|
||||
Category: {
|
||||
parent(category, args, context) {
|
||||
return category.parent ? context.Categories.findOne({_id: category.parent }, { fields: gVF(context.currentUser, context.Categories) }) : null;
|
||||
return category.parent ? context.Categories.findOne({_id: category.parent }, { fields: context.getViewableFields(context.currentUser, context.Categories) }) : null;
|
||||
}
|
||||
},
|
||||
Query: {
|
||||
categories(root, args, context) {
|
||||
const options = {
|
||||
limit: 5,
|
||||
fields: gVF(context.currentUser, context.Categories)
|
||||
fields: context.getViewableFields(context.currentUser, context.Categories)
|
||||
};
|
||||
return context.Categories.find({}, options).fetch();
|
||||
},
|
||||
category(root, args, context) {
|
||||
return context.Categories.findOne({_id: args._id}, { fields: gVF(context.currentUser, context.Categories) });
|
||||
return context.Categories.findOne({_id: args._id}, { fields: context.getViewableFields(context.currentUser, context.Categories) });
|
||||
},
|
||||
},
|
||||
};
|
||||
|
|
|
@ -1,49 +1,44 @@
|
|||
import Telescope from 'meteor/nova:lib';
|
||||
import Posts from 'meteor/nova:posts';
|
||||
import Users from 'meteor/nova:users';
|
||||
|
||||
// shortcut
|
||||
const gVF = Users.getViewableFields;
|
||||
|
||||
const resolvers = {
|
||||
Post: {
|
||||
commenters(post, args, context) {
|
||||
return post.commenters ? context.Users.find({_id: {$in: post.commenters}}, { fields: gVF(context.currentUser, Users) }).fetch() : [];
|
||||
return post.commenters ? context.Users.find({_id: {$in: post.commenters}}, { fields: context.getViewableFields(context.currentUser, Users) }).fetch() : [];
|
||||
},
|
||||
comments(post, args, context) {
|
||||
return post.commentCount ? context.Comments.find({postId: post._id}, { fields: gVF(context.currentUser, Comments) }).fetch() : [];
|
||||
return post.commentCount ? context.Comments.find({postId: post._id}, { fields: context.getViewableFields(context.currentUser, Comments) }).fetch() : [];
|
||||
},
|
||||
},
|
||||
Comment: {
|
||||
parentComment(comment, args, context) {
|
||||
return comment.parentCommentId ? context.Comments.findOne({_id: comment.parentCommentId}, { fields: gVF(context.currentUser, context.Comments) }) : null;
|
||||
return comment.parentCommentId ? context.Comments.findOne({_id: comment.parentCommentId}, { fields: context.getViewableFields(context.currentUser, context.Comments) }) : null;
|
||||
},
|
||||
topLevelComment(comment, args, context) {
|
||||
return comment.topLevelCommentId ? context.Comments.findOne({_id: comment.topLevelCommentId}, { fields: gVF(context.currentUser, context.Comments) }) : null;
|
||||
return comment.topLevelCommentId ? context.Comments.findOne({_id: comment.topLevelCommentId}, { fields: context.getViewableFields(context.currentUser, context.Comments) }) : null;
|
||||
},
|
||||
post(comment, args, context) {
|
||||
return context.Posts.findOne({_id: comment.postId}, { fields: gVF(context.currentUser, context.Posts) });
|
||||
return context.Posts.findOne({_id: comment.postId}, { fields: context.getViewableFields(context.currentUser, context.Posts) });
|
||||
},
|
||||
user(comment, args, context) {
|
||||
return context.Users.findOne({_id: comment.userId}, { fields: gVF(context.currentUser, context.Posts) });
|
||||
return context.Users.findOne({_id: comment.userId}, { fields: context.getViewableFields(context.currentUser, context.Posts) });
|
||||
},
|
||||
upvoters(comment, args, context) {
|
||||
return comment.upvoters ? context.Users.find({_id: {$in: comment.upvoters}}, { fields: gVF(context.currentUser, context.Users) }).fetch() : [];
|
||||
return comment.upvoters ? context.Users.find({_id: {$in: comment.upvoters}}, { fields: context.getViewableFields(context.currentUser, context.Users) }).fetch() : [];
|
||||
},
|
||||
downvoters(comment, args, context) {
|
||||
return comment.downvoters ? context.Users.find({_id: {$in: comment.downvoters}}, { fields: gVF(context.currentUser, context.Users) }).fetch() : [];
|
||||
return comment.downvoters ? context.Users.find({_id: {$in: comment.downvoters}}, { fields: context.getViewableFields(context.currentUser, context.Users) }).fetch() : [];
|
||||
},
|
||||
},
|
||||
Query: {
|
||||
comments(root, args, context) {
|
||||
const options = {
|
||||
limit: 5,
|
||||
fields: gVF(context.currentUser, Comments)
|
||||
fields: context.getViewableFields(context.currentUser, Comments)
|
||||
}
|
||||
return context.Comments.find({}, options).fetch();
|
||||
},
|
||||
comment(root, args, context) {
|
||||
return context.Comments.findOne({_id: args._id}, { fields: gVF(context.currentUser, Comments) });
|
||||
return context.Comments.findOne({_id: args._id}, { fields: context.getViewableFields(context.currentUser, Comments) });
|
||||
},
|
||||
}
|
||||
};
|
||||
|
|
|
@ -1,31 +1,27 @@
|
|||
import Telescope from 'meteor/nova:lib';
|
||||
import Users from 'meteor/nova:users';
|
||||
|
||||
// shortcut
|
||||
const gVF = Users.getViewableFields;
|
||||
|
||||
const resolvers = {
|
||||
Post: {
|
||||
user(post, args, context) {
|
||||
return context.Users.findOne({ _id: post.userId }, { fields: gVF(context.currentUser, Users) });
|
||||
return context.Users.findOne({ _id: post.userId }, { fields: context.getViewableFields(context.currentUser, Users) });
|
||||
},
|
||||
upvoters(post, args, context) {
|
||||
return post.upvoters ? context.Users.find({_id: {$in: post.upvoters}}, { fields: gVF(context.currentUser, Users) }).fetch() : [];
|
||||
return post.upvoters ? context.Users.find({_id: {$in: post.upvoters}}, { fields: context.getViewableFields(context.currentUser, Users) }).fetch() : [];
|
||||
},
|
||||
downvoters(post, args, context) {
|
||||
return post.downvoters ? context.Users.find({_id: {$in: post.downvoters}}, { fields: gVF(context.currentUser, Users) }).fetch() : [];
|
||||
return post.downvoters ? context.Users.find({_id: {$in: post.downvoters}}, { fields: context.getViewableFields(context.currentUser, Users) }).fetch() : [];
|
||||
},
|
||||
},
|
||||
Query: {
|
||||
posts(root, {terms, offset, limit}, context, info) {
|
||||
console.log("// context")
|
||||
console.log(context)
|
||||
// console.log("// context")
|
||||
// console.log(context)
|
||||
let {selector, options} = context.Posts.parameters.get(terms);
|
||||
const protectedLimit = (limit < 1 || limit > 10) ? 10 : limit;
|
||||
options.limit = protectedLimit;
|
||||
options.skip = offset;
|
||||
// keep only fields that should be viewable by current user
|
||||
options.fields = gVF(context.currentUser, Posts);
|
||||
options.fields = context.getViewableFields(context.currentUser, Posts);
|
||||
return context.Posts.find(selector, options).fetch();
|
||||
},
|
||||
postsViewTotal(root, {terms}, context) {
|
||||
|
@ -34,7 +30,7 @@ const resolvers = {
|
|||
},
|
||||
post(root, args, context) {
|
||||
Meteor._sleepForMs(2000); // wait 2 seconds
|
||||
return context.Posts.findOne({_id: args._id}, { fields: gVF(context.currentUser, Posts) });
|
||||
return context.Posts.findOne({_id: args._id}, { fields: context.getViewableFields(context.currentUser, Posts) });
|
||||
},
|
||||
},
|
||||
Mutation: {
|
||||
|
|
|
@ -1,8 +1,4 @@
|
|||
import Telescope from 'meteor/nova:lib';
|
||||
import Users from './collection.js';
|
||||
|
||||
// shortcut
|
||||
const gVF = Users.getViewableFields;
|
||||
|
||||
const resolvers = {
|
||||
User: {
|
||||
|
@ -28,12 +24,12 @@ const resolvers = {
|
|||
users(root, args, context) {
|
||||
const options = {
|
||||
limit: 5,
|
||||
fields: gVF(context.currentUser, Users)
|
||||
fields: context.getViewableFields(context.currentUser, Users)
|
||||
}
|
||||
return context.Users.find({}, {limit: 5}).fetch();
|
||||
},
|
||||
user(root, args, context) {
|
||||
return context.Users.findOne({$or: [{_id: args._id}, {'telescope.slug': args.slug}]}, { fields: gVF(context.currentUser, context.Users) });
|
||||
return context.Users.findOne({$or: [{_id: args._id}, {'telescope.slug': args.slug}]}, { fields: context.getViewableFields(context.currentUser, context.Users) });
|
||||
},
|
||||
currentUser(root, args, context) {
|
||||
return context && context.userId ? context.Users.findOne(context.userId) : null;
|
||||
|
@ -41,4 +37,4 @@ const resolvers = {
|
|||
}
|
||||
};
|
||||
|
||||
export default resolvers;
|
||||
Telescope.graphQL.addResolvers(resolvers);
|
Loading…
Add table
Reference in a new issue