'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 1:[1]

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 {}

Solution 2:[2]

When configuring gateway application with NestJS, and when already have integrated with Apollo studio, then you need not define any serviceList in GraphQLGatewayModule. This is how your module initialization should look like:

GraphQLGatewayModule.forRootAsync({
  useFactory: async () => ({
    gateway: {},
    server: {
      path: '/graphql',
    },
  }),
})

Following environment variables should be declared on the machine hosting your gateway application:

APOLLO_KEY: "service:<graphid>:<hash>"
APOLLO_SCHEMA_CONFIG_DELIVERY_ENDPOINT: "https://uplink.api.apollographql.com/"

Post deployment of Federated GraphQL service, you may need to run apollo/rover CLI service:push command like below to update the schema which writes to schema registry and then gets pushed to uplink URL which is polled by gateway periodically:

npx apollo service:push --graph=<graph id> --key=service:<graph id>:<hash> --variant=<environment name> --serviceName=<service name> --serviceURL=<URL of your service with /graphql path> --endpoint=<URL of your service with /graphql path>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Orel Kevin Edry
Solution 2