Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gatsby-source-wordpress): Add retries to graphql requests #35949

Merged
merged 8 commits into from
Jun 30, 2022
Merged
36 changes: 27 additions & 9 deletions packages/gatsby-source-wordpress/src/utils/fetch-graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import clipboardy from "clipboardy"
import axios, { AxiosRequestConfig, AxiosResponse } from "axios"
import rateLimit, { RateLimitedAxiosInstance } from "axios-rate-limit"
import { bold } from "chalk"
import retry from "async-retry"
import { formatLogMessage } from "./format-log-message"
import store from "~/store"
import { getPluginOptions } from "./get-gatsby-api"
Expand All @@ -27,6 +28,13 @@ export const moduleHelpers = {
},
}

const errorIs500ish = (e: Error): boolean =>
e.message.includes(`Request failed with status code 50`) &&
(e.message.includes(`502`) ||
TylerBarnes marked this conversation as resolved.
Show resolved Hide resolved
e.message.includes(`503`) ||
e.message.includes(`500`) ||
e.message.includes(`504`))

interface IHandleErrorOptionsInput {
variables: IJSON
query: string
Expand Down Expand Up @@ -331,12 +339,7 @@ const handleFetchErrors = async ({
return
}

if (
e.message.includes(`Request failed with status code 50`) &&
(e.message.includes(`502`) ||
e.message.includes(`503`) ||
e.message.includes(`504`))
) {
if (errorIs500ish(e) && !e.message.includes(`500`)) {
if (`message` in e) {
console.error(formatLogMessage(new Error(e.message).stack))
}
Expand Down Expand Up @@ -729,9 +732,24 @@ const fetchGraphql = async ({
requestOptions.auth = htaccessCredentials
}

response = await moduleHelpers
.getHttp(limit)
.post(url, { query, variables }, requestOptions)
response = await retry(
(bail: (e: Error) => void) =>
moduleHelpers
.getHttp(limit)
.post(url, { query, variables }, requestOptions)
.catch(e => {
if (!errorIs500ish(e)) {
// for any error that is not a 50x error, we bail, meaning we stop retrying. error will be thrown one level higher
bail(e)

return null
} else {
// otherwise throwing the error will cause the retry to happen again
throw e
}
}),
{ retries: 5 }
TylerBarnes marked this conversation as resolved.
Show resolved Hide resolved
)

if (response.data === ``) {
throw new Error(`GraphQL request returned an empty string.`)
Expand Down