Last active
March 29, 2020 11:10
-
-
Save DScheglov/fd17a6de80b527d977633ace0e3b217f to your computer and use it in GitHub Desktop.
dataLoaders
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 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