New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BREAKING] loadMany() returns individual Error instead of rejecting promise #216
Conversation
currently, if they don't want it to fail in Promise.all, they should have handle it in their load function? we also have something new called Promise.allSettled that should work in this case? |
b5cf718
to
5bb1605
Compare
Batch loading functions should definitely return an Error at indices that failed when a batched result has a mix of success and failures. You're totally right that this is essentially |
I appreciate your hard work on this valuable library, so thank you. However, I think this change isn't for the better, and I'm compelled to express this even though it's been months since this was merged. Our current loaders, used with TypeScript, GraphQL, and GraphQL Code Generator, are great. We can write resolvers like:
This is all fully typed, and TypeScript knows that the In short, I wish there had been a different method introduced instead of breaking the types of the current API in a way that doesn't seem to have a clear fix. |
The release notes mention a clear fix, no?
|
Yes, the fix appears to be creating a subclass which implements the older behavior, but I wouldn't call that "clear."
|
Instead sticking to the old API, I would port the call sites, e.g. in you example: products: async (parent, { filters }, { loaders }) => {
const query = db().pluck('id').from('products')
applyProductFilters(query, filters)
const ids = await query
- return loaders.products.loadMany(ids)
+ return Promise.all(ids.map(id => loader.load(id)))
}, |
Thank you. I appreciate the help and suggestions. |
I understand that this has landed a long time ago now, but we just recently updated dataloaders on our project and stumbled on this breaking change. export class OurDataLoader<Key, Value, CacheKey> extends DataLoader<Key, Value, CacheKey> {
/**
* Loads multiple keys, promising an array of values:
* Will error if any key rejects.
* Reproduce behavior of dataloader@v1 loadMany
*/
public async loadAll(keys: Key[]): Promise<Value[]> {
return await Promise.all(keys.map((key) => this.load(key)));
}
} I chose to chime here mainly because I am curious if we know of real use cases for this new On the other hand, that new behavior is pretty much "just" a wrapper for |
This changes the behavior of loadMore() to return
Promise<Array<V | Error>>
which always resolves, instead of the previousPromise<Array<V>>
which rejects if any one of the requested values fails.This makes this method meaningfully useful beyond a simple
Promise.all()