graphql-request
graphql-request copied to clipboard
Is it possible to re-request the failed ones?
Are you thinking something along the lines of apollo-link-retry?
If so, I am here looking for that too. 🙂
It seems there is nothing built-in, but you can do by hand.
function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function requestWithRetry(
requestPromise,
{ retries = 3, everyMs = 1_000 },
retriesCount = 0,
) {
try {
return await requestPromise;
} catch {
const updatedCount = retriesCount + 1;
if (updatedCount > retries) {
return null;
}
await delay(everyMs);
return await requestWithRetry(
requestPromise,
{ retries, everyMs },
updatedCount,
);
}
}
const client = new GraphQLClient(endpoint, { headers: {} })
const data = await requestWithRetry(client.request(query, variables), { retries: 5 })
Hi @dougg0k why the delay everyMs = 1_000
. what is the value 1_000
?
No reason, you can set the interval how much you want. 1000ms is 1s right?
Thanks @dougg0k I set it to 1000 and then check the network tab of chrome browser. Once the request fail, I don't the the requests trying on network tab. Do you know why?
No idea. But you can log the catch there if you have doubts.
Duplicate of https://github.com/jasonkuhrt/graphql-request/issues/235.