apollo-link icon indicating copy to clipboard operation
apollo-link copied to clipboard

Add timeout option for apollo-link-http

Open dshook opened this issue 6 years ago • 7 comments

It would be very nice to be able to include a millisecond timeout value in the apollo-link-http options.

Even if this can't abort the in progress fetch at the moment, it would be very useful so everyone doesn't have to roll their own solution:

https://stackoverflow.com/questions/40837676/apolloclient-timeout-best-option https://stackoverflow.com/questions/47198402/how-to-set-a-timeout-on-a-request-with-apollo-client

dshook avatar Feb 20 '19 20:02 dshook

Yes it should be possible to wrap the fetch and time the request out if needed.

If people depend on the non-timeout version we would need to provide an opt-out doe this behavior.

We can use the abortConttoller for the cancel itself.

Thoughts? @hwillson

JoviDeCroock avatar Feb 22 '19 17:02 JoviDeCroock

@dshook @JoviDeCroock I think what you really want is a timeout link. Presumably you want to bound the amount of time that the caller might have to wait for a response, and just return an error if no response comes back in that time, correct? If so, putting it in the http link itself will not make it composable with other links. Your query could be waiting or stuck in any other link as well, it doesn't really have to be the retry link. It could be in apollo-link-retry, apollo-link-serialize, apollo-link-queue, etc. To guarantee a specific timeout, what you really need is a timeout link that sits at the very top of the stack, and times out requests that have taken too long (canceling them and returning an error), regardless of where in the stack the request is pending.

As you pointed out, canceling the request after timing it out doesn't guarantee that the server didn't see it, so be careful with mutations that are not idempotent.

PS: It looks like someone already made a timeout link (apollo-link-timeout). Unfortunately it isn't very flexible and it breaks the link abstraction (eg. reaching into the fetch link to abort the request and returning a timeoutRef which can be called from anywhere), it doesn't appear to be well tested, has some questionable defaults (15s timeout instead of no timeout) and isn't very flexible (eg timeout can't depend on query variables), but it will probably do the trick for you.

helfer avatar Apr 28 '19 18:04 helfer

I agree this would be awesome but I understand Fetch API has no timeout mechanism so I'm not sure if Fetch is used under the hood or if just regular promises are used.

JSONRice avatar May 07 '19 20:05 JSONRice

apollo-link-timeout is not flexible as @helfer mentioned. I think timeout value should be dynamic. How can we handle timeout as best way? 🤣

example query

<Query
  query={FETCH_QUERY}
  context={{ timeout: 5000 }}
>...</Query>

vreality64 avatar May 22 '19 09:05 vreality64

Just want to follow up here, what is the acceptable current solution ? And it represents a real world scenario and seems like a fair request for a timeout.

iroy2000 avatar Nov 14 '19 17:11 iroy2000

Hello Apollo Team , It is very useful to have a timeout option supported by default in apollo graphql client implementation. As this is tightly coupled with client requests, it is fair to request for the timeout. Are you already planning this feature? What is your current stand on this request?

Looking forward to hearing from you.

Thanks Aravind

ghost avatar Nov 18 '19 20:11 ghost

Is this feature still on track? We could really benefit from timeout option if it's available.

kaungmyatlwin avatar Apr 09 '20 09:04 kaungmyatlwin