Skip to content

Commit

Permalink
Prevent re-entrant calls to update callback
Browse files Browse the repository at this point in the history
  • Loading branch information
trevor-scheer committed Dec 9, 2021
1 parent b0a4c42 commit c2a88ea
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 2 deletions.
71 changes: 71 additions & 0 deletions gateway-js/src/__tests__/gateway/supergraphSdl.test.ts
Expand Up @@ -284,5 +284,76 @@ describe('Using supergraphSdl dynamic configuration', () => {
/The gateway subgraphs health check failed\. Updating to the provided `supergraphSdl` will likely result in future request failures to subgraphs\. The following error occurred during the health check/,
);
});

it('throws an error when `update` is called after gateway fails to load', async () => {
let updateCallback: SupergraphSdlUpdateFunction;
const supergraphSdl = getTestingSupergraphSdl();
gateway = new ApolloGateway({
async supergraphSdl({ update }) {
updateCallback = update;
return {
supergraphSdl: 'invalid SDL',
};
},
});

try {
await gateway.load();
} catch {}

expect(() =>
updateCallback!(supergraphSdl),
).toThrowErrorMatchingInlineSnapshot(
`"Can't call \`update\` callback after gateway failed to load."`,
);

// gateway failed to load, so we don't want the `afterEach` to call `gateway.stop()`
gateway = null;
});

it('throws an error when `update` is called while an update is in progress', async () => {
let updateCallback: SupergraphSdlUpdateFunction;
const supergraphSdl = getTestingSupergraphSdl();
gateway = new ApolloGateway({
async supergraphSdl({ update }) {
updateCallback = update;
return {
supergraphSdl,
};
},
experimental_didUpdateComposition() {
updateCallback(getTestingSupergraphSdl(fixturesWithUpdate));
},
});

await expect(gateway.load()).rejects.toThrowErrorMatchingInlineSnapshot(
`"Can't call \`update\` callback while supergraph update is in progress."`,
);

// gateway failed to load, so we don't want the `afterEach` to call `gateway.stop()`
gateway = null;
});

it('throws an error when `update` is called after gateway is stopped', async () => {
let updateCallback: SupergraphSdlUpdateFunction;
const supergraphSdl = getTestingSupergraphSdl();
gateway = new ApolloGateway({
async supergraphSdl({ update }) {
updateCallback = update;
return {
supergraphSdl,
};
},
});

await gateway.load();
await gateway.stop();

expect(() =>
updateCallback!(getTestingSupergraphSdl(fixturesWithUpdate)),
).toThrowErrorMatchingInlineSnapshot(
`"Can't call \`update\` callback after gateway has been stopped."`,
);
});
});
});
23 changes: 21 additions & 2 deletions gateway-js/src/index.ts
Expand Up @@ -133,7 +133,8 @@ type GatewayState =
pollWaitTimer: NodeJS.Timer;
doneWaiting: () => void;
}
| { phase: 'polling'; pollingDonePromise: Promise<void> };
| { phase: 'polling'; pollingDonePromise: Promise<void> }
| { phase: 'updating schema' };

// We want to be compatible with `load()` as called by both AS2 and AS3, so we
// define its argument types ourselves instead of relying on imports.
Expand Down Expand Up @@ -614,10 +615,19 @@ export class ApolloGateway implements GraphQLService {
}

private externalSupergraphUpdateCallback(supergraphSdl: string) {
if (this.state.phase === "failed to load") {
throw new Error("Can't call `update` callback after gateway failed to load.");
} else if (this.state.phase === "updating schema") {
throw new Error("Can't call `update` callback while supergraph update is in progress.");
} else if (this.state.phase === "stopped") {
throw new Error("Can't call `update` callback after gateway has been stopped.");
}
this.state = { phase: "updating schema" };
this.updateWithSupergraphSdl({
supergraphSdl,
id: this.getIdForSupergraphSdl(supergraphSdl),
});
this.state = { phase: "loaded" };
}

private async externalSubgraphHealthCheckCallback(supergraphSdl: string) {
Expand Down Expand Up @@ -942,6 +952,11 @@ export class ApolloGateway implements GraphQLService {
case 'loaded':
// This is the normal case.
break;
case 'updating schema':
// This should never happen
throw Error(
"ApolloGateway.pollServices called from an unexpected state 'updating schema'",
);
default:
throw new UnreachableCaseError(this.state);
}
Expand Down Expand Up @@ -1306,7 +1321,7 @@ export class ApolloGateway implements GraphQLService {
// schema polling). Can be called multiple times safely. Once it (async)
// returns, all gateway background activity will be finished.
public async stop() {
Promise.all(
await Promise.all(
this.toDispose.map((p) =>
p().catch((e) => {
this.logger.error(
Expand Down Expand Up @@ -1368,6 +1383,10 @@ export class ApolloGateway implements GraphQLService {
stoppingDone!();
return;
}
case "updating schema": {
// This should never happen
throw Error("`ApolloGateway.stop` called from an unexpected state `updating schema`");
}
default:
throw new UnreachableCaseError(this.state);
}
Expand Down

0 comments on commit c2a88ea

Please sign in to comment.