Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
dataloader-test-1
const users = require("./users");
const posts = require("./posts");
module.exports = {
users,
posts: posts(),
};
const DataLoader = require("dataloader");
const Post = require("../models/Post");
module.exports = function posts() {
return {
getPosts: new DataLoader(
async (ids) => {
const posts = await Post.find({ user: { $in: ids } });
return Promise.all(
ids.map(async (id) =>
posts.filter(({ user }) => user.toString() === id.toString())
)
);
},
{ cacheKeyFn: (id) => id.toString() }
),
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment