Skip to content

Commit

Permalink
retry on queries
Browse files Browse the repository at this point in the history
  • Loading branch information
tlgimenes committed Jun 14, 2021
1 parent 85962e7 commit 6ede1ca
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 30 deletions.
41 changes: 40 additions & 1 deletion packages/gatsby-source-vtex/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))

async function fetchRetry<T>(
path: string,
init: RequestInit,
init?: RequestInit,
retryCount = 3
): Promise<T> {
try {
Expand All @@ -29,10 +29,49 @@ async function fetchRetry<T>(

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 <T>(
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 <T>(
path: string,
options: VTEXOptions,
Expand Down
33 changes: 4 additions & 29 deletions packages/gatsby-source-vtex/src/gatsby-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 6ede1ca

Please sign in to comment.