UNPKG

@nestjs/graphql

Version:

Nest - modern, fast, powerful node.js web framework (@graphql)

181 lines (179 loc) 6.95 kB
var GraphQLModule_1; import { __decorate, __metadata, __param } from "tslib"; import { Inject, Logger, Module, RequestMethod } from '@nestjs/common'; import { HttpAdapterHost } from '@nestjs/core'; import { ROUTE_MAPPED_MESSAGE } from '@nestjs/core/helpers/messages.js'; import { InitializeOnPreviewAllowlist } from '@nestjs/core/inspector/index.js'; import { MetadataScanner } from '@nestjs/core/metadata-scanner.js'; import { AbstractGraphQLDriver } from './drivers/abstract-graphql.driver.js'; import { GraphQLFederationFactory } from './federation/graphql-federation.factory.js'; import { TypeDefsDecoratorFactory } from './federation/type-defs-decorator.factory.js'; import { GraphQLAstExplorer } from './graphql-ast.explorer.js'; import { GraphQLSchemaBuilder } from './graphql-schema.builder.js'; import { GraphQLSchemaHost } from './graphql-schema.host.js'; import { GraphQLTypesLoader } from './graphql-types.loader.js'; import { GRAPHQL_MODULE_ID, GRAPHQL_MODULE_OPTIONS, } from './graphql.constants.js'; import { GraphQLFactory } from './graphql.factory.js'; import { MetadataLoader } from './plugin/metadata-loader.js'; import { GraphQLSchemaBuilderModule } from './schema-builder/schema-builder.module.js'; import { ResolverDecoratorHost, ResolversExplorerService, ScalarsExplorerService, } from './services/index.js'; import { extend, generateString } from './utils/index.js'; /** * @publicApi */ let GraphQLModule = GraphQLModule_1 = class GraphQLModule { get graphQlAdapter() { return this._graphQlAdapter; } constructor(httpAdapterHost, options, _graphQlAdapter, graphQlTypesLoader, gqlSchemaHost) { this.httpAdapterHost = httpAdapterHost; this.options = options; this._graphQlAdapter = _graphQlAdapter; this.graphQlTypesLoader = graphQlTypesLoader; this.gqlSchemaHost = gqlSchemaHost; this.metadataLoader = new MetadataLoader(); } async onModuleDestroy() { if (!this.options.stopOnApplicationShutdown) { await this._graphQlAdapter.stop(); } } async onApplicationShutdown() { if (this.options.stopOnApplicationShutdown) { await this._graphQlAdapter.stop(); } } static forRoot(options = {}) { this.assertDriver(options); return { module: GraphQLModule_1, providers: [ { provide: GRAPHQL_MODULE_OPTIONS, useValue: options, }, { provide: AbstractGraphQLDriver, useClass: options.driver, }, ], }; } static forRootAsync(options) { this.assertDriver(options); return { module: GraphQLModule_1, imports: options.imports, providers: [ ...this.createAsyncProviders(options), { provide: GRAPHQL_MODULE_ID, useValue: generateString(), }, { provide: AbstractGraphQLDriver, useClass: options.driver, }, ], }; } static createAsyncProviders(options) { if (options.useExisting || options.useFactory) { return [this.createAsyncOptionsProvider(options)]; } return [ this.createAsyncOptionsProvider(options), { provide: options.useClass, useClass: options.useClass, }, ]; } static createAsyncOptionsProvider(options) { if (options.useFactory) { return { provide: GRAPHQL_MODULE_OPTIONS, useFactory: async (...args) => await options.useFactory(...args), inject: options.inject || [], }; } return { provide: GRAPHQL_MODULE_OPTIONS, useFactory: async (optionsFactory) => await optionsFactory.createGqlOptions(), inject: [options.useExisting || options.useClass], }; } async onModuleInit() { if (this.options.metadata) { const metadataFn = this.options.metadata; const metadata = await metadataFn(); await this.metadataLoader.load(metadata); } const options = await this._graphQlAdapter.mergeDefaultOptions(this.options); const { typePaths } = options; const typeDefs = (await this.graphQlTypesLoader.mergeTypesByPaths(typePaths)) || []; const mergedTypeDefs = extend(typeDefs, options.typeDefs); const gqlSchema = await this._graphQlAdapter.generateSchema({ ...options, typeDefs: mergedTypeDefs, }); this.gqlSchemaHost.schema = gqlSchema; this.completeOptions = { ...options, schema: gqlSchema, typeDefs: undefined, }; const httpAdapter = this.httpAdapterHost?.httpAdapter; if (!httpAdapter) { return; } await this._graphQlAdapter.start(this.completeOptions); if (options.path) { GraphQLModule_1.logger.log(ROUTE_MAPPED_MESSAGE(options.path, RequestMethod.POST)); } } static assertDriver(options) { if (!options.driver) { const errorMessage = `Missing "driver" option. In the latest version of "@nestjs/graphql" package (v10) a new required configuration property called "driver" has been introduced. Check out the official documentation for more details on how to migrate (https://docs.nestjs.com/graphql/migration-guide). Example: GraphQLModule.forRoot<ApolloDriverConfig>({ driver: ApolloDriver, })`; this.logger.error(errorMessage); throw new Error(errorMessage); } } }; GraphQLModule.logger = new Logger(GraphQLModule_1.name, { timestamp: true, }); GraphQLModule = GraphQLModule_1 = __decorate([ Module({ imports: [GraphQLSchemaBuilderModule], providers: [ GraphQLFactory, MetadataScanner, ResolversExplorerService, ScalarsExplorerService, GraphQLAstExplorer, GraphQLTypesLoader, GraphQLSchemaBuilder, GraphQLSchemaHost, GraphQLFederationFactory, TypeDefsDecoratorFactory, ResolverDecoratorHost, ], exports: [ GraphQLTypesLoader, GraphQLAstExplorer, GraphQLSchemaHost, GraphQLFederationFactory, ResolverDecoratorHost, ], }), __param(1, Inject(GRAPHQL_MODULE_OPTIONS)), __metadata("design:paramtypes", [HttpAdapterHost, Object, AbstractGraphQLDriver, GraphQLTypesLoader, GraphQLSchemaHost]) ], GraphQLModule); export { GraphQLModule }; InitializeOnPreviewAllowlist.add(GraphQLModule);