Skip to content

Instantly share code, notes, and snippets.

@abdoulmouctard
Created November 9, 2019 19:00
Show Gist options
  • Save abdoulmouctard/6e6ec84d818ff165a086ca5a02c1f545 to your computer and use it in GitHub Desktop.
Save abdoulmouctard/6e6ec84d818ff165a086ca5a02c1f545 to your computer and use it in GitHub Desktop.
import { combineResolvers } from 'graphql-resolvers';
import { omitBy, isNil } from 'lodash';
import { authorize } from './auth.resolver';
import { USER, ADMIN } from '../../models/user.model';
import { Paginate } from './pagination.resolver';
export default {
Query: {
Comment: combineResolvers(authorize(USER), async (parent, { id }, { models }) => models.Comment.findById(id)),
Comments: combineResolvers(authorize(USER), Paginate('Comment')),
User: combineResolvers(authorize(USER), async (parent, { id }, { models }) => {
const comment = models.Comment.findById(id);
return models.User.findById(comment.userId);
}),
Issue: combineResolvers(authorize(USER), async (parent, { id }, { models }) => {
const comment = models.Comment.findById(id);
return models.User.findById(comment.userId);
}),
},
Mutation: {
createComment: combineResolvers(
authorize(USER),
async (parent, { user, issue, content }, { models }) =>
models.Comment.create({ userId: user.id, issueId: issue.id, content })
),
updateComment: combineResolvers(
authorize(ADMIN),
async (parent, { id, content, issue }, { models }) => {
const options = omitBy({ content, issueId: issue.id }, isNil);
return models.Comment.findByIdAndUpdate(id, options, { new: true });
}
),
deleteComment: combineResolvers(
authorize(ADMIN),
async (parent, { id }, { models }) => {
const Comment = await models.Comment.findById(id);
if (Comment) {
await Comment.remove();
return true;
}
return false;
}
)
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment