Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document non trivial custom mutation #146

Open
eric-burel opened this issue Feb 14, 2020 · 0 comments
Open

Document non trivial custom mutation #146

eric-burel opened this issue Feb 14, 2020 · 0 comments

Comments

@eric-burel
Copy link
Contributor

Example of a non trivial call gracefully provided by @yairtal

Creating a custom server mutation for any use case that is not provided by Vulcan's default mutation is a perfectly normal pattern, yet it's not a reflex because the syntax is not immediate.
Doc could be improved with a non trivial example + a more visible/higher up section about custom mutations and resolvers.

import { addGraphQLMutation, addGraphQLResolvers, Utils } from 'meteor/vulcan:core';
import Users from 'meteor/vulcan:users';
const check = (user, document) => {
  if (!user || !document) return false;
  return Users.owns(user, document)
    ? Users.canDo(user, 'post.update.own')
    : Users.canDo(user, `post.update.all`);
};
const specificResolvers = {
  Mutation: {
    addToPostsViewedByIds(root, {postId, userId}, context) {
      const document = context.Posts.findOne({_id: postId});
      Utils.performCheck(check, context.currentUser, document);
      context.Posts.update({_id: postId}, {$addToSet: {viewedByIds: userId}});
      return document;
    },
  },
};
addGraphQLResolvers(specificResolvers);
addGraphQLMutation('addToPostsViewedByIds(postId: String, userId: String) : Post');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant