1

I am trying to use dataloader's loadMany function. But I am getting error. I already search many where but not getting any solutions. Please help me.

Here is my try-

loader.js (Here I write Loader function)-

//Models
const {Subcategory} = require("../Models/categoryModel");

module.exports.batchSubCategory = async (categoryIds) => {
    const subcategory = await Subcategory.find({_id: {$in: categoryIds}});
    return categoryIds.map(categoryId => subcategory.find(category => category.id === categoryId));
}

And then in Apollo Context-

const apolloServer = new ApolloServer({
    typeDefs,
    resolvers,
    context: async ({ req }) => {
        return {
            loaders: {
                subCategory: new Dataloader(keys => loaders.subCategoryLoaders.batchSubCategory(keys)),
            }
        }
    }
});

The I call this Subcategory loader-

subCategory: async (parent, _, { loaders }) => {
    const subcategory = await loaders.subCategory.loadMany(parent.subCategory.toString())
    return subcategory;
}

Notice: Here I use loadMany because Here subCategory is a Array of [ids]. Here is image-

enter image description here

And When I run the application- I am getting this type of error-

The loader.loadMany() function must be called with Array<key> but got: 62bab157e769a7dda09ec63f,62bab15ce769a7dda09ec64a,62bab164e769a7dda09ec657.

Now I can't understand How can I solve this error. Please help me. Please give some of your valuable times to solve my problem.

Samia Sammi
  • 95
  • 2
  • 10

0 Answers0