Skip to content

Commit 0a36f7b

Browse files
authored
fix(watch): don't built schema twice in watch mode (#558)
1 parent be18000 commit 0a36f7b

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

packages/graphile-build-pg/src/plugins/PgIntrospectionPlugin.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ export default (async function PgIntrospectionPlugin(
861861
this._start();
862862
}
863863

864-
async _start() {
864+
async _start(isReconnect = false) {
865865
if (this.stopped) {
866866
return;
867867
}
@@ -921,7 +921,9 @@ export default (async function PgIntrospectionPlugin(
921921
}
922922

923923
// Trigger re-introspection on server reconnect
924-
this._handleChange();
924+
if (isReconnect) {
925+
this._handleChange();
926+
}
925927
}
926928
} catch (e) {
927929
// If something goes wrong, disconnect and try again after a short delay
@@ -949,7 +951,7 @@ export default (async function PgIntrospectionPlugin(
949951
setTimeout(() => {
950952
if (!this.stopped) {
951953
// Listen for further changes
952-
this._start();
954+
this._start(true);
953955
}
954956
}, 2000);
955957
}

packages/graphile-build/src/SchemaBuilder.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,15 @@ class SchemaBuilder extends EventEmitter {
510510
try {
511511
this._busy = true;
512512
this._explicitSchemaListener = listener;
513+
514+
// We want to ignore `triggerChange` calls that occur whilst we're setting
515+
// up the listeners to prevent an unnecessary double schema build.
516+
let ignoreChangeTriggers = true;
517+
513518
this.triggerChange = () => {
519+
if (ignoreChangeTriggers) {
520+
return;
521+
}
514522
this._generatedSchema = null;
515523
// XXX: optionally debounce
516524
try {
@@ -530,10 +538,16 @@ class SchemaBuilder extends EventEmitter {
530538
for (const fn of this.watchers) {
531539
await fn(this.triggerChange);
532540
}
541+
542+
// Now we're about to build the first schema, any further `triggerChange`
543+
// calls should be honoured.
544+
ignoreChangeTriggers = false;
545+
533546
if (listener) {
534547
this.on("schema", listener);
535548
}
536549
this.emit("schema", this.buildSchema());
550+
537551
this._watching = true;
538552
} finally {
539553
this._busy = false;

0 commit comments

Comments
 (0)