NestJS + Apollo Federation Gateway Save Schema

NestJS + Apollo Federation Gateway Save Schema


0

Question

When making the Apollo Gateway leveraging the NestJS GraphQL Module, how can you save the schema file?

Background

Normally with NestJS GraphQL support you can save the schema file with something like this.

GraphQLModule.forRoot<ApolloFederationDriverConfig>({
      driver: ApolloFederationDriver,
      autoSchemaFile: {
        path: join(process.cwd(), './schema.gql'),
        federation: 2
      },
      playground: true
    }),

However, it seems like for the Gateway it seems like the autoSchemaFile is specifically excluded. So how can I save the schema?

I’m developing within a mono-repo with code generation, so ideally I store the schema so that I can directly reference the schema file when generating clent side code.

Things I’ve Tried

Generated the Schema Manually

I’ve tried to get the Schema after the app has initialized as shown below. However the code below produces the error attached.

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { GraphQLSchemaHost } from '@nestjs/graphql';

async function bootstrap() {
  const app = await NestFactory.create(AppModule, { cors: true });
  await app.listen(3002);

  const { schema } = app.get(GraphQLSchemaHost);
}
bootstrap();
Error: GraphQL schema has not yet been created. Make sure to call the "GraphQLSchemaHost#schema" getter when the application is already initialized (after the "onModuleInit" hook triggered by either "app.listen()" or "app.init()" method).

I also tried the approach of adding onModuleInit to app.module however, the same error is produced making me think that the schema is generated later with the Gateway then in normal GraphQL drivers.

Additional Information

Current GraphQL Gateway Config

GraphQLModule.forRootAsync<ApolloGatewayDriverConfig>({
      imports: [ConfigModule],
      inject: [ConfigService],
      driver: ApolloGatewayDriver,
      useFactory: async (configService: ConfigService) => ({
        gateway: {
          supergraphSdl: new IntrospectAndCompose({
            subgraphs: [
              { name: 'service_1', url: configService.getOrThrow('service_1.uri') },
              { name: 'service_2', url: configService.getOrThrow('service_2.uri') },
            ]
          })
        }
      })
    })

Share
Improve this question


Load 4 more related questions


Show fewer related questions

0

Reset to default



Browse other questions tagged

or ask your own question.

Leave a Reply

Your email address will not be published. Required fields are marked *