Skip to content

Instantly share code, notes, and snippets.

@DScheglov
Last active March 29, 2020 11:10
Show Gist options
  • Save DScheglov/fd17a6de80b527d977633ace0e3b217f to your computer and use it in GitHub Desktop.
Save DScheglov/fd17a6de80b527d977633ace0e3b217f to your computer and use it in GitHub Desktop.
dataLoaders
const authorsByIds = () => new DataLoader(async ids => {
const authors = await authorModel.findByIds(ids);
const authorById = new Map();
authors.forEach(author => authorById.set(author.id, author));
return ids.map(id => authorById.get(id));
});
const authorResolver = (source, { id }, context) => {
let dl = context.dataLoaders.get(authorsById);
if (!dl) {
dl = authorsById();
context.dataLoaders.set(authorsById, dl);
}
return dl.load(id)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment