danyhiol
danyhiol

Reputation: 669

Call Meteor method inside Apollo Mutation

I've defined some Meteor methods on the server that I need to be executed only on the server (Meteor.isServer). Now I want to call these methods on Apollo resolvers using Meteor.call, but this does not seem to work.

Meteor.methods({ 
  'post.add': function addpost(data) {
    new SimpleSchema({
      data: { 
        type: Object
      },
      'data.title': { type: String },
      'data.body': { type: String },
      'data.tag': { type: String },
    }).validate(data);
    return Post.insert(data);
  }
});

const resolvers = {
  Mutation: {
    doSomething(){ 
      return Meteor.call('post.add', data);
    }
  }
}

But calling the method on the client (after removing Meteor.isServer) work fine. Another issue is using resolvers Mutation function inside another Mutation function:

const resolvers = {
  Mutation: {
    addData(){ ... }

    doSomething(){ return this.addData(); }
  }
}

Upvotes: 1

Views: 118

Answers (1)

danyhiol
danyhiol

Reputation: 669

I ended up writing a JS class where I define some static functions/methods (to be able to call the methods without instantiating it's class) and used them in my resolvers.

// some file .../Utils
class Utils{
  static someMethod({arg1, arg2, ...}){
    ...do something
  }
}
export default Utils;

// In my resolvers
import Utils from '.../Utils'
const resolvers = {
  Mutation: {
    const a = ...;
    const b = ...;
    const c = Utils.someMethod({a, b, ...}); 
  }
}

Upvotes: 0

Related Questions