graphqlnestjsapollo-federation

how to hot reload federation gateway in NestJS


Problem

In a federated nest app, a gateway collects all the schemas from other services and form a complete graph. The question is, how to re-run the schema collection after a sub-schema has been changed?

Current Workaround

Restarting the gateway solves the problem, but it does not seem like an elegant solution.

Other Resources

  1. Apollo server supports managed federation which essentially reverts the dependency between the gateway and the services. Sadly I couldn't find anything relating it to NestJS.

Solution

  • You can add a pollIntervalInMs option to the supergraphSdl configuration. That will automatically poll the services again in each interval.

    @Module({
      imports: [
        GraphQLModule.forRootAsync<ApolloGatewayDriverConfig>({
          driver: ApolloGatewayDriver,
          useFactory: async () => ({
            server: {
              path: '/graphql',
              cors: true
            },
            gateway: {
              supergraphSdl: new IntrospectAndCompose({
                subgraphs: [
                  { name: 'example-service', url: 'http://localhost:8081/graphql' },
                ],
                pollIntervalInMs: 15000,
              })
            },
          })
        })
      ],
    })
    export class AppModule {}