-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
if a Query is stopped using AbortController, it cannot be executed again. #4150
Comments
any updates on this? |
I am also facing the same issue.. Does anyone have an update on this?? |
You need to recreate a new AbortController for the second instance of the request. A single AbortController is associated with a single request via its AbortSignal. Hence your AbortController is still associated with the original request instance. See https://developer.mozilla.org/en-US/docs/Web/API/AbortController Edit: Seems like you should be doing that already with your code. There may be something in chrome that is actually recognising and reusing the same req. or AbortController/AbortSignal. You may also consider that the browser could be automatically retrying the request on account of not receiving any status in response from the server. |
@AW-TaylorBett thank you for the info. unfortunately the situation is somewhat different. the problem-scenario is this:
and the request in [3] does not happen. |
Same issue here. The third request never fires, even despite executing with a new, unaborted AbortController. A ugly workaround I use for now is that I pass some additional variable |
@gabor where did you read about the AbortController approach? I think it's the wrong way to do this, because reaching into the link/fetch and cancelling the request breaks all the abstractions that ApolloClient and apollo link set up for you. If you ask ApolloClient to run a query, then you should tell ApolloClient if you no longer wish to run that query, so the stack can be unwound in an orderly fashion. If you called Under the hood If you want to make As a workaround for the time being, here are two other options:
PS: The reason the second request hangs if the first one was aborted is precisely because assumptions were violated by breaking the abstraction. As you can see here, the authors originally assumed that the only way a request would get aborted is via unsubscribing. If you're aborting it directly, that assumption is obviously violated, and the error doesn't propagate through the stack. This leaves other links and subscribers in the stack hanging, because they received neither an error, nor were they unsubscribed from. In your specific case, the dedup link is left hanging, so when a second identical request comes in, it ends up being deduplicated and your second requests waits for the first one to complete, not knowing that it was aborted. The simplest workaround in that case is to just passing PPS: Looking at your code, it seems what you really want is just a query timeout. If so, then the right solution would be to use a timeout link (eg. apollo-link-timeout. Although that particular implementation issues and also breaks assumptions, it should fix your problem for now). |
@helfer thanks for the response. there is a lot of info in your comment, so i will try to react to it's parts:
|
@helfer i tried it out, and the |
@gabor could you show a code example of how to implement this from an existing query please? |
sorry @francoisromain , i do not have access to that code anymore. it went roughly like this:
unfortunately i do not remember the exact api-calls, but there is some way to go from that |
@helfer Do you know of any way to cancel a |
I spent some time this weekend debugging this problem, and I found it was necessary to set both I wound up with something like... Creating the Apollo client: const client = new ApolloClient({
// Regular options here, such as...
cache,
links: ApolloLink.from([links,go,here]),
// This is what enables cancelation
queryDeduplication: false
}) Then to issue/cancel a query... // Issue the query
const query = client.watchQuery<SomeQuery, SomeQueryVariables>({
// Usual stuff, no need for a custom `AbortController.signal`
query: SomeQueryDocument,
fetchPolicy: "network-only",
variables,
});
// Subscribe to it, and do something with the data
const observable = query.subscribe(({ data }) => {
// do something with `data`
// ...
})
// Then somewhere you want to cancel it...
observable.unsubscribe(); // <-- will implicitly cancel the fetch That should successfully cancel a query, and enable re-sending, e.g. in this case where I canceled a query twice, and let it succeed the third time: |
#4150 (comment) should help answer this. Thanks! |
I used the solution proposed by @leebenson in apollo client 2 and it worked fine. But it's not working in apollo client 3. I am using |
#6985 explains why it doesn't work in apollo client 3. |
I have a similar problem. EX)
[process] [reason]
[logic1]
[logic2]
[logic3]
[logic4]
[Method currently being considered] I am thinking of a method of separating components only in the search box and fetching components whenever they are called... |
Anyone come up with a solution in Apollo 3? |
Also in dire need of being able to cancel a long running query based on user input using Apollo 3. |
I believe that part of the issue will be fixed in |
I was able to work around this issue by integrating
|
problematic scenarios: |
createHttpLink has an unhandled TypeError for me when in offline mode and retry-link doesn't get the event. |
@leebenson 's answer still works for me using ApolloClient |
Intended outcome:
i use react with apollo.
i want to cancel a running graphql query using the
AbortController
approach to stop thefetch
request. later i want to run the query again.Actual outcome:
i can cancel the query using an
AbortController
.but, if i execute the query again, no http-request is sent. maybe some cleanup did not happen and apollo still considers the query running? no idea. i can start other queries. so for example, if this graphql query has a parameter, i can start the query with param1, cancel it, then start it with param2 and it works. but if i start it again with param1, no http request is sent.How to reproduce the issue:
withApollo
the
QUERY
should go to something slow (like, response in 20seconds)open the network-tab in the browser (i tried in google-chrome)
click the button, and verify that the query is first
pending
and then latercanceled
click the button again, and there will be no new line in the network-tab
Versions
The text was updated successfully, but these errors were encountered: