Apollo-link: Add timeout option for apollo-link-http

Created on 20 Feb 2019  ·  7Comments  ·  Source: apollographql/apollo-link

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

feature

Most helpful comment

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

All 7 comments

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

@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.

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.

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>

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.

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

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

Was this page helpful?
0 / 5 - 0 ratings