UNPKG

6.32 kBJavaScriptView Raw
1"use strict";
2var GraphQLModule_1;
3Object.defineProperty(exports, "__esModule", { value: true });
4exports.GraphQLModule = void 0;
5const tslib_1 = require("tslib");
6const common_1 = require("@nestjs/common");
7const core_1 = require("@nestjs/core");
8const metadata_scanner_1 = require("@nestjs/core/metadata-scanner");
9const messages_1 = require("@nestjs/core/helpers/messages");
10const abstract_graphql_driver_1 = require("./drivers/abstract-graphql.driver");
11const graphql_federation_factory_1 = require("./federation/graphql-federation.factory");
12const graphql_ast_explorer_1 = require("./graphql-ast.explorer");
13const graphql_schema_builder_1 = require("./graphql-schema.builder");
14const graphql_schema_host_1 = require("./graphql-schema.host");
15const graphql_types_loader_1 = require("./graphql-types.loader");
16const graphql_constants_1 = require("./graphql.constants");
17const graphql_factory_1 = require("./graphql.factory");
18const schema_builder_module_1 = require("./schema-builder/schema-builder.module");
19const services_1 = require("./services");
20const utils_1 = require("./utils");
21let GraphQLModule = GraphQLModule_1 = class GraphQLModule {
22 constructor(httpAdapterHost, options, _graphQlAdapter, graphQlTypesLoader) {
23 this.httpAdapterHost = httpAdapterHost;
24 this.options = options;
25 this._graphQlAdapter = _graphQlAdapter;
26 this.graphQlTypesLoader = graphQlTypesLoader;
27 }
28 get graphQlAdapter() {
29 return this._graphQlAdapter;
30 }
31 async onModuleDestroy() {
32 await this._graphQlAdapter.stop();
33 }
34 static forRoot(options = {}) {
35 this.assertDriver(options);
36 return {
37 module: GraphQLModule_1,
38 providers: [
39 {
40 provide: graphql_constants_1.GRAPHQL_MODULE_OPTIONS,
41 useValue: options,
42 },
43 {
44 provide: abstract_graphql_driver_1.AbstractGraphQLDriver,
45 useClass: options.driver,
46 },
47 ],
48 };
49 }
50 static forRootAsync(options) {
51 this.assertDriver(options);
52 return {
53 module: GraphQLModule_1,
54 imports: options.imports,
55 providers: [
56 ...this.createAsyncProviders(options),
57 {
58 provide: graphql_constants_1.GRAPHQL_MODULE_ID,
59 useValue: (0, utils_1.generateString)(),
60 },
61 {
62 provide: abstract_graphql_driver_1.AbstractGraphQLDriver,
63 useClass: options.driver,
64 },
65 ],
66 };
67 }
68 static createAsyncProviders(options) {
69 if (options.useExisting || options.useFactory) {
70 return [this.createAsyncOptionsProvider(options)];
71 }
72 return [
73 this.createAsyncOptionsProvider(options),
74 {
75 provide: options.useClass,
76 useClass: options.useClass,
77 },
78 ];
79 }
80 static createAsyncOptionsProvider(options) {
81 if (options.useFactory) {
82 return {
83 provide: graphql_constants_1.GRAPHQL_MODULE_OPTIONS,
84 useFactory: async (...args) => await options.useFactory(...args),
85 inject: options.inject || [],
86 };
87 }
88 return {
89 provide: graphql_constants_1.GRAPHQL_MODULE_OPTIONS,
90 useFactory: async (optionsFactory) => await optionsFactory.createGqlOptions(),
91 inject: [options.useExisting || options.useClass],
92 };
93 }
94 async onModuleInit() {
95 var _a;
96 const httpAdapter = (_a = this.httpAdapterHost) === null || _a === void 0 ? void 0 : _a.httpAdapter;
97 if (!httpAdapter) {
98 return;
99 }
100 const options = await this._graphQlAdapter.mergeDefaultOptions(this.options);
101 const { typePaths } = options;
102 const typeDefs = (await this.graphQlTypesLoader.mergeTypesByPaths(typePaths)) || [];
103 const mergedTypeDefs = (0, utils_1.extend)(typeDefs, options.typeDefs);
104 await this._graphQlAdapter.start({
105 ...options,
106 typeDefs: mergedTypeDefs,
107 });
108 if (options.path) {
109 GraphQLModule_1.logger.log((0, messages_1.ROUTE_MAPPED_MESSAGE)(options.path, common_1.RequestMethod.POST));
110 }
111 }
112 static assertDriver(options) {
113 if (!options.driver) {
114 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:
115
116GraphQLModule.forRoot<ApolloDriverConfig>({
117 driver: ApolloDriver,
118})`;
119 this.logger.error(errorMessage);
120 throw new Error(errorMessage);
121 }
122 }
123};
124GraphQLModule.logger = new common_1.Logger(GraphQLModule_1.name, { timestamp: true });
125GraphQLModule = GraphQLModule_1 = tslib_1.__decorate([
126 (0, common_1.Module)({
127 imports: [schema_builder_module_1.GraphQLSchemaBuilderModule],
128 providers: [
129 graphql_factory_1.GraphQLFactory,
130 metadata_scanner_1.MetadataScanner,
131 services_1.ResolversExplorerService,
132 services_1.ScalarsExplorerService,
133 graphql_ast_explorer_1.GraphQLAstExplorer,
134 graphql_types_loader_1.GraphQLTypesLoader,
135 graphql_schema_builder_1.GraphQLSchemaBuilder,
136 graphql_schema_host_1.GraphQLSchemaHost,
137 graphql_federation_factory_1.GraphQLFederationFactory,
138 ],
139 exports: [
140 graphql_types_loader_1.GraphQLTypesLoader,
141 graphql_ast_explorer_1.GraphQLAstExplorer,
142 graphql_schema_host_1.GraphQLSchemaHost,
143 graphql_federation_factory_1.GraphQLFederationFactory,
144 ],
145 }),
146 tslib_1.__param(1, (0, common_1.Inject)(graphql_constants_1.GRAPHQL_MODULE_OPTIONS)),
147 tslib_1.__metadata("design:paramtypes", [core_1.HttpAdapterHost, Object, abstract_graphql_driver_1.AbstractGraphQLDriver,
148 graphql_types_loader_1.GraphQLTypesLoader])
149], GraphQLModule);
150exports.GraphQLModule = GraphQLModule;