From cfa377919a18fa63c3c7e1138d15b33c2be55eae Mon Sep 17 00:00:00 2001 From: Vladimir Razuvaev Date: Sat, 11 Apr 2020 02:49:17 +0700 Subject: [PATCH] fix(gatsby): call schema rebuild manually on __refresh --- .../src/bootstrap/schema-hot-reloader.ts | 35 ++++++++----------- packages/gatsby/src/commands/develop.ts | 5 +++ 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/packages/gatsby/src/bootstrap/schema-hot-reloader.ts b/packages/gatsby/src/bootstrap/schema-hot-reloader.ts index 808b0ab70b89c..70390c29e12ac 100644 --- a/packages/gatsby/src/bootstrap/schema-hot-reloader.ts +++ b/packages/gatsby/src/bootstrap/schema-hot-reloader.ts @@ -7,7 +7,6 @@ import report from "gatsby-cli/lib/reporter" import { IGatsbyState } from "../redux/types" type TypeMap = IGatsbyState["inferenceMetadata"]["typeMap"] -type SchemaCustomization = IGatsbyState["schemaCustomization"] type InferenceMetadata = IGatsbyState["inferenceMetadata"] const inferredTypesChanged = ( @@ -19,41 +18,37 @@ const inferredTypesChanged = ( typeMap[type].dirty && !haveEqualFields(typeMap[type], prevTypeMap[type]) ) -const schemaChanged = ( - schemaCustomization: SchemaCustomization, - lastSchemaCustomization: SchemaCustomization -): boolean => - [`fieldExtensions`, `printConfig`, `thirdPartySchemas`, `types`].some( - key => schemaCustomization[key] !== lastSchemaCustomization[key] - ) - let lastMetadata: InferenceMetadata -let lastSchemaCustomization: SchemaCustomization // API_RUNNING_QUEUE_EMPTY could be emitted multiple types // in a short period of time, so debounce seems reasonable const maybeRebuildSchema = debounce(async (): Promise => { - const { inferenceMetadata, schemaCustomization } = store.getState() + const { inferenceMetadata } = store.getState() - if ( - !inferredTypesChanged(inferenceMetadata.typeMap, lastMetadata.typeMap) && - !schemaChanged(schemaCustomization, lastSchemaCustomization) - ) { + if (!inferredTypesChanged(inferenceMetadata.typeMap, lastMetadata.typeMap)) { return } const activity = report.activityTimer(`rebuild schema`) activity.start() - lastMetadata = cloneDeep(inferenceMetadata) - lastSchemaCustomization = schemaCustomization await rebuild({ parentSpan: activity }) await updateStateAndRunQueries(false, { parentSpan: activity }) activity.end() }, 1000) -export const bootstrapSchemaHotReloader = (): void => { - const { inferenceMetadata, schemaCustomization } = store.getState() +const snapshotInferenceMetadata = (): void => { + const { inferenceMetadata } = store.getState() lastMetadata = cloneDeep(inferenceMetadata) - lastSchemaCustomization = schemaCustomization +} + +export const bootstrapSchemaHotReloader = (): void => { + // Snapshot inference metadata at the time of the last schema rebuild + // (even if schema was rebuilt elsewhere) + // Using the snapshot later to check if inferred types actually changed since the last rebuild + snapshotInferenceMetadata() + emitter.on(`SET_SCHEMA`, snapshotInferenceMetadata) + + // Listen for node changes outside of a regular sourceNodes API call, + // e.g. markdown file update via watcher emitter.on(`API_RUNNING_QUEUE_EMPTY`, maybeRebuildSchema) } diff --git a/packages/gatsby/src/commands/develop.ts b/packages/gatsby/src/commands/develop.ts index 3f06dea88627c..54a9a9ee188f4 100644 --- a/packages/gatsby/src/commands/develop.ts +++ b/packages/gatsby/src/commands/develop.ts @@ -37,6 +37,7 @@ import { developStatic } from "./develop-static" import withResolverContext from "../schema/context" import sourceNodes from "../utils/source-nodes" import { createSchemaCustomization } from "../utils/create-schema-customization" +import { rebuild } from "../schema" import { websocketManager } from "../utils/websocket-manager" import getSslCert from "../utils/get-ssl-cert" import { slash } from "gatsby-core-utils" @@ -215,6 +216,10 @@ async function startServer(program: IProgram): Promise { webhookBody: req.body, }) activity.end() + activity = report.activityTimer(`rebuild schema`) + activity.start() + await rebuild({ parentSpan: activity }) + activity.end() } app.use(REFRESH_ENDPOINT, express.json()) app.post(REFRESH_ENDPOINT, (req, res) => {