How can I set http timeout for GraphQLClient rawRequest? #622
Replies: 6 comments
-
I found into source code RequestInit object that contains a timeout attribute. These param can set into GraphQLClient constructor. |
Beta Was this translation helpful? Give feedback.
-
I did install "2.1.0-next.1" version with NPM. In this version RequestInit doesn't have "timeout" attribute. How can I set http timeout with this version? |
Beta Was this translation helpful? Give feedback.
-
Hi! I checked the repository for usages of the So, it seems there is no support of the timeout in neither of the versions. Is it something that is under development? Thanks |
Beta Was this translation helpful? Give feedback.
-
I got client stuck on request today, and thanks @maximvsidorenko your work did save my time to find how to setup timeout. The cross-fetch is a proxy to the node-fetch and the github fetch (issue: lquixada/cross-fetch#62). And both of these libraries can setup timeout in this way: import AbortController from 'abort-controller'
const controller = new AbortController()
const timeoutID = setTimeout(() => {
controller.abort();
}, 150)
try {
const response = await crossFetch('https://example.com', {signal: controller.signal})
const data = await response.json()
} catch (error) {
if (error instanceof crossFetch.AbortError) {
console.log('request was aborted')
}
} finally {
clearTimeout(timeoutID)
} So you can setup timeout in Graph Client: import { GraphQLClient, gql } from 'graphql-request'
import AbortController from 'abort-controller'
const controller = new AbortController();
const timeoutId = setTimeout(() => {
controller.abort()
console.log('timeout')
}, 10)
const graphClient = new GraphQLClient(url, {
signal: controller.signal
})
// request
await graphClient.request(query, variables)
// rawRequest
graphClient.rawRequest(query, variables) Didn't add try catch in last piece of code, sorry for that. |
Beta Was this translation helpful? Give feedback.
-
Note about the above solution; you'll need to clear that timeout or abort would still be called. A try/finally would work. There's also an example of how to do this in |
Beta Was this translation helpful? Give feedback.
-
Hi Please try with this code block.
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi!
I need your help to know How can I set http timeout for GraphQLClient rawRequest? I was read the documentation but I didn't found anything about.
Thanks
Beta Was this translation helpful? Give feedback.
All reactions