graphql-request icon indicating copy to clipboard operation
graphql-request copied to clipboard

Is it possible to re-request the failed ones?

Open parthsynup opened this issue 3 years ago • 2 comments

parthsynup avatar Oct 15 '20 07:10 parthsynup

Are you thinking something along the lines of apollo-link-retry?

If so, I am here looking for that too. 🙂

CvBlixen avatar Nov 18 '20 07:11 CvBlixen

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 })

dougg0k avatar Jul 22 '22 19:07 dougg0k

Hi @dougg0k why the delay everyMs = 1_000 . what is the value 1_000?

huykon avatar Jan 12 '23 04:01 huykon

No reason, you can set the interval how much you want. 1000ms is 1s right?

dougg0k avatar Jan 12 '23 05:01 dougg0k

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?

huykon avatar Jan 12 '23 05:01 huykon

No idea. But you can log the catch there if you have doubts.

dougg0k avatar Jan 12 '23 12:01 dougg0k

Duplicate of https://github.com/jasonkuhrt/graphql-request/issues/235.

jasonkuhrt avatar Feb 03 '24 05:02 jasonkuhrt