graphql-dataloader-boilerplate
graphql-dataloader-boilerplate copied to clipboard
Improve loaders return
https://github.com/facebook/dataloader/blob/master/src/index.js#L13
DataLoader originally returns a Promise of an Array of values or Errors, while we return the value or null.
Code that can solve this:
const load = async (context: GraphQLContext, id: string): Promise<?User> => {
if (!id) {
return null;
}
let data;
try {
data = await context.dataloaders.UserLoader.load(id);
} catch (err) {
return null;
}
return viewerCanSee(context, data) ? new User(data, context) : null;
}