Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Document non trivial custom mutation #146

@eric-burel

Description

@eric-burel

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');

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions