Skip to content

feat(openapi-react-query): Pass abort signal to fetch #1864

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

Merged
merged 1 commit into from
Sep 20, 2024
Merged

feat(openapi-react-query): Pass abort signal to fetch #1864

merged 1 commit into from
Sep 20, 2024

Conversation

zsugabubus
Copy link
Contributor

Changes

See changeset.

How to Review

How can a reviewer review your changes? What should be kept in mind for this review?

Checklist

  • Unit tests updated
  • docs/ updated (if necessary)

@zsugabubus zsugabubus requested a review from a team as a code owner August 19, 2024 22:08
Copy link

changeset-bot bot commented Aug 19, 2024

🦋 Changeset detected

Latest commit: 0778893

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
openapi-react-query Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

@kerwanp kerwanp left a comment

Choose a reason for hiding this comment

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

Clean update and great test! 👌

Might be a stupid question but should it be aswell added to other useSuspenseQuery and useMutation? @zsugabubus

@zsugabubus
Copy link
Contributor Author

@kerwanp As far as I know mutations cannot be cancelled, mutationFn does not have a signal parameter, it just gets the variables and I neither can find an API for it. However, it turns out useSuspenseQuery can be cancelled, I updated the code, thanks for the question, at first I was too lazy because I just copy pasted the same code and it did not work.

@kerwanp kerwanp merged commit 899b157 into openapi-ts:main Sep 20, 2024
8 checks passed
@github-actions github-actions bot mentioned this pull request Sep 20, 2024
@zsugabubus zsugabubus deleted the signal branch September 21, 2024 17:54
@SteinarVrenne
Copy link

This is great to prevent memory leaks, but is there a way to circumvent this behavior in case you want the fetch to continue even if the component has been unmounted?

@zsugabubus
Copy link
Contributor Author

My first thought was that this kind of stuff should be configured via Tanstack Query but I couldn't find such setting. So you have to build your own queryFn. You can either create a wrapper around queryOptions().queryFn or just call client.GET directly. A slightly less correct solution would be to clear request.signal in a middleware.

But are you 100% sure you want to go down this road? Maybe useMutation or queryClient.fetchQuery would be more appropriate for your use-case?

@SteinarVrenne
Copy link

Thanks for the quick reply!

Some background to why I am asking: I am currently migrating to this library, and the old behaviour was to fire and forget a fetch call. This fetch call is very expensive and not cancellable, so it locked down the API until it finished (but of course, no one took the answer). This happened because of a route change so the component unmounted. It was also not cached, so overall not good.

It can definitely be solved using the options you provided, or using the fetchClient, but I wish maybe an override of this feature was available in the options :)

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.

4 participants