v1.2.0
New:
Prime values in the cache - #18. — If you have values cached locally, this provides an API for adding those already known values to a DataLoader cache:
let loader = new DataLoader(keys => promiseToGetKeys(keys));
loader.prime('abc', 'My Value');
loader.load('abc').then(val => console.log(val)); // Logs "My Value"
This also works for priming errors:
let loader = new DataLoader(keys => promiseToGetKeys(keys));
loader.prime('abc', new Error('My Error'));
loader.load('abc').catch(err => console.log(err)); // Logs "My Error"
This is particularly useful for allowing two loaders to interact based on two fetchable keys for one type:
let userByIDLoader = new DataLoader(ids => promiseToGetByID(ids).then(users => {
for (let user of users) {
userByUsernameLoader.prime(user.username, user);
}
return users;
}));
let userByUsernameLoader = new DataLoader(names => promiseToGetByUsername(names).then(users => {
for (let user of users) {
userByIDLoader.prime(user.id, user);
}
return users;
}));