Last active
April 4, 2021 16:04
-
-
Save AoiYamada/659431cfb0c574efb9e2dff37beeea6a to your computer and use it in GitHub Desktop.
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 users = new Map([ | |
[ | |
1, | |
{ | |
id: 1, | |
name: "Tom", | |
}, | |
], | |
[ | |
2, | |
{ | |
id: 2, | |
name: "Peter", | |
}, | |
], | |
[ | |
3, | |
{ | |
id: 3, | |
name: "Mary", | |
}, | |
], | |
[ | |
4, | |
{ | |
id: 4, | |
name: "John", | |
}, | |
], | |
[ | |
5, | |
{ | |
id: 5, | |
name: "Tommy", | |
}, | |
], | |
[ | |
6, | |
{ | |
id: 6, | |
name: "Johnny", | |
}, | |
], | |
[ | |
7, | |
{ | |
id: 7, | |
name: "Betty", | |
}, | |
], | |
[ | |
8, | |
{ | |
id: 8, | |
name: "Alex", | |
}, | |
], | |
[ | |
9, | |
{ | |
id: 9, | |
name: "Gary", | |
}, | |
], | |
]); | |
const replies = [ | |
{ | |
user_id: 1, | |
content: "dummy 1", | |
replies: [ | |
{ | |
user_id: 2, | |
content: "dummy 2", | |
replies: [], | |
}, | |
{ | |
user_id: 1, | |
content: "dummy 3", | |
replies: [ | |
{ | |
user_id: 5, | |
content: "dummy 14", | |
replies: [ | |
{ | |
user_id: 8, | |
content: "dummy 15", | |
replies: [], | |
}, | |
{ | |
user_id: 9, | |
content: "dummy 16", | |
replies: [], | |
}, | |
], | |
}, | |
], | |
}, | |
{ | |
user_id: 3, | |
content: "dummy 4", | |
replies: [ | |
{ | |
user_id: 2, | |
content: "dummy 5", | |
replies: [], | |
}, | |
], | |
}, | |
], | |
}, | |
{ | |
user_id: 2, | |
content: "dummy 6", | |
replies: [ | |
{ | |
user_id: 1, | |
content: "dummy 7", | |
replies: [ | |
{ | |
user_id: 6, | |
content: "dummy 13", | |
replies: [], | |
}, | |
], | |
}, | |
{ | |
user_id: 3, | |
content: "dummy 8", | |
replies: [], | |
}, | |
{ | |
user_id: 1, | |
content: "dummy 9", | |
replies: [ | |
{ | |
user_id: 2, | |
content: "dummy 10", | |
replies: [], | |
}, | |
{ | |
user_id: 7, | |
content: "dummy 11", | |
replies: [], | |
}, | |
], | |
}, | |
{ | |
user_id: 4, | |
content: "dummy 12", | |
replies: [], | |
}, | |
], | |
}, | |
]; | |
const userLoader = new DataLoader(async (ids) => { | |
console.log("user loader has been called with", ids); | |
return ids.map((id) => users.get(id)); | |
}); | |
// batch all nested | |
const repliesAggregatorNoAwait = async (replies) => | |
Promise.all( | |
replies.map(async ({ user_id, replies: nestedReplies, content }) => ({ | |
user: userLoader.load(user_id), | |
content, | |
replies: repliesAggregatorNoAwait(nestedReplies), | |
})) | |
); | |
const repliesResolver = async (repliesPromises) => | |
Promise.all( | |
(await repliesPromises).map( | |
async ({ user, replies: nestedRepliesPromises, content }) => ({ | |
user: await user, | |
content, | |
replies: await repliesResolver(nestedRepliesPromises), | |
}) | |
) | |
); | |
(async () => { | |
/** | |
* Await all .load outside the recursive aggregator can perform optimized batching | |
* | |
*/ | |
const repliesPromises = repliesAggregatorNoAwait(replies); | |
// user loader has been called with [ 1, 2, 5, 8, 9, 3, 6, 7, 4 ] | |
const values = await repliesResolver(repliesPromises); | |
console.log("values:", JSON.stringify(values, null, 2)); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment