Skip to content

Commit

Permalink
fix(gatsby): call schema rebuild manually on __refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
vladar committed Apr 10, 2020
1 parent 3ac14e8 commit cfa3779
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
35 changes: 15 additions & 20 deletions packages/gatsby/src/bootstrap/schema-hot-reloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand All @@ -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<void> => {
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)
}
5 changes: 5 additions & 0 deletions packages/gatsby/src/commands/develop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -215,6 +216,10 @@ async function startServer(program: IProgram): Promise<IServer> {
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) => {
Expand Down

0 comments on commit cfa3779

Please sign in to comment.