-
Notifications
You must be signed in to change notification settings - Fork 310
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
Timeout configuration #103
Comments
Any updates on this? It would be great to have. |
I found after a bit of digging that the
Delegation: https://github.com/prisma/graphql-request/blob/master/src/index.ts#L29 It would be fantastic to get this added to the documentation. |
My IDE complains about using timeout being not defined - How to make it work? |
@prakhar241 TS won't let you add excess properties when you pass an object literal. The solution is to construct the options as a separate const options = { timeout: 30000, mode: 'cors' };
const graphQLClient = new GraphQLClient(requestURL, options); |
Unfortunately, this means we need to find another way to add a timeout option. I'd be happy to put up a PR to add it to this library if that's cool with the maintainers! |
@jescalan I'm not familiar with |
For people who found this issue to look for how to implement timeout for Since const createFetchWithTimeout = (timeout: number) => async (
input: RequestInfo,
init?: RequestInit
) => {
if (init.signal) {
throw new Error(
"it looks like graphql-request started using AbortSignal on its own. Please check graphql-request's recent updates"
);
}
const controller = new AbortController();
const timerId = setTimeout(() => {
controller.abort();
}, timeout);
try {
return await fetch(input, { ...init, signal: controller.signal });
} finally {
clearTimeout(timerId);
}
}; Then we can use it like the following: const client = new GraphQLClient(YOUR_API_ENDPOINT, {
// 2000ms timeout
fetch: createFetchWithTimeout(2000),
}); |
If your project is using TypeScript, the solution above will need to add const createFetchWithTimeout =
(timeout?: number) =>
async (input: RequestInfo | URL, init?: RequestInit) Otherwise TS compiler would complain that |
Shorter version:
|
If you are running in nodejs this does the trick: return new GraphQLClient(someUrl, {
signal: AbortSignal.timeout(API_CALL_TIMEOUT_MS),
}); See AbortSignal-Docs |
Hei, would it be possible to support configuring the timeout ?
The text was updated successfully, but these errors were encountered: