1 | "use strict";
|
2 | var GraphQLModule_1;
|
3 | Object.defineProperty(exports, "__esModule", { value: true });
|
4 | exports.GraphQLModule = void 0;
|
5 | const tslib_1 = require("tslib");
|
6 | const common_1 = require("@nestjs/common");
|
7 | const load_package_util_1 = require("@nestjs/common/utils/load-package.util");
|
8 | const core_1 = require("@nestjs/core");
|
9 | const metadata_scanner_1 = require("@nestjs/core/metadata-scanner");
|
10 | const graphql_1 = require("graphql");
|
11 | const graphql_ast_explorer_1 = require("./graphql-ast.explorer");
|
12 | const graphql_schema_builder_1 = require("./graphql-schema.builder");
|
13 | const graphql_schema_host_1 = require("./graphql-schema.host");
|
14 | const graphql_types_loader_1 = require("./graphql-types.loader");
|
15 | const graphql_constants_1 = require("./graphql.constants");
|
16 | const graphql_factory_1 = require("./graphql.factory");
|
17 | const schema_builder_module_1 = require("./schema-builder/schema-builder.module");
|
18 | const services_1 = require("./services");
|
19 | const utils_1 = require("./utils");
|
20 | let GraphQLModule = GraphQLModule_1 = class GraphQLModule {
|
21 | constructor(httpAdapterHost, options, graphqlFactory, graphqlTypesLoader, applicationConfig) {
|
22 | this.httpAdapterHost = httpAdapterHost;
|
23 | this.options = options;
|
24 | this.graphqlFactory = graphqlFactory;
|
25 | this.graphqlTypesLoader = graphqlTypesLoader;
|
26 | this.applicationConfig = applicationConfig;
|
27 | }
|
28 | get apolloServer() {
|
29 | return this._apolloServer;
|
30 | }
|
31 | static forRoot(options = {}) {
|
32 | options = utils_1.mergeDefaults(options);
|
33 | return {
|
34 | module: GraphQLModule_1,
|
35 | providers: [
|
36 | {
|
37 | provide: graphql_constants_1.GRAPHQL_MODULE_OPTIONS,
|
38 | useValue: options,
|
39 | },
|
40 | ],
|
41 | };
|
42 | }
|
43 | static forRootAsync(options) {
|
44 | return {
|
45 | module: GraphQLModule_1,
|
46 | imports: options.imports,
|
47 | providers: [
|
48 | ...this.createAsyncProviders(options),
|
49 | {
|
50 | provide: graphql_constants_1.GRAPHQL_MODULE_ID,
|
51 | useValue: utils_1.generateString(),
|
52 | },
|
53 | ],
|
54 | };
|
55 | }
|
56 | static createAsyncProviders(options) {
|
57 | if (options.useExisting || options.useFactory) {
|
58 | return [this.createAsyncOptionsProvider(options)];
|
59 | }
|
60 | return [
|
61 | this.createAsyncOptionsProvider(options),
|
62 | {
|
63 | provide: options.useClass,
|
64 | useClass: options.useClass,
|
65 | },
|
66 | ];
|
67 | }
|
68 | static createAsyncOptionsProvider(options) {
|
69 | if (options.useFactory) {
|
70 | return {
|
71 | provide: graphql_constants_1.GRAPHQL_MODULE_OPTIONS,
|
72 | useFactory: (...args) => tslib_1.__awaiter(this, void 0, void 0, function* () { return utils_1.mergeDefaults(yield options.useFactory(...args)); }),
|
73 | inject: options.inject || [],
|
74 | };
|
75 | }
|
76 | return {
|
77 | provide: graphql_constants_1.GRAPHQL_MODULE_OPTIONS,
|
78 | useFactory: (optionsFactory) => tslib_1.__awaiter(this, void 0, void 0, function* () { return utils_1.mergeDefaults(yield optionsFactory.createGqlOptions()); }),
|
79 | inject: [options.useExisting || options.useClass],
|
80 | };
|
81 | }
|
82 | onModuleInit() {
|
83 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
84 | if (!this.httpAdapterHost) {
|
85 | return;
|
86 | }
|
87 | const httpAdapter = this.httpAdapterHost.httpAdapter;
|
88 | if (!httpAdapter) {
|
89 | return;
|
90 | }
|
91 | const typeDefs = (yield this.graphqlTypesLoader.mergeTypesByPaths(this.options.typePaths)) || [];
|
92 | const mergedTypeDefs = utils_1.extend(typeDefs, this.options.typeDefs);
|
93 | const apolloOptions = yield this.graphqlFactory.mergeOptions(Object.assign(Object.assign({}, this.options), { typeDefs: mergedTypeDefs }));
|
94 | yield this.runExecutorFactoryIfPresent(apolloOptions);
|
95 | if (this.options.definitions && this.options.definitions.path) {
|
96 | yield this.graphqlFactory.generateDefinitions(graphql_1.printSchema(apolloOptions.schema), this.options);
|
97 | }
|
98 | yield this.registerGqlServer(apolloOptions);
|
99 | if (this.options.installSubscriptionHandlers) {
|
100 | this._apolloServer.installSubscriptionHandlers(httpAdapter.getHttpServer());
|
101 | }
|
102 | });
|
103 | }
|
104 | onModuleDestroy() {
|
105 | var _a;
|
106 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
107 | yield ((_a = this._apolloServer) === null || _a === void 0 ? void 0 : _a.stop());
|
108 | });
|
109 | }
|
110 | registerGqlServer(apolloOptions) {
|
111 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
112 | const httpAdapter = this.httpAdapterHost.httpAdapter;
|
113 | const platformName = httpAdapter.getType();
|
114 | if (platformName === 'express') {
|
115 | this.registerExpress(apolloOptions);
|
116 | }
|
117 | else if (platformName === 'fastify') {
|
118 | yield this.registerFastify(apolloOptions);
|
119 | }
|
120 | else {
|
121 | throw new Error(`No support for current HttpAdapter: ${platformName}`);
|
122 | }
|
123 | });
|
124 | }
|
125 | registerExpress(apolloOptions) {
|
126 | const { ApolloServer } = load_package_util_1.loadPackage('apollo-server-express', 'GraphQLModule', () => require('apollo-server-express'));
|
127 | const path = this.getNormalizedPath(apolloOptions);
|
128 | const { disableHealthCheck, onHealthCheck, cors, bodyParserConfig, } = this.options;
|
129 | const httpAdapter = this.httpAdapterHost.httpAdapter;
|
130 | const app = httpAdapter.getInstance();
|
131 | const apolloServer = new ApolloServer(apolloOptions);
|
132 | apolloServer.applyMiddleware({
|
133 | app,
|
134 | path,
|
135 | disableHealthCheck,
|
136 | onHealthCheck,
|
137 | cors,
|
138 | bodyParserConfig,
|
139 | });
|
140 | this._apolloServer = apolloServer;
|
141 | }
|
142 | registerFastify(apolloOptions) {
|
143 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
144 | const { ApolloServer } = load_package_util_1.loadPackage('apollo-server-fastify', 'GraphQLModule', () => require('apollo-server-fastify'));
|
145 | const httpAdapter = this.httpAdapterHost.httpAdapter;
|
146 | const app = httpAdapter.getInstance();
|
147 | const path = this.getNormalizedPath(apolloOptions);
|
148 | const apolloServer = new ApolloServer(apolloOptions);
|
149 | const { disableHealthCheck, onHealthCheck, cors, bodyParserConfig, } = this.options;
|
150 | yield app.register(apolloServer.createHandler({
|
151 | disableHealthCheck,
|
152 | onHealthCheck,
|
153 | cors,
|
154 | bodyParserConfig,
|
155 | path,
|
156 | }));
|
157 | this._apolloServer = apolloServer;
|
158 | });
|
159 | }
|
160 | getNormalizedPath(apolloOptions) {
|
161 | const prefix = this.applicationConfig.getGlobalPrefix();
|
162 | const useGlobalPrefix = prefix && this.options.useGlobalPrefix;
|
163 | const gqlOptionsPath = utils_1.normalizeRoutePath(apolloOptions.path);
|
164 | return useGlobalPrefix
|
165 | ? utils_1.normalizeRoutePath(prefix) + gqlOptionsPath
|
166 | : gqlOptionsPath;
|
167 | }
|
168 | runExecutorFactoryIfPresent(apolloOptions) {
|
169 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
170 | if (!apolloOptions.executorFactory) {
|
171 | return;
|
172 | }
|
173 | const executor = yield apolloOptions.executorFactory(apolloOptions.schema);
|
174 | apolloOptions.executor = executor;
|
175 | });
|
176 | }
|
177 | };
|
178 | GraphQLModule = GraphQLModule_1 = tslib_1.__decorate([
|
179 | common_1.Module({
|
180 | imports: [schema_builder_module_1.GraphQLSchemaBuilderModule],
|
181 | providers: [
|
182 | graphql_factory_1.GraphQLFactory,
|
183 | metadata_scanner_1.MetadataScanner,
|
184 | services_1.ResolversExplorerService,
|
185 | services_1.ScalarsExplorerService,
|
186 | services_1.PluginsExplorerService,
|
187 | graphql_ast_explorer_1.GraphQLAstExplorer,
|
188 | graphql_types_loader_1.GraphQLTypesLoader,
|
189 | graphql_schema_builder_1.GraphQLSchemaBuilder,
|
190 | graphql_schema_host_1.GraphQLSchemaHost,
|
191 | ],
|
192 | exports: [graphql_types_loader_1.GraphQLTypesLoader, graphql_ast_explorer_1.GraphQLAstExplorer, graphql_schema_host_1.GraphQLSchemaHost],
|
193 | }),
|
194 | tslib_1.__param(1, common_1.Inject(graphql_constants_1.GRAPHQL_MODULE_OPTIONS)),
|
195 | tslib_1.__metadata("design:paramtypes", [core_1.HttpAdapterHost, Object, graphql_factory_1.GraphQLFactory,
|
196 | graphql_types_loader_1.GraphQLTypesLoader,
|
197 | core_1.ApplicationConfig])
|
198 | ], GraphQLModule);
|
199 | exports.GraphQLModule = GraphQLModule;
|