Comments (8)
I found after a bit of digging that the graphql-request
library delegates additional options to the underlying node-fetch
module. So, instantiating a client like so, delegates the timeout to the node-fetch library.
const client = new GraphQLClient(config.url, {
timeout: 30000,
headers: {
Authorization: `Bearer ${token}`
}
})
Delegation: https://github.com/prisma/graphql-request/blob/master/src/index.ts#L29
node-fetch options: https://www.npmjs.com/package/node-fetch#options
It would be fantastic to get this added to the documentation.
from graphql-request.
For people who found this issue to look for how to implement timeout for graphql-request
:
Since graphql-request
supports custom fetch
like the above, we can easily add global timeout option without changing code of graphql-request
with the following helper:
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),
});
from graphql-request.
Any updates on this? It would be great to have.
from graphql-request.
@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
and then pass them in:
const options = { timeout: 30000, mode: 'cors' };
const graphQLClient = new GraphQLClient(requestURL, options);
from graphql-request.
node-fetch
has deprecated the timeout
option in their latest release in favor of passing an AbortSignal
. Unfortunately, passing this at the top level doesn't work correctly, as each request must have its own signal, and if the request was successful, the timeout on the signal must be cleared.
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!
from graphql-request.
If your project is using TypeScript, the solution above will need to add URL
as an alternative type of input
, like this:
const createFetchWithTimeout =
(timeout?: number) =>
async (input: RequestInfo | URL, init?: RequestInit)
Otherwise TS compiler would complain that RestquestInfo
is not assignable to RequestInfo | URL
.
from graphql-request.
My IDE complains about using timeout being not defined -
https://github.com/prisma-labs/graphql-request/blob/master/src/types.ts#L7?
How to make it work?
from graphql-request.
@jescalan I'm not familiar with AbortSignal
yet or how it could impact the graphql-request API. Could you start a new issue to show and discuss the design further? Once we have a some better understanding around the implications then I can better tell you if the PR is safe to start or not (safe meaning: will the work be wasted).
from graphql-request.
Related Issues (20)
- Wrong request reference
- graphql-request is throwing error TypeError: setPosition is not a function error HOT 4
- Disable window focus change refetching HOT 3
- Error in getResult function HOT 1
- Remove CommonJS build
- Replace `GraphQLClientRequestHeaders` with built-in `HeadersInit` type
- Types not exported through package.json HOT 1
- Fail to run project: "Module parse failed: Unexpected token", due to coalesing operator HOT 1
- Don't export GraphQLClientRequestHeaders in src/index.ts HOT 1
- ECONNRESET on node19+ HOT 2
- Connection pooling Mechanism HOT 2
- Empty result when variable isn't set or set with null HOT 1
- Cannot find module 'graphql-request/build/esm/types' or its corresponding type declarations.ts HOT 10
- Extension to bring back file upload support
- How do get the IP address of the client in the request middleware?
- I want the GraphQLClient instance to return response headers along with data HOT 2
- Include a new option to ignore operationName to avoid parsing for extraction. HOT 1
- How to pass headers while using `getServerSideProps` HOT 1
- Image Upload HOT 1
- Dynamic Headers: use async function to get headers HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from graphql-request.