From 6ede1cab14374fc5d1a3b6d9f6f26c8893eeee6a Mon Sep 17 00:00:00 2001 From: Tiago Gimenes Date: Mon, 14 Jun 2021 20:17:03 -0300 Subject: [PATCH] retry on queries --- packages/gatsby-source-vtex/src/fetch.ts | 41 ++++++++++++++++++- .../gatsby-source-vtex/src/gatsby-node.ts | 33 ++------------- 2 files changed, 44 insertions(+), 30 deletions(-) diff --git a/packages/gatsby-source-vtex/src/fetch.ts b/packages/gatsby-source-vtex/src/fetch.ts index 1fb85689c9..3e463ed995 100644 --- a/packages/gatsby-source-vtex/src/fetch.ts +++ b/packages/gatsby-source-vtex/src/fetch.ts @@ -15,7 +15,7 @@ const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)) async function fetchRetry( path: string, - init: RequestInit, + init?: RequestInit, retryCount = 3 ): Promise { try { @@ -29,10 +29,49 @@ async function fetchRetry( await delay(300) // wait for 300ms + console.error( + `[gatsby-source-vtex]: Error while fetching. Retrying...`, + err + ) + return fetchRetry(path, init, retryCount - 1) } } +export const fetchGraphQL = async ( + path: string, + init?: RequestInit +): Promise<{ data: T; errors: any[] }> => { + let retryCount = 0 + let response: { data: T; errors: any[] } | undefined + + while (retryCount !== 3) { + response = await fetchRetry<{ data: T; errors: any[] }>(path, init) + + if (!response.errors || response.errors.length === 0) { + return response + } + + retryCount += 1 + console.error( + `[gatsby-source-vtex]: Error while fetching graphql data. Retry ${retryCount}`, + response.errors + ) + } + + if (response) { + for (const error of response.errors) { + console.error(error) + } + + throw new Error( + `[gatsby-source-vtex]: GraphQL Error\n\n: ${response.errors}` + ) + } + + throw new Error('[gatsby-source-vtex]: Error while fetching graphql data') +} + export const fetchVTEX = async ( path: string, options: VTEXOptions, diff --git a/packages/gatsby-source-vtex/src/gatsby-node.ts b/packages/gatsby-source-vtex/src/gatsby-node.ts index 0439b3a8aa..8e45f07e10 100644 --- a/packages/gatsby-source-vtex/src/gatsby-node.ts +++ b/packages/gatsby-source-vtex/src/gatsby-node.ts @@ -16,7 +16,7 @@ import type { } from 'gatsby' import { api } from './api' -import { fetchVTEX } from './fetch' +import { fetchGraphQL, fetchVTEX } from './fetch' import { md5 } from './md5' import { assertRedirects } from './redirects' import defaultStaticPaths from './staticPaths' @@ -91,41 +91,16 @@ export const sourceNodes: GatsbyNode['sourceNodes'] = async ( // Create executor to run queries against schema const url = getGraphQLUrl(tenant, workspace) - const executor: AsyncExecutor = async ({ document, variables }) => { - const query = print(document) - const fetchResult = await fetch(url, { + const executor: AsyncExecutor = ({ document, variables }) => + fetchGraphQL(url, { method: 'POST', headers: { accept: 'application/json', 'Content-Type': 'application/json', }, - body: JSON.stringify({ query, variables }), + body: JSON.stringify({ query: print(document), variables }), }) - const result = await fetchResult.json() - - /** - * We've chosen to ignore the 404 errors on build time. - * This allows us to complete builds with slightly old slugs and - * to handle this type of error on the client, where we will make - * some redirects. - */ - if (result.errors && result.errors.length > 0) { - result.errors = result.errors.filter((error: any) => { - console.warn(error) - const status = error.extensions?.exception?.status - - return !status || status !== 404 - }) - - if (result.errors.length === 0) { - delete result.errors - } - } - - return result - } - const schema = wrapSchema( { schema: await introspectSchema(executor),