Skip to content

Conversation

@csorfab
Copy link
Contributor

@csorfab csorfab commented Jan 20, 2022

The fetchData function of useClientRequest returns an empty Promise if the hook is unmounted. Typing has been updated to reflect this behavior

What does this PR do?

It fixes a typing error that affects useClientRequest's fetchData function, and as a result, useManualQuery and useMutation too. useClientRequest's fetchData function returns an empty (undefined) Promise if called after the component has unmounted, but this is not currently reflected in its typing. Admittedly, this is an edge case behavior, but it's caused my team hard to reproduce bugs, and since the behavior is codified in useClientRequest.test.js:476 ("returns undefined instantly if not mounted"), it seems appropriate that the typing reflect it.

This would be a breaking change for many projects, but I believe that Typescript's usefulness lies exactly in preventing hard-to-reproduce bugs like those caused by this behavior, so I think it's a small price to pay for correctness.

Related issues

Closes #772

Checklist

  • I have checked the contributing document
  • I have added or updated any relevant documentation
  • I have added or updated any relevant tests

The fetchData function of useClient request return an empty Promise if the hook is unmounted. Typing has been updated to reflect this behavior
@netlify
Copy link

netlify bot commented Jan 20, 2022

👷 Deploy request for graphql-hooks-cra pending review.
Visit the deploys page to approve it

🔨 Explore the source changes: c9f9647

@simoneb
Copy link
Member

simoneb commented Jan 20, 2022

can you please create an issue and use this PR to propose a solution for it?

@csorfab
Copy link
Contributor Author

csorfab commented Jan 20, 2022

Hi @simoneb, I've created issue #772 and linked to this PR in it.

@csorfab
Copy link
Contributor Author

csorfab commented Jan 20, 2022

Another solution would be to return a { loading: false, cacheHit: false } object instead of the empty Promise.resolve() in useClientRequest - this would not be a breaking change for Typescript users, and I don't imagine many depends on undefined being returned in this edge case. I got discouraged from this route since the tests explicitly test for behavior, but I think it might be a better solution than what the current PR achieves? @simoneb what do you think about this? I'd gladly make a PR for that too

@simoneb
Copy link
Member

simoneb commented Jan 20, 2022

Can you please cover this change with a test by changing or adding to the code in https://github.com/nearform/graphql-hooks/tree/master/packages/graphql-hooks/test-d?

@simoneb
Copy link
Member

simoneb commented Jan 24, 2022

@csorfab ping :)

@csorfab
Copy link
Contributor Author

csorfab commented Jan 24, 2022

@simoneb sorry for the delay! I checked the typing tests with npm run tsd and it ran fine, so I added two assertions that cover the change in the PR.

Have you considered the other option, which would be returning a "dummy" object ({ loading: false, cacheHit: false }) instead of undefined? I'm beginning to see it as a better option, because it feels like more consistent behavior - maybe an Error could be included, describing that the request was aborted because the hook unmounted? Certainly feels better than just returning undefined without any explanation

@tiagoheliob
Copy link

@csorfab Could you please draft an implementation using your suggestion of returning a dummy object? it's certainly more suitable than an undefined return for sure as you mentioned

@csorfab
Copy link
Contributor Author

csorfab commented Jan 27, 2022

@tiagoheliob Hi! I've created a new PR with my implementation of the suggestion: #777

@tiagoheliob
Copy link

@csorfab Thanks a lot, I'll take a look

@csorfab
Copy link
Contributor Author

csorfab commented Feb 3, 2022

I'm closing this as #777 has been merged instead

@csorfab csorfab closed this Feb 3, 2022
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.

The typing of FetchData is incorrect with regards to its return type

3 participants