Created
April 9, 2020 11:57
-
-
Save furkancelik/87e805ee2dc2515c3eaaddf0f30d9453 to your computer and use it in GitHub Desktop.
dataloader-test-1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const users = require("./users"); | |
const posts = require("./posts"); | |
module.exports = { | |
users, | |
posts: posts(), | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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