Skip to content

Unable to call ApolloGateway.load() running version > 22 #61

@brettski

Description

@brettski

Calling ApolloGateway.load() throws the error Error: ApolloGateway.load called in surprising state loaded
We call this at,

const { schema, executor } = await graphServer.config.gateway.load();

The issue is due to an update made to Apollo Gateway in version 0.23.0 noted in their changelog

If you are on v2.18 or v2.19 of Apollo Server, you should upgrade to Apollo Server v2.20 before upgrading to this version, or your Node process may not shut down properly after stopping your Apollo Server.. Code that calls ApolloGateway.load is now expected to call ApolloGateway.stop. If you don't do that and you're using managed federation or experimental_pollInterval, the background polling interval will now keep a Node process alive rather than allowing it to exit if it's the only remaining event loop handler. Generally, ApolloServer is what calls ApolloGateway.load, and if you use at least v2.20.0 of Apollo Server, ApolloServer.stop() will invoke ApolloGateway.stop(). There's a bit of a hack where ApolloGateway does the old behavior if it believes that it is being called by a version of Apollo Server older than v2.18.0. So if you are manually calling ApolloGateway.load from your code, make sure to call ApolloGateway.stop when you're done, and don't use this version with Apollo Server v2.18 or v2.19. PR #452 apollo-server Issue #4428

I tried adding a gateway.stop() before, after, and other places but that didn't help this issue. I am not sure what it is looking for here or how to resolve this surprising state.

@apollo/gateway version currently locked at "0.22.0"

NOTE

A very strange aspect of this is that currently in production the package.json file has @apollo/gateway 0.23.0 listed and the refresh request works as expected. Locally, this isn't the case, even using that same version.

Looking at @apollo/gateway source, I don't see how we can get the correct state of initialized when calling load(). It seems we need to rethink the approach we are using to update the gateway when children are reloaded/deployed/etc.

https://github.com/apollographql/federation/blob/e3b3035711d31f60a053ceafa3828666fe808435/gateway-js/src/index.ts#L266-L274

  public async load(options?: {
    apollo?: ApolloConfig;
    engine?: GraphQLServiceEngineConfig;
  }) {
    if (this.state.phase !== 'initialized') {
      throw Error(
        `ApolloGateway.load called in surprising state ${this.state.phase}`,
      );
    }
....

Came across this issue in the Apollo Federation repo: apollographql/federation#461
It describes another user losing their ability to run ApolloGateway.load() to refresh schemas.

Metadata

Metadata

Labels

bugSomething isn't workinghelp wantedExtra attention is needed

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions