@nestjs/graphql#GraphQLSchemaHost TypeScript Examples

The following examples show how to use @nestjs/graphql#GraphQLSchemaHost. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: graphql.factory.ts    From nestjs-mercurius with MIT License 6 votes vote down vote up
constructor(
    resolversExplorerService: ResolversExplorerService,
    scalarsExplorerService: ScalarsExplorerService,
    pluginExplorerService: PluginsExplorerService,
    graphqlAstExplorer: GraphQLAstExplorer,
    gqlSchemaBuilder: GraphQLSchemaBuilder,
    gqlSchemaHost: GraphQLSchemaHost,
    protected readonly loaderExplorerService: LoadersExplorerService,
  ) {
    super(
      resolversExplorerService,
      scalarsExplorerService,
      pluginExplorerService,
      graphqlAstExplorer,
      gqlSchemaBuilder,
      gqlSchemaHost,
    );
  }
Example #2
Source File: web.module.ts    From relate with GNU General Public License v3.0 5 votes vote down vote up
constructor(
        @Inject(GraphQLSchemaHost) private readonly schemaHost: GraphQLSchemaHost,
        @Inject(HttpAdapterHost) private readonly httpAdapterHost: HttpAdapterHost,
    ) {}
Example #3
Source File: mercurius.module.ts    From nestjs-mercurius with MIT License 4 votes vote down vote up
@Module({
  imports: [GraphQLSchemaBuilderModule, MercuriusCoreModule],
  providers: [
    GraphQLFactory,
    MetadataScanner,
    ResolversExplorerService,
    ScalarsExplorerService,
    PluginsExplorerService,
    GraphQLAstExplorer,
    GraphQLTypesLoader,
    GraphQLSchemaBuilder,
    GraphQLSchemaHost,
    LoadersExplorerService,
    HookExplorerService,
  ],
})
export class MercuriusModule
  extends BaseMercuriusModule<MercuriusModuleOptions>
  implements OnModuleInit
{
  constructor(
    private readonly graphqlFactory: GraphQLFactory,
    private readonly graphqlTypesLoader: GraphQLTypesLoader,
    @Inject(GRAPHQL_MODULE_OPTIONS)
    protected readonly options: MercuriusModuleOptions,
    protected readonly applicationConfig: ApplicationConfig,
    protected readonly httpAdapterHost: HttpAdapterHost,
    protected readonly hookExplorerService: HookExplorerService,
  ) {
    super(httpAdapterHost, applicationConfig, options, hookExplorerService);
  }

  static forRoot(options: MercuriusModuleOptions) {
    options = mergeDefaults(options);
    return {
      module: MercuriusModule,
      providers: [
        {
          provide: GRAPHQL_MODULE_OPTIONS,
          useValue: options,
        },
      ],
    };
  }

  static forRootAsync(options: MercuriusModuleAsyncOptions): DynamicModule {
    return {
      module: MercuriusModule,
      imports: options.imports,
      providers: [
        ...this.createAsyncProviders(options),
        {
          provide: GRAPHQL_MODULE_ID,
          useValue: generateString(),
        },
      ],
    };
  }

  private static createAsyncProviders(
    options: MercuriusModuleAsyncOptions,
  ): Provider[] {
    if (options.useExisting || options.useFactory) {
      return [this.createAsyncOptionsProvider(options)];
    }
    return [
      this.createAsyncOptionsProvider(options),
      {
        provide: options.useClass,
        useClass: options.useClass,
      },
    ];
  }

  private static createAsyncOptionsProvider(
    options: MercuriusModuleAsyncOptions,
  ): Provider {
    if (options.useFactory) {
      return {
        provide: GRAPHQL_MODULE_OPTIONS,
        useFactory: async (...args: any[]) =>
          mergeDefaults(await options.useFactory(...args)),
        inject: options.inject || [],
      };
    }
    return {
      provide: GRAPHQL_MODULE_OPTIONS,
      useFactory: async (optionsFactory: MercuriusOptionsFactory) =>
        mergeDefaults(await optionsFactory.createMercuriusOptions()),
      inject: [options.useExisting || options.useClass],
    };
  }

  async onModuleInit() {
    if (!this.httpAdapterHost) {
      return;
    }
    const httpAdapter = this.httpAdapterHost.httpAdapter;
    if (!httpAdapter) {
      return;
    }
    const typeDefs =
      (await this.graphqlTypesLoader.mergeTypesByPaths(
        this.options.typePaths,
      )) || [];
    const mergedTypeDefs = extend(typeDefs, this.options.typeDefs);

    const mercuriusOptions = (await this.graphqlFactory.mergeOptions({
      ...this.options,
      typeDefs: mergedTypeDefs,
    } as any)) as unknown as MercuriusModuleOptions;

    if (
      this.options.definitions &&
      this.options.definitions.path &&
      mercuriusOptions.schema instanceof GraphQLSchema
    ) {
      await this.graphqlFactory.generateDefinitions(
        printSchema(mercuriusOptions.schema),
        this.options as any,
      );
    }
    await this.registerGqlServer(mercuriusOptions);
  }
}