Skip to content

Commit

Permalink
feat(gatsby-source-wordpress): PQR support (#32779)
Browse files Browse the repository at this point in the history
* we only want to check plugin requirements during initial boot up
* allow ingestRemoteSchema to run more than once per 10 seconds when not in development
  • Loading branch information
TylerBarnes authored Aug 25, 2021
1 parent 7116ab5 commit 2500324
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,12 @@ ${data.generalSettings.url}/wp-admin/options-permalink.php.
const ensurePluginRequirementsAreMet = async (
helpers: NodePluginArgs
): Promise<void> => {
if (helpers.traceId === `refresh-createSchemaCustomization`) {
if (
helpers.traceId === `refresh-createSchemaCustomization` ||
// PQR doesn't have a trace id.
// By the time this runs in PQR we don't need it to run again.
!helpers.traceId
) {
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,27 @@ import { cacheFetchedTypes } from "./cache-fetched-types"
import { writeQueriesToDisk } from "./write-queries-to-disk"

const ingestRemoteSchema = async (helpers, pluginOptions) => {
const schemaTimeKey = `lastIngestRemoteSchemaTime`
const lastIngestRemoteSchemaTime = await helpers.cache.get(schemaTimeKey)

const ingestedSchemaInLastTenSeconds =
Date.now() - lastIngestRemoteSchemaTime <= 10000

if (lastIngestRemoteSchemaTime && ingestedSchemaInLastTenSeconds) {
// only allow this to run once every ten seconds
// this prevents thrashing when many webhooks are received at once
return
if (process.env.NODE_ENV === `development`) {
// running this code block in production is problematic for PQR
// since this fn will run once for each worker and we need the result in each
// we'll return early in most workers when it checks the cache here
// Since PQR doesn't run in development and this code block was only meant for dev
// it should be ok to wrap it in this if statement
const schemaTimeKey = `lastIngestRemoteSchemaTime`
const lastIngestRemoteSchemaTime = await helpers.cache.get(schemaTimeKey)

const ingestedSchemaInLastTenSeconds =
Date.now() - lastIngestRemoteSchemaTime <= 10000

if (lastIngestRemoteSchemaTime && ingestedSchemaInLastTenSeconds) {
// only allow this to run once every ten seconds
// this prevents thrashing when many webhooks are received at once
return
}

await helpers.cache.set(schemaTimeKey, Date.now())
}

await helpers.cache.set(schemaTimeKey, Date.now())

const activity = helpers.reporter.activityTimer(
formatLogMessage(`ingest WPGraphQL schema`)
)
Expand Down

0 comments on commit 2500324

Please sign in to comment.