UNPKG

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