0

I have a project where I use Nodejs for the backend, the backend I use Mongoose and Graphql, Accordingly, I have schema and resolver folders, and also a population file where I get a function that helps me to drill into data

this is the population file:

as you see I use DataLoader to decrease calls and increase server performance, but I have problems with DataLoader, when I try to use it I get many obstacles, I try to use taskLoader there where I need it, but I have such error: "DataLoader must be constructed with a function which accepts Array and returns Promise<Array>, but the function errored synchronously: BSONError: Argument passed in does not match the accepted types."

const taskLoader = new DataLoader((taskId) => {
  return tasks(Ids);
});

const tasks = async (taskId) => {
  try {
    const tasks = await taskModel.find({ _id: taskId });

    return tasks.map((el) => {
      return {
        ...el._doc,
        taskItems: taskItem.bind(this, el.taskItems),
        author: user.bind(this, el.author),
      };
    });
  } catch (error) {
    throw error;
  }
};

const taskItem = async (taskItemId) => {
  try {
    const taskItem = await taskItemModel.find({ _id: taskItemId });

    return taskItem.map((el) => {
      return {
        ...el._doc,
        itemFrom: tasks.bind(this, el.itemFrom),
        author: user.bind(this, el.author),
      };
    });
  } catch (error) {
    throw error;
  }
};

const user = async (userId) => {
  try {
    const user = await userModel.findById(userId);

    return {
      ...user._doc,
      tasks: taskLoader.load(user.tasks),
    };
  } catch (error) {
    throw error;
  }
};

exports.taskLoader = taskLoader;
exports.tasks = tasks;
exports.user = user;
exports.taskItem = taskItem;

also, this is the Graphql resolver folder where I use taskLoader to get Tasks:

// ========== Middleware ========== \\
const {
  tasks,
  taskItem,
  taskLoader,
  user,
} = require("../middleware/population.middleware");

const { removeTask, removeTaskItem } = require("../middleware/crud.middleware");

module.exports = {
  getTasks: async (args) => {
    const { accountId } = args;

    try {
      const user = await userModel.findById(accountId);

      // ====== Filter Statement
      if (!user) return new Error("such user not exist");

      return taskLoader.load(user._doc.tasks);
    } catch (error) {
      throw error;
    }
  },
}

I don't have any idea why it doesn't works, I tried many ways to solve it, but vain

david
  • 47
  • 1
  • 5

0 Answers0