How to setup dataloader + meteor + apollo? Getting Fiber/Promise errors


#1

Has anyone set this up? I just need some psuedo code of a resolver and corresponding loader function, maybe for Meteor.users.

Field on type MeteorstagramPost, a instagram clone

postOwner: ( { userId }, args, { loaders, user }) => {

      let owner = loaders.userLoader.load(userId)

      if (!owner) { return null; }
      
      return owner;
    },

loaders.js

loaders.userLoader = new DataLoader(userId => {
    return Meteor.users.findOne({ _id: userId });
});

The above and a few different variations I’ve tried are giving errors about fibers from meteor, or errors from dataloader about returning promises. But I can’t seem to crack the codex here. I see @sacha got it into vulcan.js.

One Solution:

loaders.userLoader = new DataLoader(keys => {
    return new Promise(function(resolve, reject) {
        Meteor.users.rawCollection().find({ _id: { $in: keys } })
        .toArray((err, result) => {
            if (err)
              reject(err);
            else
              resolve(result);
          });
      });
});


#2

You can give Meteor.bindEnvironment a try:

loaders.userLoader = new DataLoader(Meteor.bindEnvironment(userId => {
    return Meteor.users.findOne({ _id: userId });
}));

#3

Relevant code in Vulcan:

And

I suggest using async/await, it makes things a lot easier (you can make your resolver async)