[REQUEST] Unify the way `load` and `loadMany` handle errors
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 25/100
- Loại issue
- Tính năng
- Độ rõ ràng
- Cần làm rõ
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- javascript, typescript
Hướng nghiên cứu
Bắt đầu bằng cách đọc các entry point load và loadMany cùng các khai báo TypeScript của chúng, sau đó xem lại hành vi promise và lỗi đã được tài liệu hóa trong issue này. So sánh cách tiếp cận theo kiểu Result được đề xuất với các ví dụ xử lý hiện có trong V1 và V2; việc này chỉ được thực hiện sau khi dự án đã xác định một hợp đồng lỗi nhất quán và tài liệu tương ứng.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
What problem are you trying to solve?
At the moment, the way that load and loadMany are handled is very different.
load is a regular promise that rejects, so awaits need to be try-catched.
loadMany always resolves, so we need to iterate over the results in order to determine if we couldn't get anything.
Since there's no indication in the Typescript declaration that one always resolves and the other doesn't. Not even a JSDoc to mention that if the batch function returns an error, the promise will reject.
Describe the solution you'd like
Unification in how we need to handle errors. Maybe something like Rust's Result type could be useful. Neverthrow looks like a robust implementation of Result
Describe alternatives you've considered
-
Sticking with V1. Not as ideal, since the errors are useful for providing additional context to errors in a centralised location. Our custom error messages no longer need to include the entity ID.
-
Tried several different ways of handling the errors, as detailed in the below additional context. We're struggling to come to a consensus on it, due to the fact that we need to be cognisant of the fact that
loadmay throw if an ID isn't found in our DB.
Additional context
I'm having some trouble on my team trying to migrate to Dataloader v2. The improved error information is great, but migrating from V1 just feels unergonomic.
Places where we would check for null are now utter chaos.
We've tried try-catch blocks but the sheer amount of boilerplate that it introduces (can't use const x = loader.load(...) anymore) is unergonomic and verbose.
We've tried promises, which feels ok at the loader callsite, since Prettier wraps the line nicely. But the wrappers are a bit unsightly with wrapWithContextError being a curryied function.
await loader
.loadMany(idArray)
.then(throwIfError)
.catch(wrapWithContextError(wrapperError))
The fact that loader.load needs to be handled significantly differently to loader.loadMany has brought with it some confusion.
Here's the load variants
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// =-=-=- Allow the error to propagate =-=-=-=
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
const entity = await ctx.loaders.entityLoader.load(id);
// Do something with the entity
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// =-=-=- Allow the error to propagate, but wrap in a custom error =-=-=-=
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
let entity;
try {
entity = await ctx.loaders.entityLoader.load(id);
} catch (e) {
throw new UserInputError(`Could not find <entity name>`, {
cause: e
});
}
// Do something with the entity
// Or
const entity = await ctx.loaders.entityLoader.load(id).catch(e => {
throw new UserInputError(`Could not find <entity name>`, {
cause: e
});
});
// Do something with the entity
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// =-=-=- Promises, ignoring the error =-=-=-=
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
const entity = await ctx.loaders.entityLoader.load(id).catch(() => null);
// Do something with the entity
Here's the loadMany variants
// =-=-=-=-=-=-=-=-=-=-=-=
// =-=-=- Promises =-=-=-=
// =-=-=-=-=-=-=-=-=-=-=-=
const entities = await ctx.loaders.entityLoader
.loadMany(ids)
.then(throwIfError());
// Do something with the collection
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// =-=-=- Promises with a custom error =-=-=-=
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
const entities = await ctx.loaders.entityLoader
.loadMany(ids)
.then(throwIfError())
.catch(wrapWithContextError(
new UserInputError("One or more <entity name>s could not be found")
));
// Do something with the collection
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// =-=-=- Promises, ignoring the error =-=-=-=
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
const entities = await ctx.loaders.entityLoader
.loadMany(ids)
.then(entities => entities.map(
entity => isError(entity) ? null : entity)
);
// Do something with the collection
// =-=-=-=-=-=-=-=-=-=-=-=-=
// =-=-=- Type guard =-=-=-=
// =-=-=-=-=-=-=-=-=-=-=-=-=
const entities = await ctx.loaders.entityLoader.loadMany(ids);
if (!loadSuccessful(entities)) {
throw entities.find(isError);
}
// Do something with the collection
- Ngôn ngữ chính
- JavaScript
- Star
- 13.4k
- Fork
- 522
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của graphql/dataloader
-
[BUG] `maxBatchSize: NaN` is accepted and silently disables batchingCó thể đã có người làm @RaphaelFakhri đã nhận 4 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
graphql/dataloader#405 ·
-
Failed batch evicts a replacement cache entry after clear/primeCó thể đã có người làm @Claire-ovo đã nhận 13 ngày trước. Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 57/100
graphql/dataloader#403 ·
-
enhancement
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 38/100
graphql/dataloader#393 ·
-
enhancement
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 25/100
graphql/dataloader#382 ·
-
enhancement
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
graphql/dataloader#379 · 2 bình luận · 5 reaction ·
Tất cả issue của graphql/dataloader
Issue tương tự
-
area:models enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
status: ready
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
melodic-software/claude-code-plugins#14389 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[推荐] oai123 - ChatGPT代充平台与AI服务商城Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 72/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100