Skip to content

Conversation

@ryanruanwork
Copy link
Contributor

In some case, item in resultGroups is an error rather than an array of object, see https://github.com/Yelp/dataloader-codegen/blob/v0.1.4/src/implementation.ts#L246-L251

We need to turn that Error object into an array of Error in order to match the number of item in requestGroups

@ryanruanwork ryanruanwork requested a review from magicmark March 18, 2020 22:54
Copy link
Collaborator

@magicmark magicmark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, does this case ever actually happen tho?

https://github.com/Yelp/dataloader-codegen/blob/master/src/implementation.ts#L287
https://github.com/Yelp/dataloader-codegen/blob/master/src/implementation.ts#L310

We check if a resource return value is an error or not, and replace it with a list of CaughtResourceErrors if so - which matches the current function type shape.

Have you observed this behaviour actually happening? If so I think that's a bug...but if typescript is complaining about it, maybe we're missing something. Are you able to trace how this could happen?

tl;dr I think the current shape that this function accepts is correct and should stay the same?

(worst case scenario we can write a guard at the top of the function and throw an error)

@ryanruanwork
Copy link
Contributor Author

Hmm, does this case ever actually happen tho?

https://github.com/Yelp/dataloader-codegen/blob/master/src/implementation.ts#L287
https://github.com/Yelp/dataloader-codegen/blob/master/src/implementation.ts#L310

We check if a resource return value is an error or not, and replace it with a list of CaughtResourceErrors if so - which matches the current function type shape.

Have you observed this behaviour actually happening? If so I think that's a bug...but if typescript is complaining about it, maybe we're missing something. Are you able to trace how this could happen?

tl;dr I think the current shape that this function accepts is correct and should stay the same?

(worst case scenario we can write a guard at the top of the function and throw an error)

Well, you are right. I miss that part of code, I add an invariant check to make Flow understand that BatchItemNotFoundError is an Error

Comment on lines 252 to 255
// Tell flow that "response" is actually an error object.
// (This is so we can pass it as 'cause' to CaughtResourceError)
invariant(response instanceof Error, 'expected BatchItemNotFoundError to be an Error');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm why is this needed? We made response an error in the line just above

Here's a similar repro:

https://flow.org/try/#0PTAEDMBsHsHcCh4GNIEMDO7QDFrQKIBOh0hoApgB4Au5AdgCZZElkDeAvgNzLR3rUIeUAF5QdcrBx4WpABQByAJ4BLANbl0CgJQ94ICDCnpy5ALZYAFqgAONpaFgrql0C5XoAXPDng8n0FlCbURwAFc6JGoVPlBrRkhyILlyYgCg7VA2eABIAwZoUFQ3SxU6AHMAOmrQeA5EA1RGEo9veIZE5L9oXSA

Is just this a flow version thing? Or can you show a similar minimal repro that errors with our current logic?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Root cause comes from https://unpkg.com/dataloader-codegen@0.1.4/lib/runtimeHelpers.js.flow and search BatchItemNotFoundError
you can see declare export class BatchItemNotFoundError mixins Error, this is not correct, we should use extends rather than mixins

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah I see. Ok makes sense, thanks for doing the digging here!

Can we add a comment about why we're adding the invariant, linking to this conversation or something? Otherwise it looks redundant when reading the code.

(Bonus points if we can make a minimal repro and make a ticket on the flowgen project - not necessary tho)

After the comment I think we're good to ship!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minimal repro is https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVATApgYxgQwCcsw98BncsAMTjgFFDC5CwBbASwA8OA7Kxs1YBvAL6oAFLQZMWALjCCWASiA, I will create an issue on the flowgen project.

yea, I will add some comments about the reason why I add redundant code here.

@ryanruanwork ryanruanwork changed the title Handle case when resultGroups return single error object Add invariant to let Flow understand BatchItemNotFound is a subclass of Error Mar 19, 2020
@ryanruanwork ryanruanwork requested a review from magicmark March 19, 2020 17:37
@ryanruanwork ryanruanwork force-pushed the fix_flow_type_error_in_unPartitionResults branch from 8baf635 to b26f633 Compare March 19, 2020 17:39
Copy link
Collaborator

@magicmark magicmark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks!

@ryanruanwork ryanruanwork merged commit e05ac94 into master Mar 19, 2020
@ryanruanwork ryanruanwork deleted the fix_flow_type_error_in_unPartitionResults branch March 19, 2020 17:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants