@tsed/apollo 6.88.1 → 6.89.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
File without changes
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ApolloModule = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const common_1 = require("@tsed/common");
6
+ const di_1 = require("@tsed/di");
7
+ const ApolloService_1 = require("./services/ApolloService");
8
+ let ApolloModule = class ApolloModule {
9
+ get settings() {
10
+ return this.configuration.get("apollo");
11
+ }
12
+ $onRoutesInit() {
13
+ const { settings } = this;
14
+ if (settings) {
15
+ const promises = Object.entries(settings).map(async ([key, options]) => {
16
+ return this.service.createServer(key, options);
17
+ });
18
+ return Promise.all(promises);
19
+ }
20
+ }
21
+ $afterListen() {
22
+ const host = this.configuration.getBestHost();
23
+ const displayLog = (key, path) => {
24
+ const url = typeof host.port === "number" ? `${host.protocol}://${host.address}:${host.port}` : "";
25
+ this.logger.info(`[${key}] Apollo server is available on ${url}${path.replace(/^\//, "")}`);
26
+ };
27
+ const { settings } = this;
28
+ if (settings) {
29
+ Object.entries(settings).map(async ([key, options]) => {
30
+ const { path } = options;
31
+ displayLog(key, path);
32
+ });
33
+ }
34
+ }
35
+ };
36
+ (0, tslib_1.__decorate)([
37
+ (0, di_1.Inject)(),
38
+ (0, tslib_1.__metadata)("design:type", common_1.Logger)
39
+ ], ApolloModule.prototype, "logger", void 0);
40
+ (0, tslib_1.__decorate)([
41
+ (0, di_1.Inject)(),
42
+ (0, tslib_1.__metadata)("design:type", ApolloService_1.ApolloService)
43
+ ], ApolloModule.prototype, "service", void 0);
44
+ (0, tslib_1.__decorate)([
45
+ (0, di_1.Configuration)(),
46
+ (0, tslib_1.__metadata)("design:type", Object)
47
+ ], ApolloModule.prototype, "configuration", void 0);
48
+ ApolloModule = (0, tslib_1.__decorate)([
49
+ (0, di_1.Module)()
50
+ ], ApolloModule);
51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EAAC,QAAQ,EAAC,GAAG,IAAI,CAAC;QAExB,IAAI,QAAQ,EAAE;YACZ,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,GAAG,EAAE,OAAO,CAAC,EAAE,EAAE;gBACpD,MAAM,EAAC,IAAI,EAAC,GAAG,OAAO,CAAC;gBAEvB,UAAU,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;YACxB,CAAC,CAAC,CAAC;SACJ;IACH,CAAC;CACF,CAAA;AA3CC;IADC,IAAA,WAAM,GAAE;2CACS,eAAM;4CAAC;AAGzB;IADC,IAAA,WAAM,GAAE;2CACU,6BAAa;6CAAC;AAGjC;IADC,IAAA,kBAAa,GAAE;;mDACuB;AAR5B,YAAY;IADxB,IAAA,WAAM,GAAE;GACI,YAAY,CA6CxB;AA7CY,oCAAY","sourcesContent":["import {AfterListen, Logger, OnRoutesInit} from \"@tsed/common\";\nimport {Configuration, Inject, Module} from \"@tsed/di\";\nimport {ApolloSettings} from \"./interfaces/ApolloSettings\";\nimport {ApolloService} from \"./services/ApolloService\";\n\n@Module()\nexport class ApolloModule implements OnRoutesInit, AfterListen {\n @Inject()\n protected logger: Logger;\n\n @Inject()\n protected service: ApolloService;\n\n @Configuration()\n protected configuration: Configuration;\n\n get settings(): {[key: string]: ApolloSettings} | undefined {\n return this.configuration.get(\"apollo\");\n }\n\n $onRoutesInit(): Promise<any> | void {\n const {settings} = this;\n\n if (settings) {\n const promises = Object.entries(settings).map(async ([key, options]) => {\n return this.service.createServer(key, options);\n });\n\n return Promise.all(promises);\n }\n }\n\n $afterListen(): Promise<any> | void {\n const host = this.configuration.getBestHost();\n\n const displayLog = (key: string, path: string) => {\n const url = typeof host.port === \"number\" ? `${host.protocol}://${host.address}:${host.port}` : \"\";\n\n this.logger.info(`[${key}] Apollo server is available on ${url}${path.replace(/^\\//, \"\")}`);\n };\n\n const {settings} = this;\n\n if (settings) {\n Object.entries(settings).map(async ([key, options]) => {\n const {path} = options;\n\n displayLog(key, path);\n });\n }\n }\n}\n"]}
File without changes
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ (0, tslib_1.__exportStar)(require("./interfaces/ApolloSettings"), exports);
5
+ (0, tslib_1.__exportStar)(require("./services/ApolloService"), exports);
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,2EAA4C;AAC5C,wEAAyC","sourcesContent":["export * from \"./interfaces/ApolloSettings\";\nexport * from \"./services/ApolloService\";\n"]}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=ApolloSettings.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ApolloSettings.js","sourceRoot":"","sources":["../../../src/interfaces/ApolloSettings.ts"],"names":[],"mappings":"","sourcesContent":["import type {ApolloServerBase, Config, ApolloServerPluginLandingPageGraphQLPlaygroundOptions} from \"apollo-server-core\";\n\nexport type ApolloMiddlewareOptions = Record<string, any>;\nexport type ApolloServer = ApolloServerBase & {applyMiddleware(settings: ApolloMiddlewareOptions): any};\nexport type ApolloCustomServerCB = (config: ApolloConfig) => ApolloServer;\nexport type ApolloConfig = Config;\n\nexport interface ApolloSettings extends ApolloConfig {\n // Basic options\n path: string;\n server?: ApolloCustomServerCB;\n playground?: boolean | ApolloServerPluginLandingPageGraphQLPlaygroundOptions;\n // ApplyMiddleware Options\n // See options descriptions on https://www.apollographql.com/docs/apollo-server/api/apollo-server.html\n serverRegistration?: ApolloMiddlewareOptions;\n middlewareOptions?: ApolloMiddlewareOptions;\n}\n"]}
@@ -0,0 +1,139 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ApolloService = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const di_1 = require("@tsed/di");
6
+ const logger_1 = require("@tsed/logger");
7
+ const common_1 = require("@tsed/common");
8
+ const apollo_server_core_1 = require("apollo-server-core");
9
+ let ApolloService = class ApolloService {
10
+ constructor() {
11
+ /**
12
+ *
13
+ * @type {Map<any, any>}
14
+ * @private
15
+ */
16
+ this.servers = new Map();
17
+ }
18
+ async createServer(id, settings) {
19
+ if (this.has(id)) {
20
+ return this.get(id);
21
+ }
22
+ try {
23
+ const { path, middlewareOptions = {}, server: customServer, ...config } = settings;
24
+ this.logger.info(`Create server with Apollo for: ${id}`);
25
+ this.logger.debug(`options: ${JSON.stringify({ path })}`);
26
+ const server = this.createInstance({
27
+ ...config,
28
+ plugins: this.getPlugins(settings)
29
+ }, customServer);
30
+ if (server) {
31
+ this.servers.set(id || "default", {
32
+ instance: server,
33
+ schema: settings.schema
34
+ });
35
+ await server.start();
36
+ await server.applyMiddleware({
37
+ path: settings.path,
38
+ ...middlewareOptions,
39
+ app: this.app.raw
40
+ });
41
+ return server;
42
+ }
43
+ }
44
+ catch (err) {
45
+ /* istanbul ignore next */
46
+ this.logger.error(err);
47
+ /* istanbul ignore next */
48
+ process.exit(-1);
49
+ }
50
+ }
51
+ /**
52
+ * Get an instance of ApolloServer from his id
53
+ * @returns ApolloServer
54
+ */
55
+ get(id = "default") {
56
+ var _a;
57
+ return (_a = this.servers.get(id)) === null || _a === void 0 ? void 0 : _a.instance;
58
+ }
59
+ /**
60
+ * Get an instance of GraphQL schema from his id
61
+ * @returns GraphQLSchema
62
+ */
63
+ getSchema(id = "default") {
64
+ var _a;
65
+ return (_a = this.servers.get(id)) === null || _a === void 0 ? void 0 : _a.schema;
66
+ }
67
+ /**
68
+ *
69
+ * @param {string} id
70
+ * @returns {boolean}
71
+ */
72
+ has(id = "default") {
73
+ return this.servers.has(id);
74
+ }
75
+ createInstance(options, server) {
76
+ // istanbul ignore next
77
+ if (server) {
78
+ return server(options);
79
+ }
80
+ // istanbul ignore next
81
+ try {
82
+ const Server = require(`apollo-server-${this.platformName || "express"}`).ApolloServer;
83
+ return new Server(options);
84
+ }
85
+ catch (er) {
86
+ this.logger.error(`Platform "${this.platformName}" not supported by @tsed/apollo`);
87
+ }
88
+ }
89
+ getPlugins(serverSettings) {
90
+ const playground = serverSettings.playground || serverSettings.playground === undefined;
91
+ return [
92
+ playground && process.env.NODE_ENV === "production"
93
+ ? (0, apollo_server_core_1.ApolloServerPluginLandingPageDisabled)()
94
+ : (0, apollo_server_core_1.ApolloServerPluginLandingPageGraphQLPlayground)(),
95
+ this.httpPort &&
96
+ (0, apollo_server_core_1.ApolloServerPluginDrainHttpServer)({
97
+ httpServer: this.httpServer
98
+ }),
99
+ this.httpsPort &&
100
+ (0, apollo_server_core_1.ApolloServerPluginDrainHttpServer)({
101
+ httpServer: this.httpsServer
102
+ }),
103
+ ...(serverSettings.plugins || [])
104
+ ].filter(Boolean);
105
+ }
106
+ };
107
+ (0, tslib_1.__decorate)([
108
+ (0, di_1.Constant)("PLATFORM_NAME"),
109
+ (0, tslib_1.__metadata)("design:type", String)
110
+ ], ApolloService.prototype, "platformName", void 0);
111
+ (0, tslib_1.__decorate)([
112
+ (0, di_1.Inject)(),
113
+ (0, tslib_1.__metadata)("design:type", logger_1.Logger)
114
+ ], ApolloService.prototype, "logger", void 0);
115
+ (0, tslib_1.__decorate)([
116
+ (0, di_1.Constant)("httpPort"),
117
+ (0, tslib_1.__metadata)("design:type", Object)
118
+ ], ApolloService.prototype, "httpPort", void 0);
119
+ (0, tslib_1.__decorate)([
120
+ (0, di_1.Constant)("httpsPort"),
121
+ (0, tslib_1.__metadata)("design:type", Object)
122
+ ], ApolloService.prototype, "httpsPort", void 0);
123
+ (0, tslib_1.__decorate)([
124
+ (0, di_1.Inject)(),
125
+ (0, tslib_1.__metadata)("design:type", common_1.PlatformApplication)
126
+ ], ApolloService.prototype, "app", void 0);
127
+ (0, tslib_1.__decorate)([
128
+ (0, di_1.Inject)(common_1.HttpServer),
129
+ (0, tslib_1.__metadata)("design:type", Object)
130
+ ], ApolloService.prototype, "httpServer", void 0);
131
+ (0, tslib_1.__decorate)([
132
+ (0, di_1.Inject)(common_1.HttpsServer),
133
+ (0, tslib_1.__metadata)("design:type", Object)
134
+ ], ApolloService.prototype, "httpsServer", void 0);
135
+ ApolloService = (0, tslib_1.__decorate)([
136
+ (0, di_1.Service)()
137
+ ], ApolloService);
138
+ exports.ApolloService = ApolloService;
139
+ //# sourceMappingURL=ApolloService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ApolloService.js","sourceRoot":"","sources":["../../../src/services/ApolloService.ts"],"names":[],"mappings":";;;;AAAA,iCAAmD;AACnD,yCAAoC;AACpC,yCAA0E;AAE1E,2DAK4B;AAM5B,IAAa,aAAa,GAA1B,MAAa,aAAa;IAA1B;QAOE;;;;WAIG;QACO,YAAO,GAMb,IAAI,GAAG,EAAE,CAAC;IAuHhB,CAAC;IAtGC,KAAK,CAAC,YAAY,CAAC,EAAU,EAAE,QAAwB;QACrD,IAAI,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE;YAChB,OAAO,IAAI,CAAC,GAAG,CAAC,EAAE,CAAE,CAAC;SACtB;QAED,IAAI;YACF,MAAM,EAAC,IAAI,EAAE,iBAAiB,GAAG,EAAE,EAAE,MAAM,EAAE,YAAY,EAAE,GAAG,MAAM,EAAC,GAAG,QAAQ,CAAC;YAEjF,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,kCAAkC,EAAE,EAAE,CAAC,CAAC;YACzD,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,YAAY,IAAI,CAAC,SAAS,CAAC,EAAC,IAAI,EAAC,CAAC,EAAE,CAAC,CAAC;YAExD,MAAM,MAAM,GAAG,IAAI,CAAC,cAAc,CAChC;gBACE,GAAG,MAAM;gBACT,OAAO,EAAE,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC;aACnC,EACD,YAAY,CACb,CAAC;YAEF,IAAI,MAAM,EAAE;gBACV,IAAI,CAA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port {Logger} from \"@tsed/logger\";\nimport {HttpServer, HttpsServer, PlatformApplication} from \"@tsed/common\";\nimport type {Config} from \"apollo-server-core\";\nimport {\n ApolloServerBase,\n ApolloServerPluginDrainHttpServer,\n ApolloServerPluginLandingPageDisabled,\n ApolloServerPluginLandingPageGraphQLPlayground\n} from \"apollo-server-core\";\nimport type {GraphQLSchema} from \"graphql\";\nimport type {ApolloServer, ApolloSettings} from \"../interfaces/ApolloSettings\";\nimport {ApolloCustomServerCB} from \"../interfaces/ApolloSettings\";\n\n@Service()\nexport class ApolloService {\n @Constant(\"PLATFORM_NAME\")\n platformName: string;\n\n @Inject()\n protected logger: Logger;\n\n /**\n *\n * @type {Map<any, any>}\n * @private\n */\n protected servers: Map<\n string,\n {\n instance: ApolloServerBase;\n schema: GraphQLSchema | undefined;\n }\n > = new Map();\n\n @Constant(\"httpPort\")\n private httpPort: string | number;\n\n @Constant(\"httpsPort\")\n private httpsPort: string | number;\n\n @Inject()\n private app: PlatformApplication;\n\n @Inject(HttpServer)\n private httpServer: HttpServer;\n\n @Inject(HttpsServer)\n private httpsServer: HttpsServer;\n\n async createServer(id: string, settings: ApolloSettings): Promise<any> {\n if (this.has(id)) {\n return this.get(id)!;\n }\n\n try {\n const {path, middlewareOptions = {}, server: customServer, ...config} = settings;\n\n this.logger.info(`Create server with Apollo for: ${id}`);\n this.logger.debug(`options: ${JSON.stringify({path})}`);\n\n const server = this.createInstance(\n {\n ...config,\n plugins: this.getPlugins(settings)\n },\n customServer\n );\n\n if (server) {\n this.servers.set(id || \"default\", {\n instance: server,\n schema: settings.schema\n });\n\n await server.start();\n\n await server.applyMiddleware({\n path: settings.path,\n ...middlewareOptions,\n app: this.app.raw\n });\n\n return server;\n }\n } catch (err) {\n /* istanbul ignore next */\n this.logger.error(err);\n /* istanbul ignore next */\n process.exit(-1);\n }\n }\n\n /**\n * Get an instance of ApolloServer from his id\n * @returns ApolloServer\n */\n get(id: string = \"default\"): ApolloServerBase | undefined {\n return this.servers.get(id)?.instance;\n }\n\n /**\n * Get an instance of GraphQL schema from his id\n * @returns GraphQLSchema\n */\n getSchema(id: string = \"default\"): GraphQLSchema | undefined {\n return this.servers.get(id)?.schema;\n }\n\n /**\n *\n * @param {string} id\n * @returns {boolean}\n */\n has(id: string = \"default\"): boolean {\n return this.servers.has(id);\n }\n\n protected createInstance(options: Config, server?: ApolloCustomServerCB): ApolloServer | undefined {\n // istanbul ignore next\n if (server) {\n return server(options);\n }\n\n // istanbul ignore next\n try {\n const Server = require(`apollo-server-${this.platformName || \"express\"}`).ApolloServer;\n\n return new Server(options);\n } catch (er) {\n this.logger.error(`Platform \"${this.platformName}\" not supported by @tsed/apollo`);\n }\n }\n\n private getPlugins(serverSettings: ApolloSettings): any[] {\n const playground = serverSettings.playground || serverSettings.playground === undefined;\n\n return [\n playground && process.env.NODE_ENV === \"production\"\n ? ApolloServerPluginLandingPageDisabled()\n : ApolloServerPluginLandingPageGraphQLPlayground(),\n this.httpPort &&\n ApolloServerPluginDrainHttpServer({\n httpServer: this.httpServer\n }),\n this.httpsPort &&\n ApolloServerPluginDrainHttpServer({\n httpServer: this.httpsServer\n }),\n ...(serverSettings.plugins || [])\n ].filter(Boolean);\n }\n}\n"]}
@@ -0,0 +1,49 @@
1
+ import { __decorate, __metadata } from "tslib";
2
+ import { Logger } from "@tsed/common";
3
+ import { Configuration, Inject, Module } from "@tsed/di";
4
+ import { ApolloService } from "./services/ApolloService";
5
+ let ApolloModule = class ApolloModule {
6
+ get settings() {
7
+ return this.configuration.get("apollo");
8
+ }
9
+ $onRoutesInit() {
10
+ const { settings } = this;
11
+ if (settings) {
12
+ const promises = Object.entries(settings).map(async ([key, options]) => {
13
+ return this.service.createServer(key, options);
14
+ });
15
+ return Promise.all(promises);
16
+ }
17
+ }
18
+ $afterListen() {
19
+ const host = this.configuration.getBestHost();
20
+ const displayLog = (key, path) => {
21
+ const url = typeof host.port === "number" ? `${host.protocol}://${host.address}:${host.port}` : "";
22
+ this.logger.info(`[${key}] Apollo server is available on ${url}${path.replace(/^\//, "")}`);
23
+ };
24
+ const { settings } = this;
25
+ if (settings) {
26
+ Object.entries(settings).map(async ([key, options]) => {
27
+ const { path } = options;
28
+ displayLog(key, path);
29
+ });
30
+ }
31
+ }
32
+ };
33
+ __decorate([
34
+ Inject(),
35
+ __metadata("design:type", Logger)
36
+ ], ApolloModule.prototype, "logger", void 0);
37
+ __decorate([
38
+ Inject(),
39
+ __metadata("design:type", ApolloService)
40
+ ], ApolloModule.prototype, "service", void 0);
41
+ __decorate([
42
+ Configuration(),
43
+ __metadata("design:type", Object)
44
+ ], ApolloModule.prototype, "configuration", void 0);
45
+ ApolloModule = __decorate([
46
+ Module()
47
+ ], ApolloModule);
48
+ export { ApolloModule };
49
+ //# sourceMappingURL=ApolloModule.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ApolloModule.js","sourceRoot":"","sources":["../../src/ApolloModule.ts"],"names":[],"mappings":";AAAA,OAAO,EAAc,MAAM,EAAe,MAAM,cAAc,CAAC;AAC/D,OAAO,EAAC,aAAa,EAAE,MAAM,EAAE,MAAM,EAAC,MAAM,UAAU,CAAC;AAEvD,OAAO,EAAC,aAAa,EAAC,MAAM,0BAA0B,CAAC;AAGvD,IAAa,YAAY,GAAzB,MAAa,YAAY;IAUvB,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC1C,CAAC;IAED,aAAa;QACX,MAAM,EAAC,QAAQ,EAAC,GAAG,IAAI,CAAC;QAExB,IAAI,QAAQ,EAAE;YACZ,MAAM,QAAQ,GAAG,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,GAAG,EAAE,OAAO,CAAC,EAAE,EAAE;gBACrE,OAAO,IAAI,CAAC,OAAO,CAAC,YAAY,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;YACjD,CAAC,CAAC,CAAC;YAEH,OAAO,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;SAC9B;IACH,CAAC;IAED,YAAY;QACV,MAAM,IAAI,GAAG,IAAI,CAAC,aAAa,CAAC,WAAW,EAAE,CAAC;QAE9C,MAAM,UAAU,GAAG,CAAC,GAAW,EAAE,IAAY,EAAE,EAAE;YAC/C,MAAM,GAAG,GAAG,OAAO,IAAI,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,QAAQ,MAAM,IAAI,CAAC,OAAO,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;YAEnG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,GAAG,mCAAmC,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC;QAC9F,CAAC,CAAC;QAEF,MAAM,EAAC,QAAQ,EAAC,GAAG,IAAI,CAAC;QAExB,IAAI,QAAQ,EAAE;YACZ,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,GAAG,EAAE,OAAO,CAAC,EAAE,EAAE;gBACpD,MAAM,EAAC,IAAI,EAAC,GAAG,OAAO,CAAC;gBAEvB,UAAU,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;YACxB,CAAC,CAAC,CAAC;SACJ;IACH,CAAC;CACF,CAAA;AA3CC;IADC,MAAM,EAAE;8BACS,MAAM;4CAAC;AAGzB;IADC,MAAM,EAAE;8BACU,aAAa;6CAAC;AAGjC;IADC,aAAa,EAAE;;mDACuB;AAR5B,YAAY;IADxB,MAAM,EAAE;GACI,YAAY,CA6CxB;SA7CY,YAAY","sourcesContent":["import {AfterListen, Logger, OnRoutesInit} from \"@tsed/common\";\nimport {Configuration, Inject, Module} from \"@tsed/di\";\nimport {ApolloSettings} from \"./interfaces/ApolloSettings\";\nimport {ApolloService} from \"./services/ApolloService\";\n\n@Module()\nexport class ApolloModule implements OnRoutesInit, AfterListen {\n @Inject()\n protected logger: Logger;\n\n @Inject()\n protected service: ApolloService;\n\n @Configuration()\n protected configuration: Configuration;\n\n get settings(): {[key: string]: ApolloSettings} | undefined {\n return this.configuration.get(\"apollo\");\n }\n\n $onRoutesInit(): Promise<any> | void {\n const {settings} = this;\n\n if (settings) {\n const promises = Object.entries(settings).map(async ([key, options]) => {\n return this.service.createServer(key, options);\n });\n\n return Promise.all(promises);\n }\n }\n\n $afterListen(): Promise<any> | void {\n const host = this.configuration.getBestHost();\n\n const displayLog = (key: string, path: string) => {\n const url = typeof host.port === \"number\" ? `${host.protocol}://${host.address}:${host.port}` : \"\";\n\n this.logger.info(`[${key}] Apollo server is available on ${url}${path.replace(/^\\//, \"\")}`);\n };\n\n const {settings} = this;\n\n if (settings) {\n Object.entries(settings).map(async ([key, options]) => {\n const {path} = options;\n\n displayLog(key, path);\n });\n }\n }\n}\n"]}
@@ -0,0 +1,3 @@
1
+ export * from "./interfaces/ApolloSettings";
2
+ export * from "./services/ApolloService";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,6BAA6B,CAAC;AAC5C,cAAc,0BAA0B,CAAC","sourcesContent":["export * from \"./interfaces/ApolloSettings\";\nexport * from \"./services/ApolloService\";\n"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=ApolloSettings.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ApolloSettings.js","sourceRoot":"","sources":["../../../src/interfaces/ApolloSettings.ts"],"names":[],"mappings":"","sourcesContent":["import type {ApolloServerBase, Config, ApolloServerPluginLandingPageGraphQLPlaygroundOptions} from \"apollo-server-core\";\n\nexport type ApolloMiddlewareOptions = Record<string, any>;\nexport type ApolloServer = ApolloServerBase & {applyMiddleware(settings: ApolloMiddlewareOptions): any};\nexport type ApolloCustomServerCB = (config: ApolloConfig) => ApolloServer;\nexport type ApolloConfig = Config;\n\nexport interface ApolloSettings extends ApolloConfig {\n // Basic options\n path: string;\n server?: ApolloCustomServerCB;\n playground?: boolean | ApolloServerPluginLandingPageGraphQLPlaygroundOptions;\n // ApplyMiddleware Options\n // See options descriptions on https://www.apollographql.com/docs/apollo-server/api/apollo-server.html\n serverRegistration?: ApolloMiddlewareOptions;\n middlewareOptions?: ApolloMiddlewareOptions;\n}\n"]}
@@ -0,0 +1,136 @@
1
+ import { __decorate, __metadata } from "tslib";
2
+ import { Constant, Inject, Service } from "@tsed/di";
3
+ import { Logger } from "@tsed/logger";
4
+ import { HttpServer, HttpsServer, PlatformApplication } from "@tsed/common";
5
+ import { ApolloServerPluginDrainHttpServer, ApolloServerPluginLandingPageDisabled, ApolloServerPluginLandingPageGraphQLPlayground } from "apollo-server-core";
6
+ let ApolloService = class ApolloService {
7
+ constructor() {
8
+ /**
9
+ *
10
+ * @type {Map<any, any>}
11
+ * @private
12
+ */
13
+ this.servers = new Map();
14
+ }
15
+ async createServer(id, settings) {
16
+ if (this.has(id)) {
17
+ return this.get(id);
18
+ }
19
+ try {
20
+ const { path, middlewareOptions = {}, server: customServer, ...config } = settings;
21
+ this.logger.info(`Create server with Apollo for: ${id}`);
22
+ this.logger.debug(`options: ${JSON.stringify({ path })}`);
23
+ const server = this.createInstance({
24
+ ...config,
25
+ plugins: this.getPlugins(settings)
26
+ }, customServer);
27
+ if (server) {
28
+ this.servers.set(id || "default", {
29
+ instance: server,
30
+ schema: settings.schema
31
+ });
32
+ await server.start();
33
+ await server.applyMiddleware({
34
+ path: settings.path,
35
+ ...middlewareOptions,
36
+ app: this.app.raw
37
+ });
38
+ return server;
39
+ }
40
+ }
41
+ catch (err) {
42
+ /* istanbul ignore next */
43
+ this.logger.error(err);
44
+ /* istanbul ignore next */
45
+ process.exit(-1);
46
+ }
47
+ }
48
+ /**
49
+ * Get an instance of ApolloServer from his id
50
+ * @returns ApolloServer
51
+ */
52
+ get(id = "default") {
53
+ var _a;
54
+ return (_a = this.servers.get(id)) === null || _a === void 0 ? void 0 : _a.instance;
55
+ }
56
+ /**
57
+ * Get an instance of GraphQL schema from his id
58
+ * @returns GraphQLSchema
59
+ */
60
+ getSchema(id = "default") {
61
+ var _a;
62
+ return (_a = this.servers.get(id)) === null || _a === void 0 ? void 0 : _a.schema;
63
+ }
64
+ /**
65
+ *
66
+ * @param {string} id
67
+ * @returns {boolean}
68
+ */
69
+ has(id = "default") {
70
+ return this.servers.has(id);
71
+ }
72
+ createInstance(options, server) {
73
+ // istanbul ignore next
74
+ if (server) {
75
+ return server(options);
76
+ }
77
+ // istanbul ignore next
78
+ try {
79
+ const Server = require(`apollo-server-${this.platformName || "express"}`).ApolloServer;
80
+ return new Server(options);
81
+ }
82
+ catch (er) {
83
+ this.logger.error(`Platform "${this.platformName}" not supported by @tsed/apollo`);
84
+ }
85
+ }
86
+ getPlugins(serverSettings) {
87
+ const playground = serverSettings.playground || serverSettings.playground === undefined;
88
+ return [
89
+ playground && process.env.NODE_ENV === "production"
90
+ ? ApolloServerPluginLandingPageDisabled()
91
+ : ApolloServerPluginLandingPageGraphQLPlayground(),
92
+ this.httpPort &&
93
+ ApolloServerPluginDrainHttpServer({
94
+ httpServer: this.httpServer
95
+ }),
96
+ this.httpsPort &&
97
+ ApolloServerPluginDrainHttpServer({
98
+ httpServer: this.httpsServer
99
+ }),
100
+ ...(serverSettings.plugins || [])
101
+ ].filter(Boolean);
102
+ }
103
+ };
104
+ __decorate([
105
+ Constant("PLATFORM_NAME"),
106
+ __metadata("design:type", String)
107
+ ], ApolloService.prototype, "platformName", void 0);
108
+ __decorate([
109
+ Inject(),
110
+ __metadata("design:type", Logger)
111
+ ], ApolloService.prototype, "logger", void 0);
112
+ __decorate([
113
+ Constant("httpPort"),
114
+ __metadata("design:type", Object)
115
+ ], ApolloService.prototype, "httpPort", void 0);
116
+ __decorate([
117
+ Constant("httpsPort"),
118
+ __metadata("design:type", Object)
119
+ ], ApolloService.prototype, "httpsPort", void 0);
120
+ __decorate([
121
+ Inject(),
122
+ __metadata("design:type", PlatformApplication)
123
+ ], ApolloService.prototype, "app", void 0);
124
+ __decorate([
125
+ Inject(HttpServer),
126
+ __metadata("design:type", Object)
127
+ ], ApolloService.prototype, "httpServer", void 0);
128
+ __decorate([
129
+ Inject(HttpsServer),
130
+ __metadata("design:type", Object)
131
+ ], ApolloService.prototype, "httpsServer", void 0);
132
+ ApolloService = __decorate([
133
+ Service()
134
+ ], ApolloService);
135
+ export { ApolloService };
136
+ //# sourceMappingURL=ApolloService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ApolloService.js","sourceRoot":"","sources":["../../../src/services/ApolloService.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAC,MAAM,UAAU,CAAC;AACnD,OAAO,EAAC,MAAM,EAAC,MAAM,cAAc,CAAC;AACpC,OAAO,EAAC,UAAU,EAAE,WAAW,EAAE,mBAAmB,EAAC,MAAM,cAAc,CAAC;AAE1E,OAAO,EAEL,iCAAiC,EACjC,qCAAqC,EACrC,8CAA8C,EAC/C,MAAM,oBAAoB,CAAC;AAM5B,IAAa,aAAa,GAA1B,MAAa,aAAa;IAA1B;QAOE;;;;WAIG;QACO,YAAO,GAMb,IAAI,GAAG,EAAE,CAAC;IAuHhB,CAAC;IAtGC,KAAK,CAAC,YAAY,CAAC,EAAU,EAAE,QAAwB;QACrD,IAAI,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE;YAChB,OAAO,IAAI,CAAC,GAAG,CAAC,EAAE,CAAE,CAAC;SACtB;QAED,IAAI;YACF,MAAM,EAAC,IAAI,EAAE,iBAAiB,GAAG,EAAE,EAAE,MAAM,EAAE,YAAY,EAAE,GAAG,MAAM,EAAC,GAAG,QAAQ,CAAC;YAEjF,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,kCAAkC,EAAE,EAAE,CAAC,CAAC;YACzD,IAAI,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8BACI,mBAAmB;0CAAC;AAGjC;IADC,MAAM,CAAC,UAAU,CAAC;;iDACY;AAG/B;IADC,MAAM,CAAC,WAAW,CAAC;;kDACa;AAjCtB,aAAa;IADzB,OAAO,EAAE;GACG,aAAa,CAyIzB;SAzIY,aAAa","sourcesContent":["import {Constant, Inject, Service} from \"@tsed/di\";\nimport {Logger} from \"@tsed/logger\";\nimport {HttpServer, HttpsServer, PlatformApplication} from \"@tsed/common\";\nimport type {Config} from \"apollo-server-core\";\nimport {\n ApolloServerBase,\n ApolloServerPluginDrainHttpServer,\n ApolloServerPluginLandingPageDisabled,\n ApolloServerPluginLandingPageGraphQLPlayground\n} from \"apollo-server-core\";\nimport type {GraphQLSchema} from \"graphql\";\nimport type {ApolloServer, ApolloSettings} from \"../interfaces/ApolloSettings\";\nimport {ApolloCustomServerCB} from \"../interfaces/ApolloSettings\";\n\n@Service()\nexport class ApolloService {\n @Constant(\"PLATFORM_NAME\")\n platformName: string;\n\n @Inject()\n protected logger: Logger;\n\n /**\n *\n * @type {Map<any, any>}\n * @private\n */\n protected servers: Map<\n string,\n {\n instance: ApolloServerBase;\n schema: GraphQLSchema | undefined;\n }\n > = new Map();\n\n @Constant(\"httpPort\")\n private httpPort: string | number;\n\n @Constant(\"httpsPort\")\n private httpsPort: string | number;\n\n @Inject()\n private app: PlatformApplication;\n\n @Inject(HttpServer)\n private httpServer: HttpServer;\n\n @Inject(HttpsServer)\n private httpsServer: HttpsServer;\n\n async createServer(id: string, settings: ApolloSettings): Promise<any> {\n if (this.has(id)) {\n return this.get(id)!;\n }\n\n try {\n const {path, middlewareOptions = {}, server: customServer, ...config} = settings;\n\n this.logger.info(`Create server with Apollo for: ${id}`);\n this.logger.debug(`options: ${JSON.stringify({path})}`);\n\n const server = this.createInstance(\n {\n ...config,\n plugins: this.getPlugins(settings)\n },\n customServer\n );\n\n if (server) {\n this.servers.set(id || \"default\", {\n instance: server,\n schema: settings.schema\n });\n\n await server.start();\n\n await server.applyMiddleware({\n path: settings.path,\n ...middlewareOptions,\n app: this.app.raw\n });\n\n return server;\n }\n } catch (err) {\n /* istanbul ignore next */\n this.logger.error(err);\n /* istanbul ignore next */\n process.exit(-1);\n }\n }\n\n /**\n * Get an instance of ApolloServer from his id\n * @returns ApolloServer\n */\n get(id: string = \"default\"): ApolloServerBase | undefined {\n return this.servers.get(id)?.instance;\n }\n\n /**\n * Get an instance of GraphQL schema from his id\n * @returns GraphQLSchema\n */\n getSchema(id: string = \"default\"): GraphQLSchema | undefined {\n return this.servers.get(id)?.schema;\n }\n\n /**\n *\n * @param {string} id\n * @returns {boolean}\n */\n has(id: string = \"default\"): boolean {\n return this.servers.has(id);\n }\n\n protected createInstance(options: Config, server?: ApolloCustomServerCB): ApolloServer | undefined {\n // istanbul ignore next\n if (server) {\n return server(options);\n }\n\n // istanbul ignore next\n try {\n const Server = require(`apollo-server-${this.platformName || \"express\"}`).ApolloServer;\n\n return new Server(options);\n } catch (er) {\n this.logger.error(`Platform \"${this.platformName}\" not supported by @tsed/apollo`);\n }\n }\n\n private getPlugins(serverSettings: ApolloSettings): any[] {\n const playground = serverSettings.playground || serverSettings.playground === undefined;\n\n return [\n playground && process.env.NODE_ENV === \"production\"\n ? ApolloServerPluginLandingPageDisabled()\n : ApolloServerPluginLandingPageGraphQLPlayground(),\n this.httpPort &&\n ApolloServerPluginDrainHttpServer({\n httpServer: this.httpServer\n }),\n this.httpsPort &&\n ApolloServerPluginDrainHttpServer({\n httpServer: this.httpsServer\n }),\n ...(serverSettings.plugins || [])\n ].filter(Boolean);\n }\n}\n"]}
@@ -0,0 +1,14 @@
1
+ import { AfterListen, Logger, OnRoutesInit } from "@tsed/common";
2
+ import { Configuration } from "@tsed/di";
3
+ import { ApolloSettings } from "./interfaces/ApolloSettings";
4
+ import { ApolloService } from "./services/ApolloService";
5
+ export declare class ApolloModule implements OnRoutesInit, AfterListen {
6
+ protected logger: Logger;
7
+ protected service: ApolloService;
8
+ protected configuration: Configuration;
9
+ get settings(): {
10
+ [key: string]: ApolloSettings;
11
+ } | undefined;
12
+ $onRoutesInit(): Promise<any> | void;
13
+ $afterListen(): Promise<any> | void;
14
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./interfaces/ApolloSettings";
2
+ export * from "./services/ApolloService";
@@ -0,0 +1,14 @@
1
+ import type { ApolloServerBase, Config, ApolloServerPluginLandingPageGraphQLPlaygroundOptions } from "apollo-server-core";
2
+ export declare type ApolloMiddlewareOptions = Record<string, any>;
3
+ export declare type ApolloServer = ApolloServerBase & {
4
+ applyMiddleware(settings: ApolloMiddlewareOptions): any;
5
+ };
6
+ export declare type ApolloCustomServerCB = (config: ApolloConfig) => ApolloServer;
7
+ export declare type ApolloConfig = Config;
8
+ export interface ApolloSettings extends ApolloConfig {
9
+ path: string;
10
+ server?: ApolloCustomServerCB;
11
+ playground?: boolean | ApolloServerPluginLandingPageGraphQLPlaygroundOptions;
12
+ serverRegistration?: ApolloMiddlewareOptions;
13
+ middlewareOptions?: ApolloMiddlewareOptions;
14
+ }
@@ -0,0 +1,43 @@
1
+ import { Logger } from "@tsed/logger";
2
+ import type { Config } from "apollo-server-core";
3
+ import { ApolloServerBase } from "apollo-server-core";
4
+ import type { GraphQLSchema } from "graphql";
5
+ import type { ApolloServer, ApolloSettings } from "../interfaces/ApolloSettings";
6
+ import { ApolloCustomServerCB } from "../interfaces/ApolloSettings";
7
+ export declare class ApolloService {
8
+ platformName: string;
9
+ protected logger: Logger;
10
+ /**
11
+ *
12
+ * @type {Map<any, any>}
13
+ * @private
14
+ */
15
+ protected servers: Map<string, {
16
+ instance: ApolloServerBase;
17
+ schema: GraphQLSchema | undefined;
18
+ }>;
19
+ private httpPort;
20
+ private httpsPort;
21
+ private app;
22
+ private httpServer;
23
+ private httpsServer;
24
+ createServer(id: string, settings: ApolloSettings): Promise<any>;
25
+ /**
26
+ * Get an instance of ApolloServer from his id
27
+ * @returns ApolloServer
28
+ */
29
+ get(id?: string): ApolloServerBase | undefined;
30
+ /**
31
+ * Get an instance of GraphQL schema from his id
32
+ * @returns GraphQLSchema
33
+ */
34
+ getSchema(id?: string): GraphQLSchema | undefined;
35
+ /**
36
+ *
37
+ * @param {string} id
38
+ * @returns {boolean}
39
+ */
40
+ has(id?: string): boolean;
41
+ protected createInstance(options: Config, server?: ApolloCustomServerCB): ApolloServer | undefined;
42
+ private getPlugins;
43
+ }
package/package.json CHANGED
@@ -1,16 +1,20 @@
1
1
  {
2
2
  "name": "@tsed/apollo",
3
- "version": "6.88.1",
3
+ "version": "6.89.0",
4
4
  "description": "Apollo package for Ts.ED framework",
5
5
  "source": "./src/index.ts",
6
- "main": "./lib/index.js",
7
- "typings": "./lib/index.d.ts",
6
+ "main": "./lib/cjs/index.js",
7
+ "typings": "./lib/types/index.d.ts",
8
8
  "exports": {
9
- "require": "./lib/index.js",
10
- "default": "./lib/index.modern.js"
9
+ "types": "./lib/types/index.d.ts",
10
+ "import": "./lib/esm/index.js",
11
+ "require": "./lib/cjs/index.js",
12
+ "default": "./lib/esm/index.js"
11
13
  },
12
14
  "scripts": {
13
- "build": "microbundle --target node --no-compress --format modern,cjs --tsconfig ./tsconfig.compile.json",
15
+ "build": "yarn run build:esm && yarn run build:cjs",
16
+ "build:cjs": "tsc --build tsconfig.compile.json",
17
+ "build:esm": "tsc --build tsconfig.compile.esm.json",
14
18
  "test": "cross-env NODE_ENV=test jest"
15
19
  },
16
20
  "dependencies": {
@@ -18,9 +22,9 @@
18
22
  },
19
23
  "private": false,
20
24
  "devDependencies": {
21
- "@tsed/common": "6.88.1",
22
- "@tsed/core": "6.88.1",
23
- "@tsed/di": "6.88.1",
25
+ "@tsed/common": "6.89.0",
26
+ "@tsed/core": "6.89.0",
27
+ "@tsed/di": "6.89.0",
24
28
  "@tsed/logger": "^6.0.0",
25
29
  "@types/graphql": "14.5.0",
26
30
  "apollo-datasource": "^3.2.0",
@@ -32,9 +36,9 @@
32
36
  "graphql": "15.7.2"
33
37
  },
34
38
  "peerDependencies": {
35
- "@tsed/common": "^6.88.1",
36
- "@tsed/core": "^6.88.1",
37
- "@tsed/di": "^6.88.1",
39
+ "@tsed/common": "^6.89.0",
40
+ "@tsed/core": "^6.89.0",
41
+ "@tsed/di": "^6.89.0",
38
42
  "@tsed/logger": ">6.0.0",
39
43
  "apollo-datasource": ">=3.0.0",
40
44
  "apollo-datasource-rest": ">=3.0.0",
@@ -0,0 +1,23 @@
1
+ {
2
+ "extends": "./tsconfig.compile.json",
3
+ "compilerOptions": {
4
+ "baseUrl": ".",
5
+ "module": "ES2020",
6
+ "rootDir": "src",
7
+ "outDir": "./lib/esm",
8
+ "declaration": true,
9
+ "declarationDir": "./lib/types"
10
+ },
11
+ "exclude": [
12
+ "node_modules",
13
+ "test",
14
+ "lib",
15
+ "benchmark",
16
+ "coverage",
17
+ "spec",
18
+ "**/*.benchmark.ts",
19
+ "**/*.spec.ts",
20
+ "keys",
21
+ "jest.config.js"
22
+ ]
23
+ }
package/lib/index.js DELETED
@@ -1,168 +0,0 @@
1
- var tslib = require('tslib');
2
- var di = require('@tsed/di');
3
- var logger = require('@tsed/logger');
4
- var common = require('@tsed/common');
5
- var apolloServerCore = require('apollo-server-core');
6
-
7
- function _extends() {
8
- _extends = Object.assign || function (target) {
9
- for (var i = 1; i < arguments.length; i++) {
10
- var source = arguments[i];
11
-
12
- for (var key in source) {
13
- if (Object.prototype.hasOwnProperty.call(source, key)) {
14
- target[key] = source[key];
15
- }
16
- }
17
- }
18
-
19
- return target;
20
- };
21
-
22
- return _extends.apply(this, arguments);
23
- }
24
-
25
- function _objectWithoutPropertiesLoose(source, excluded) {
26
- if (source == null) return {};
27
- var target = {};
28
- var sourceKeys = Object.keys(source);
29
- var key, i;
30
-
31
- for (i = 0; i < sourceKeys.length; i++) {
32
- key = sourceKeys[i];
33
- if (excluded.indexOf(key) >= 0) continue;
34
- target[key] = source[key];
35
- }
36
-
37
- return target;
38
- }
39
-
40
- const _excluded = ["path", "middlewareOptions", "server"];
41
- exports.ApolloService = class ApolloService {
42
- constructor() {
43
- /**
44
- *
45
- * @type {Map<any, any>}
46
- * @private
47
- */
48
- this.servers = new Map();
49
- }
50
-
51
- async createServer(id, settings) {
52
- if (this.has(id)) {
53
- return this.get(id);
54
- }
55
-
56
- try {
57
- const {
58
- path,
59
- middlewareOptions = {},
60
- server: customServer
61
- } = settings,
62
- config = _objectWithoutPropertiesLoose(settings, _excluded);
63
-
64
- this.logger.info(`Create server with Apollo for: ${id}`);
65
- this.logger.debug(`options: ${JSON.stringify({
66
- path
67
- })}`);
68
- const server = this.createInstance(_extends({}, config, {
69
- plugins: this.getPlugins(settings)
70
- }), customServer);
71
-
72
- if (server) {
73
- this.servers.set(id || "default", {
74
- instance: server,
75
- schema: settings.schema
76
- });
77
- await server.start();
78
- await server.applyMiddleware(_extends({
79
- path: settings.path
80
- }, middlewareOptions, {
81
- app: this.app.raw
82
- }));
83
- return server;
84
- }
85
- } catch (err) {
86
- /* istanbul ignore next */
87
- this.logger.error(err);
88
- /* istanbul ignore next */
89
-
90
- process.exit(-1);
91
- }
92
- }
93
- /**
94
- * Get an instance of ApolloServer from his id
95
- * @returns ApolloServer
96
- */
97
-
98
-
99
- get(id = "default") {
100
- var _this$servers$get;
101
-
102
- return (_this$servers$get = this.servers.get(id)) == null ? void 0 : _this$servers$get.instance;
103
- }
104
- /**
105
- * Get an instance of GraphQL schema from his id
106
- * @returns GraphQLSchema
107
- */
108
-
109
-
110
- getSchema(id = "default") {
111
- var _this$servers$get2;
112
-
113
- return (_this$servers$get2 = this.servers.get(id)) == null ? void 0 : _this$servers$get2.schema;
114
- }
115
- /**
116
- *
117
- * @param {string} id
118
- * @returns {boolean}
119
- */
120
-
121
-
122
- has(id = "default") {
123
- return this.servers.has(id);
124
- }
125
-
126
- createInstance(options, server) {
127
- // istanbul ignore next
128
- if (server) {
129
- return server(options);
130
- } // istanbul ignore next
131
-
132
-
133
- try {
134
- const Server = require(`apollo-server-${this.platformName || "express"}`).ApolloServer;
135
-
136
- return new Server(options);
137
- } catch (er) {
138
- this.logger.error(`Platform "${this.platformName}" not supported by @tsed/apollo`);
139
- }
140
- }
141
-
142
- getPlugins(serverSettings) {
143
- const playground = serverSettings.playground || serverSettings.playground === undefined;
144
- return [playground && process.env.NODE_ENV === "production" ? apolloServerCore.ApolloServerPluginLandingPageDisabled() : apolloServerCore.ApolloServerPluginLandingPageGraphQLPlayground(), this.httpPort && apolloServerCore.ApolloServerPluginDrainHttpServer({
145
- httpServer: this.httpServer
146
- }), this.httpsPort && apolloServerCore.ApolloServerPluginDrainHttpServer({
147
- httpServer: this.httpsServer
148
- }), ...(serverSettings.plugins || [])].filter(Boolean);
149
- }
150
-
151
- };
152
-
153
- tslib.__decorate([di.Constant("PLATFORM_NAME"), tslib.__metadata("design:type", String)], exports.ApolloService.prototype, "platformName", void 0);
154
-
155
- tslib.__decorate([di.Inject(), tslib.__metadata("design:type", logger.Logger)], exports.ApolloService.prototype, "logger", void 0);
156
-
157
- tslib.__decorate([di.Constant("httpPort"), tslib.__metadata("design:type", Object)], exports.ApolloService.prototype, "httpPort", void 0);
158
-
159
- tslib.__decorate([di.Constant("httpsPort"), tslib.__metadata("design:type", Object)], exports.ApolloService.prototype, "httpsPort", void 0);
160
-
161
- tslib.__decorate([di.Inject(), tslib.__metadata("design:type", common.PlatformApplication)], exports.ApolloService.prototype, "app", void 0);
162
-
163
- tslib.__decorate([di.Inject(common.HttpServer), tslib.__metadata("design:type", Object)], exports.ApolloService.prototype, "httpServer", void 0);
164
-
165
- tslib.__decorate([di.Inject(common.HttpsServer), tslib.__metadata("design:type", Object)], exports.ApolloService.prototype, "httpsServer", void 0);
166
-
167
- exports.ApolloService = tslib.__decorate([di.Service()], exports.ApolloService);
168
- //# sourceMappingURL=index.js.map
package/lib/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sources":["../src/services/ApolloService.ts"],"sourcesContent":["import {Constant, Inject, Service} from \"@tsed/di\";\nimport {Logger} from \"@tsed/logger\";\nimport {HttpServer, HttpsServer, PlatformApplication} from \"@tsed/common\";\nimport type {Config} from \"apollo-server-core\";\nimport {\n ApolloServerBase,\n ApolloServerPluginDrainHttpServer,\n ApolloServerPluginLandingPageDisabled,\n ApolloServerPluginLandingPageGraphQLPlayground\n} from \"apollo-server-core\";\nimport type {GraphQLSchema} from \"graphql\";\nimport type {ApolloServer, ApolloSettings} from \"../interfaces/ApolloSettings\";\nimport {ApolloCustomServerCB} from \"../interfaces/ApolloSettings\";\n\n@Service()\nexport class ApolloService {\n @Constant(\"PLATFORM_NAME\")\n platformName: string;\n\n @Inject()\n protected logger: Logger;\n\n /**\n *\n * @type {Map<any, any>}\n * @private\n */\n protected servers: Map<\n string,\n {\n instance: ApolloServerBase;\n schema: GraphQLSchema | undefined;\n }\n > = new Map();\n\n @Constant(\"httpPort\")\n private httpPort: string | number;\n\n @Constant(\"httpsPort\")\n private httpsPort: string | number;\n\n @Inject()\n private app: PlatformApplication;\n\n @Inject(HttpServer)\n private httpServer: HttpServer;\n\n @Inject(HttpsServer)\n private httpsServer: HttpsServer;\n\n async createServer(id: string, settings: ApolloSettings): Promise<any> {\n if (this.has(id)) {\n return this.get(id)!;\n }\n\n try {\n const {path, middlewareOptions = {}, server: customServer, ...config} = settings;\n\n this.logger.info(`Create server with Apollo for: ${id}`);\n this.logger.debug(`options: ${JSON.stringify({path})}`);\n\n const server = this.createInstance(\n {\n ...config,\n plugins: this.getPlugins(settings)\n },\n customServer\n );\n\n if (server) {\n this.servers.set(id || \"default\", {\n instance: server,\n schema: settings.schema\n });\n\n await server.start();\n\n await server.applyMiddleware({\n path: settings.path,\n ...middlewareOptions,\n app: this.app.raw\n });\n\n return server;\n }\n } catch (err) {\n /* istanbul ignore next */\n this.logger.error(err);\n /* istanbul ignore next */\n process.exit(-1);\n }\n }\n\n /**\n * Get an instance of ApolloServer from his id\n * @returns ApolloServer\n */\n get(id: string = \"default\"): ApolloServerBase | undefined {\n return this.servers.get(id)?.instance;\n }\n\n /**\n * Get an instance of GraphQL schema from his id\n * @returns GraphQLSchema\n */\n getSchema(id: string = \"default\"): GraphQLSchema | undefined {\n return this.servers.get(id)?.schema;\n }\n\n /**\n *\n * @param {string} id\n * @returns {boolean}\n */\n has(id: string = \"default\"): boolean {\n return this.servers.has(id);\n }\n\n protected createInstance(options: Config, server?: ApolloCustomServerCB): ApolloServer | undefined {\n // istanbul ignore next\n if (server) {\n return server(options);\n }\n\n // istanbul ignore next\n try {\n const Server = require(`apollo-server-${this.platformName || \"express\"}`).ApolloServer;\n\n return new Server(options);\n } catch (er) {\n this.logger.error(`Platform \"${this.platformName}\" not supported by @tsed/apollo`);\n }\n }\n\n private getPlugins(serverSettings: ApolloSettings): any[] {\n const playground = serverSettings.playground || serverSettings.playground === undefined;\n\n return [\n playground && process.env.NODE_ENV === \"production\"\n ? ApolloServerPluginLandingPageDisabled()\n : ApolloServerPluginLandingPageGraphQLPlayground(),\n this.httpPort &&\n ApolloServerPluginDrainHttpServer({\n httpServer: this.httpServer\n }),\n this.httpsPort &&\n ApolloServerPluginDrainHttpServer({\n httpServer: this.httpsServer\n }),\n ...(serverSettings.plugins || [])\n ].filter(Boolean);\n }\n}\n"],"names":["ApolloService","constructor","Map","createServer","id","settings","has","get","path","middlewareOptions","server","customServer","config","logger","info","debug","JSON","stringify","createInstance","plugins","getPlugins","servers","set","instance","schema","start","applyMiddleware","app","raw","err","error","process","exit","getSchema","options","Server","require","platformName","ApolloServer","er","serverSettings","playground","undefined","env","NODE_ENV","ApolloServerPluginLandingPageDisabled","ApolloServerPluginLandingPageGraphQLPlayground","httpPort","ApolloServerPluginDrainHttpServer","httpServer","httpsPort","httpsServer","filter","Boolean","__decorate","Constant","Inject","Logger","PlatformApplication","HttpServer","HttpsServer","Service"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAeaA,qBAAa,GAA1B,MAAaA,aAAb;AAAAC,EAAAA;AAOE;;;;;AAKU,gBAAA,GAMN,IAAIC,GAAJ,EANM;AA6HX;;AAt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IDC,kBADCC,WAAQ,CAAC,eAAD,qGACT;;AAGAD,kBADCE,SAAM,oCACWC,mEAAlB;;AAgBAH,kBADCC,WAAQ,CAAC,UAAD,iGACT;;AAGAD,kBADCC,WAAQ,CAAC,WAAD,kGACT;;AAGAD,kBADCE,SAAM,oCACME,6EAAb;;AAGAJ,kBADCE,SAAM,CAACG,iBAAD,mGACP;;AAGAL,kBADCE,SAAM,CAACI,kBAAD,oGACP;;AAjCW5D,qBAAa,qBADzB6D,UAAO,KACK7D,sBAAA;;"}
@@ -1,170 +0,0 @@
1
- import { __decorate, __metadata } from 'tslib';
2
- import { Constant, Inject, Service } from '@tsed/di';
3
- import { Logger } from '@tsed/logger';
4
- import { PlatformApplication, HttpServer, HttpsServer } from '@tsed/common';
5
- import { ApolloServerPluginLandingPageDisabled, ApolloServerPluginLandingPageGraphQLPlayground, ApolloServerPluginDrainHttpServer } from 'apollo-server-core';
6
-
7
- function _extends() {
8
- _extends = Object.assign || function (target) {
9
- for (var i = 1; i < arguments.length; i++) {
10
- var source = arguments[i];
11
-
12
- for (var key in source) {
13
- if (Object.prototype.hasOwnProperty.call(source, key)) {
14
- target[key] = source[key];
15
- }
16
- }
17
- }
18
-
19
- return target;
20
- };
21
-
22
- return _extends.apply(this, arguments);
23
- }
24
-
25
- function _objectWithoutPropertiesLoose(source, excluded) {
26
- if (source == null) return {};
27
- var target = {};
28
- var sourceKeys = Object.keys(source);
29
- var key, i;
30
-
31
- for (i = 0; i < sourceKeys.length; i++) {
32
- key = sourceKeys[i];
33
- if (excluded.indexOf(key) >= 0) continue;
34
- target[key] = source[key];
35
- }
36
-
37
- return target;
38
- }
39
-
40
- const _excluded = ["path", "middlewareOptions", "server"];
41
- let ApolloService = class ApolloService {
42
- constructor() {
43
- /**
44
- *
45
- * @type {Map<any, any>}
46
- * @private
47
- */
48
- this.servers = new Map();
49
- }
50
-
51
- async createServer(id, settings) {
52
- if (this.has(id)) {
53
- return this.get(id);
54
- }
55
-
56
- try {
57
- const {
58
- path,
59
- middlewareOptions = {},
60
- server: customServer
61
- } = settings,
62
- config = _objectWithoutPropertiesLoose(settings, _excluded);
63
-
64
- this.logger.info(`Create server with Apollo for: ${id}`);
65
- this.logger.debug(`options: ${JSON.stringify({
66
- path
67
- })}`);
68
- const server = this.createInstance(_extends({}, config, {
69
- plugins: this.getPlugins(settings)
70
- }), customServer);
71
-
72
- if (server) {
73
- this.servers.set(id || "default", {
74
- instance: server,
75
- schema: settings.schema
76
- });
77
- await server.start();
78
- await server.applyMiddleware(_extends({
79
- path: settings.path
80
- }, middlewareOptions, {
81
- app: this.app.raw
82
- }));
83
- return server;
84
- }
85
- } catch (err) {
86
- /* istanbul ignore next */
87
- this.logger.error(err);
88
- /* istanbul ignore next */
89
-
90
- process.exit(-1);
91
- }
92
- }
93
- /**
94
- * Get an instance of ApolloServer from his id
95
- * @returns ApolloServer
96
- */
97
-
98
-
99
- get(id = "default") {
100
- var _this$servers$get;
101
-
102
- return (_this$servers$get = this.servers.get(id)) == null ? void 0 : _this$servers$get.instance;
103
- }
104
- /**
105
- * Get an instance of GraphQL schema from his id
106
- * @returns GraphQLSchema
107
- */
108
-
109
-
110
- getSchema(id = "default") {
111
- var _this$servers$get2;
112
-
113
- return (_this$servers$get2 = this.servers.get(id)) == null ? void 0 : _this$servers$get2.schema;
114
- }
115
- /**
116
- *
117
- * @param {string} id
118
- * @returns {boolean}
119
- */
120
-
121
-
122
- has(id = "default") {
123
- return this.servers.has(id);
124
- }
125
-
126
- createInstance(options, server) {
127
- // istanbul ignore next
128
- if (server) {
129
- return server(options);
130
- } // istanbul ignore next
131
-
132
-
133
- try {
134
- const Server = require(`apollo-server-${this.platformName || "express"}`).ApolloServer;
135
-
136
- return new Server(options);
137
- } catch (er) {
138
- this.logger.error(`Platform "${this.platformName}" not supported by @tsed/apollo`);
139
- }
140
- }
141
-
142
- getPlugins(serverSettings) {
143
- const playground = serverSettings.playground || serverSettings.playground === undefined;
144
- return [playground && process.env.NODE_ENV === "production" ? ApolloServerPluginLandingPageDisabled() : ApolloServerPluginLandingPageGraphQLPlayground(), this.httpPort && ApolloServerPluginDrainHttpServer({
145
- httpServer: this.httpServer
146
- }), this.httpsPort && ApolloServerPluginDrainHttpServer({
147
- httpServer: this.httpsServer
148
- }), ...(serverSettings.plugins || [])].filter(Boolean);
149
- }
150
-
151
- };
152
-
153
- __decorate([Constant("PLATFORM_NAME"), __metadata("design:type", String)], ApolloService.prototype, "platformName", void 0);
154
-
155
- __decorate([Inject(), __metadata("design:type", Logger)], ApolloService.prototype, "logger", void 0);
156
-
157
- __decorate([Constant("httpPort"), __metadata("design:type", Object)], ApolloService.prototype, "httpPort", void 0);
158
-
159
- __decorate([Constant("httpsPort"), __metadata("design:type", Object)], ApolloService.prototype, "httpsPort", void 0);
160
-
161
- __decorate([Inject(), __metadata("design:type", PlatformApplication)], ApolloService.prototype, "app", void 0);
162
-
163
- __decorate([Inject(HttpServer), __metadata("design:type", Object)], ApolloService.prototype, "httpServer", void 0);
164
-
165
- __decorate([Inject(HttpsServer), __metadata("design:type", Object)], ApolloService.prototype, "httpsServer", void 0);
166
-
167
- ApolloService = __decorate([Service()], ApolloService);
168
-
169
- export { ApolloService };
170
- //# sourceMappingURL=index.modern.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.modern.js","sources":["../src/services/ApolloService.ts"],"sourcesContent":["import {Constant, Inject, Service} from \"@tsed/di\";\nimport {Logger} from \"@tsed/logger\";\nimport {HttpServer, HttpsServer, PlatformApplication} from \"@tsed/common\";\nimport type {Config} from \"apollo-server-core\";\nimport {\n ApolloServerBase,\n ApolloServerPluginDrainHttpServer,\n ApolloServerPluginLandingPageDisabled,\n ApolloServerPluginLandingPageGraphQLPlayground\n} from \"apollo-server-core\";\nimport type {GraphQLSchema} from \"graphql\";\nimport type {ApolloServer, ApolloSettings} from \"../interfaces/ApolloSettings\";\nimport {ApolloCustomServerCB} from \"../interfaces/ApolloSettings\";\n\n@Service()\nexport class ApolloService {\n @Constant(\"PLATFORM_NAME\")\n platformName: string;\n\n @Inject()\n protected logger: Logger;\n\n /**\n *\n * @type {Map<any, any>}\n * @private\n */\n protected servers: Map<\n string,\n {\n instance: ApolloServerBase;\n schema: GraphQLSchema | undefined;\n }\n > = new Map();\n\n @Constant(\"httpPort\")\n private httpPort: string | number;\n\n @Constant(\"httpsPort\")\n private httpsPort: string | number;\n\n @Inject()\n private app: PlatformApplication;\n\n @Inject(HttpServer)\n private httpServer: HttpServer;\n\n @Inject(HttpsServer)\n private httpsServer: HttpsServer;\n\n async createServer(id: string, settings: ApolloSettings): Promise<any> {\n if (this.has(id)) {\n return this.get(id)!;\n }\n\n try {\n const {path, middlewareOptions = {}, server: customServer, ...config} = settings;\n\n this.logger.info(`Create server with Apollo for: ${id}`);\n this.logger.debug(`options: ${JSON.stringify({path})}`);\n\n const server = this.createInstance(\n {\n ...config,\n plugins: this.getPlugins(settings)\n },\n customServer\n );\n\n if (server) {\n this.servers.set(id || \"default\", {\n instance: server,\n schema: settings.schema\n });\n\n await server.start();\n\n await server.applyMiddleware({\n path: settings.path,\n ...middlewareOptions,\n app: this.app.raw\n });\n\n return server;\n }\n } catch (err) {\n /* istanbul ignore next */\n this.logger.error(err);\n /* istanbul ignore next */\n process.exit(-1);\n }\n }\n\n /**\n * Get an instance of ApolloServer from his id\n * @returns ApolloServer\n */\n get(id: string = \"default\"): ApolloServerBase | undefined {\n return this.servers.get(id)?.instance;\n }\n\n /**\n * Get an instance of GraphQL schema from his id\n * @returns GraphQLSchema\n */\n getSchema(id: string = \"default\"): GraphQLSchema | undefined {\n return this.servers.get(id)?.schema;\n }\n\n /**\n *\n * @param {string} id\n * @returns {boolean}\n */\n has(id: string = \"default\"): boolean {\n return this.servers.has(id);\n }\n\n protected createInstance(options: Config, server?: ApolloCustomServerCB): ApolloServer | undefined {\n // istanbul ignore next\n if (server) {\n return server(options);\n }\n\n // istanbul ignore next\n try {\n const Server = require(`apollo-server-${this.platformName || \"express\"}`).ApolloServer;\n\n return new Server(options);\n } catch (er) {\n this.logger.error(`Platform \"${this.platformName}\" not supported by @tsed/apollo`);\n }\n }\n\n private getPlugins(serverSettings: ApolloSettings): any[] {\n const playground = serverSettings.playground || serverSettings.playground === undefined;\n\n return [\n playground && process.env.NODE_ENV === \"production\"\n ? ApolloServerPluginLandingPageDisabled()\n : ApolloServerPluginLandingPageGraphQLPlayground(),\n this.httpPort &&\n ApolloServerPluginDrainHttpServer({\n httpServer: this.httpServer\n }),\n this.httpsPort &&\n ApolloServerPluginDrainHttpServer({\n httpServer: this.httpsServer\n }),\n ...(serverSettings.plugins || [])\n ].filter(Boolean);\n }\n}\n"],"names":["ApolloService","constructor","Map","createServer","id","settings","has","get","path","middlewareOptions","server","customServer","config","logger","info","debug","JSON","stringify","createInstance","plugins","getPlugins","servers","set","instance","schema","start","applyMiddleware","app","raw","err","error","process","exit","getSchema","options","Server","require","platformName","ApolloServer","er","serverSettings","playground","undefined","env","NODE_ENV","ApolloServerPluginLandingPageDisabled","ApolloServerPluginLandingPageGraphQLPlayground","httpPort","ApolloServerPluginDrainHttpServer","httpServer","httpsPort","httpsServer","filter","Boolean","__decorate","Constant","Inject","Logger","PlatformApplication","HttpServer","HttpsServer","Service"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAeaA,aAAa,GAA1B,MAAaA,aAAb;AAAAC,EAAAA;AAOE;;;;;AAKU,gBAAA,GAMN,IAAIC,GAAJ,EANM;AA6HX;;AAtGmB,QAAZC,YAAY,CAACC,EAAD,EAAaC,QAAb;AAChB,QAAI,KAAKC,GAAL,CAASF,EAAT,CAAJ,EAAkB;AAChB,aAAO,KAAKG,GAAL,CAASH,EAAT,CAAP;AACD;;AAED,QAAI;AACF,YAAM;AAACI,QAAAA,IAAD;AAAOC,QAAAA,iBAAiB,GAAG,EAA3B;AAA+BC,QAAAA,MAAM,EAAEC;AAAvC,UAAkEN,QAAxE;AAAA,YAA8DO,MAA9D,iCAAwEP,QAAxE;;AA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