apollo-client
apollo-client copied to clipboard
Increase the timeout for a request.
The Apollo Client SDK Kotlin has a field timeoutMills
to add a custom timeout to a request.
I didn't find any way to do so for the React API neither in the docs or discussion. Some of the issues were closed without add an explanation .
Apollo Client with the HttpLink just calls fetch
, which does not have a timeout option.
It will try to wait until the server closes the connection, so it's literally impossible to increase the timeout on client side any further than that.
You could shorten the waiting time by introducing your own link to the link chain that would time out requests after a while, but making requests last longer than they currently do is just not possible as they already wait the maximum possible amount of time.
@phryneas fetch
allows to cancel an inflight requests through the AbortController
signal.
Current browsers support a shortcut through AbortSignal.timeout
, like:
async function loadPosts() {
try {
const response = await fetch('/posts', {
signal: AbortSignal.timeout(5000)
});
const posts = await response.json();
return posts;
} catch (error) {
// Timeouts if the request takes longer than 5 seconds
console.log(error.name === 'AbortError');
// handle error
}
}
This use case is quite common to ensure the server, or the overall network latency is holding the response for a long period of time.
Considering Apollo Client uses a fetch interface, it would be appropriate to have this option available from Apollo Client directly. Right now the only way possible is to create a custom HttpLink
with the custom logic. Not a hard task, but is a bit tedious for such a basic functionality.
@lughino I'm not sure if this is the right issue to ask for this.
This issue was not asking for a timeout
option, but it was asking for a way to increase an assumed existing timeout (which doesn't exist).
Generally, I agree, that we could introduce timeout
option, but that should probably be tracked in a separate issue over in https://github.com/apollographql/apollo-feature-requests.
Could you please open an issue over there?
Sure thing @phryneas I'll open a issue there
Thank you!
I believe I can close this issue here then, since @jeebeez never answered - I assume the explanation I gave in my first answer was probably enough.
Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. For general questions, we recommend using StackOverflow or our discord server.