@tsed/apollo 7.0.0-alpha.2 → 7.0.0-beta.1

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.
package/jest.config.js ADDED
@@ -0,0 +1,14 @@
1
+ // For a detailed explanation regarding each configuration property, visit:
2
+ // https://jestjs.io/docs/en/configuration.html
3
+
4
+ module.exports = {
5
+ ...require("@tsed/jest-config")(__dirname, "apollo"),
6
+ coverageThreshold: {
7
+ global: {
8
+ statements: 100,
9
+ branches: 75,
10
+ functions: 100,
11
+ lines: 100
12
+ }
13
+ }
14
+ };
@@ -19,30 +19,34 @@ let ApolloModule = class ApolloModule {
19
19
  }
20
20
  }
21
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
+ };
22
27
  const { settings } = this;
23
28
  if (settings) {
24
- const host = this.configuration.getHttpPort();
25
29
  Object.entries(settings).map(async ([key, options]) => {
26
30
  const { path } = options;
27
- this.logger.info(`[${key}] Apollo server is available on http://${host.address}:${host.port}/${path.replace(/^\//, "")}`);
31
+ displayLog(key, path);
28
32
  });
29
33
  }
30
34
  }
31
35
  };
32
- tslib_1.__decorate([
33
- di_1.Inject(),
34
- tslib_1.__metadata("design:type", common_1.Logger)
36
+ (0, tslib_1.__decorate)([
37
+ (0, di_1.Inject)(),
38
+ (0, tslib_1.__metadata)("design:type", common_1.Logger)
35
39
  ], ApolloModule.prototype, "logger", void 0);
36
- tslib_1.__decorate([
37
- di_1.Inject(),
38
- tslib_1.__metadata("design:type", ApolloService_1.ApolloService)
40
+ (0, tslib_1.__decorate)([
41
+ (0, di_1.Inject)(),
42
+ (0, tslib_1.__metadata)("design:type", ApolloService_1.ApolloService)
39
43
  ], ApolloModule.prototype, "service", void 0);
40
- tslib_1.__decorate([
41
- di_1.Configuration(),
42
- tslib_1.__metadata("design:type", Object)
44
+ (0, tslib_1.__decorate)([
45
+ (0, di_1.Configuration)(),
46
+ (0, tslib_1.__metadata)("design:type", Object)
43
47
  ], ApolloModule.prototype, "configuration", void 0);
44
- ApolloModule = tslib_1.__decorate([
45
- di_1.Module()
48
+ ApolloModule = (0, tslib_1.__decorate)([
49
+ (0, di_1.Module)()
46
50
  ], ApolloModule);
47
51
  exports.ApolloModule = ApolloModule;
48
52
  //# sourceMappingURL=ApolloModule.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ApolloModule.js","sourceRoot":"","sources":["../../src/ApolloModule.ts"],"names":[],"mappings":";;;;AAAA,yCAA+D;AAC/D,iCAAuD;AAEvD,4DAAuD;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,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"]}
@@ -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 @@
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,133 @@
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
+ const http_1 = (0, tslib_1.__importDefault)(require("http"));
10
+ const https_1 = (0, tslib_1.__importDefault)(require("https"));
11
+ let ApolloService = class ApolloService {
12
+ constructor() {
13
+ /**
14
+ *
15
+ * @type {Map<any, any>}
16
+ * @private
17
+ */
18
+ this.servers = new Map();
19
+ }
20
+ async createServer(id, settings) {
21
+ if (this.has(id)) {
22
+ return this.get(id);
23
+ }
24
+ try {
25
+ const { path, middlewareOptions = {}, server: customServer, ...config } = settings;
26
+ this.logger.info(`Create server with Apollo for: ${id}`);
27
+ this.logger.debug(`options: ${JSON.stringify({ path })}`);
28
+ const server = this.createInstance({
29
+ ...config,
30
+ plugins: this.getPlugins(settings)
31
+ }, customServer);
32
+ if (server) {
33
+ this.servers.set(id || "default", {
34
+ instance: server,
35
+ schema: settings.schema
36
+ });
37
+ await server.start();
38
+ await server.applyMiddleware({
39
+ path: settings.path,
40
+ ...middlewareOptions,
41
+ app: this.app.raw
42
+ });
43
+ return server;
44
+ }
45
+ }
46
+ catch (err) {
47
+ /* istanbul ignore next */
48
+ this.logger.error(err);
49
+ /* istanbul ignore next */
50
+ process.exit(-1);
51
+ }
52
+ }
53
+ /**
54
+ * Get an instance of ApolloServer from his id
55
+ * @returns ApolloServer
56
+ */
57
+ get(id = "default") {
58
+ var _a;
59
+ return (_a = this.servers.get(id)) === null || _a === void 0 ? void 0 : _a.instance;
60
+ }
61
+ /**
62
+ * Get an instance of GraphQL schema from his id
63
+ * @returns GraphQLSchema
64
+ */
65
+ getSchema(id = "default") {
66
+ var _a;
67
+ return (_a = this.servers.get(id)) === null || _a === void 0 ? void 0 : _a.schema;
68
+ }
69
+ /**
70
+ *
71
+ * @param {string} id
72
+ * @returns {boolean}
73
+ */
74
+ has(id = "default") {
75
+ return this.servers.has(id);
76
+ }
77
+ createInstance(options, server) {
78
+ // istanbul ignore next
79
+ if (server) {
80
+ return server(options);
81
+ }
82
+ // istanbul ignore next
83
+ try {
84
+ const Server = require(`apollo-server-${this.platformName || "express"}`).ApolloServer;
85
+ return new Server(options);
86
+ }
87
+ catch (er) {
88
+ this.logger.error(`Platform "${this.platformName}" not supported by @tsed/apollo`);
89
+ }
90
+ }
91
+ getPlugins(serverSettings) {
92
+ const playground = serverSettings.playground || serverSettings.playground === undefined;
93
+ return [
94
+ playground && process.env.NODE_ENV === "production"
95
+ ? (0, apollo_server_core_1.ApolloServerPluginLandingPageDisabled)()
96
+ : (0, apollo_server_core_1.ApolloServerPluginLandingPageGraphQLPlayground)(),
97
+ this.httpServer &&
98
+ (0, apollo_server_core_1.ApolloServerPluginDrainHttpServer)({
99
+ httpServer: this.httpServer
100
+ }),
101
+ this.httpsServer &&
102
+ (0, apollo_server_core_1.ApolloServerPluginDrainHttpServer)({
103
+ httpServer: this.httpsServer
104
+ }),
105
+ ...(serverSettings.plugins || [])
106
+ ].filter(Boolean);
107
+ }
108
+ };
109
+ (0, tslib_1.__decorate)([
110
+ (0, di_1.Constant)("PLATFORM_NAME"),
111
+ (0, tslib_1.__metadata)("design:type", String)
112
+ ], ApolloService.prototype, "platformName", void 0);
113
+ (0, tslib_1.__decorate)([
114
+ (0, di_1.Inject)(),
115
+ (0, tslib_1.__metadata)("design:type", logger_1.Logger)
116
+ ], ApolloService.prototype, "logger", void 0);
117
+ (0, tslib_1.__decorate)([
118
+ (0, di_1.Inject)(),
119
+ (0, tslib_1.__metadata)("design:type", common_1.PlatformApplication)
120
+ ], ApolloService.prototype, "app", void 0);
121
+ (0, tslib_1.__decorate)([
122
+ (0, di_1.Inject)(http_1.default.Server),
123
+ (0, tslib_1.__metadata)("design:type", Object)
124
+ ], ApolloService.prototype, "httpServer", void 0);
125
+ (0, tslib_1.__decorate)([
126
+ (0, di_1.Inject)(https_1.default.Server),
127
+ (0, tslib_1.__metadata)("design:type", Object)
128
+ ], ApolloService.prototype, "httpsServer", void 0);
129
+ ApolloService = (0, tslib_1.__decorate)([
130
+ (0, di_1.Service)()
131
+ ], ApolloService);
132
+ exports.ApolloService = ApolloService;
133
+ //# 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,yCAAiD;AAEjD,2DAK4B;AAI5B,6DAAwB;AACxB,+DAA0B;AAG1B,IAAa,aAAa,GAA1B,MAAa,aAAa;IAA1B;QAOE;;;;WAIG;QACO,YAAO,GAMb,IAAI,GAAG,EAAE,CAAC;IAiHhB,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,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,CAAC,YAAY,CAAC;YAEvF,OAAO,IAAI,MAAM,CAAC,OAAO,CAAC,CAAC;SAC5B;QAAC,OAAO,EAAE,EAAE;YACX,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,aAAa,IAAI,CAAC,YAAY,iCAAiC,CAAC,CAAC;SACpF;IACH,CAAC;IAEO,UAAU,CAAC,cAA8B;QAC/C,MAAM,UAAU,GAAG,cAAc,CAAC,UAAU,IAAI,cAAc,CAAC,UAAU,KAAK,SAAS,CAAC;QAExF,OAAO;YACL,UAAU,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY;gBACjD,CAAC,CAAC,IAAA,0DAAqC,GAAE;gBACzC,CAAC,CAAC,IAAA,mEAA8C,GAAE;YACpD,IAAI,CAAC,UAAU;gBACb,IAAA,sDAAiC,EAAC;oBAChC,UAAU,EAAE,IAAI,CAAC,UAAU;iBAC5B,CAAC;YACJ,IAAI,CAAC,WAAW;gBACd,IAAA,sDAAiC,EAAC;oBAChC,UAAU,EAAE,IAAI,CAAC,WAAW;iBAC7B,CAAC;YACJ,GAAG,CAAC,cAAc,CAAC,OAAO,IAAI,EAAE,CAAC;SAClC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IACpB,CAAC;CACF,CAAA;AAjIC;IADC,IAAA,aAAQ,EAAC,eAAe,CAAC;;mDACL;AAGrB;IADC,IAAA,WAAM,GAAE;2CACS,eAAM;6CAAC;AAgBzB;IADC,IAAA,WAAM,GAAE;2CACI,4BAAmB;0CAAC;AAGjC;IADC,IAAA,WAAM,EAAC,cAAI,CAAC,MAAM,CAAC;;iDACmB;AAGvC;IADC,IAAA,WAAM,EAAC,eAAK,CAAC,MAAM,CAAC;;kDACoB;AA3B9B,aAAa;IADzB,IAAA,YAAO,GAAE;GACG,aAAa,CAmIzB;AAnIY,sCAAa","sourcesContent":["import {Constant, Inject, Service} from \"@tsed/di\";\nimport {Logger} from \"@tsed/logger\";\nimport {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\";\nimport Http from \"http\";\nimport Https from \"https\";\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 @Inject()\n private app: PlatformApplication;\n\n @Inject(Http.Server)\n private httpServer: Http.Server | null;\n\n @Inject(Https.Server)\n private httpsServer: Https.Server | null;\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.httpServer &&\n ApolloServerPluginDrainHttpServer({\n httpServer: this.httpServer\n }),\n this.httpsServer &&\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,130 @@
1
+ import { __decorate, __metadata } from "tslib";
2
+ import { Constant, Inject, Service } from "@tsed/di";
3
+ import { Logger } from "@tsed/logger";
4
+ import { PlatformApplication } from "@tsed/common";
5
+ import { ApolloServerPluginDrainHttpServer, ApolloServerPluginLandingPageDisabled, ApolloServerPluginLandingPageGraphQLPlayground } from "apollo-server-core";
6
+ import Http from "http";
7
+ import Https from "https";
8
+ let ApolloService = class ApolloService {
9
+ constructor() {
10
+ /**
11
+ *
12
+ * @type {Map<any, any>}
13
+ * @private
14
+ */
15
+ this.servers = new Map();
16
+ }
17
+ async createServer(id, settings) {
18
+ if (this.has(id)) {
19
+ return this.get(id);
20
+ }
21
+ try {
22
+ const { path, middlewareOptions = {}, server: customServer, ...config } = settings;
23
+ this.logger.info(`Create server with Apollo for: ${id}`);
24
+ this.logger.debug(`options: ${JSON.stringify({ path })}`);
25
+ const server = this.createInstance({
26
+ ...config,
27
+ plugins: this.getPlugins(settings)
28
+ }, customServer);
29
+ if (server) {
30
+ this.servers.set(id || "default", {
31
+ instance: server,
32
+ schema: settings.schema
33
+ });
34
+ await server.start();
35
+ await server.applyMiddleware({
36
+ path: settings.path,
37
+ ...middlewareOptions,
38
+ app: this.app.raw
39
+ });
40
+ return server;
41
+ }
42
+ }
43
+ catch (err) {
44
+ /* istanbul ignore next */
45
+ this.logger.error(err);
46
+ /* istanbul ignore next */
47
+ process.exit(-1);
48
+ }
49
+ }
50
+ /**
51
+ * Get an instance of ApolloServer from his id
52
+ * @returns ApolloServer
53
+ */
54
+ get(id = "default") {
55
+ var _a;
56
+ return (_a = this.servers.get(id)) === null || _a === void 0 ? void 0 : _a.instance;
57
+ }
58
+ /**
59
+ * Get an instance of GraphQL schema from his id
60
+ * @returns GraphQLSchema
61
+ */
62
+ getSchema(id = "default") {
63
+ var _a;
64
+ return (_a = this.servers.get(id)) === null || _a === void 0 ? void 0 : _a.schema;
65
+ }
66
+ /**
67
+ *
68
+ * @param {string} id
69
+ * @returns {boolean}
70
+ */
71
+ has(id = "default") {
72
+ return this.servers.has(id);
73
+ }
74
+ createInstance(options, server) {
75
+ // istanbul ignore next
76
+ if (server) {
77
+ return server(options);
78
+ }
79
+ // istanbul ignore next
80
+ try {
81
+ const Server = require(`apollo-server-${this.platformName || "express"}`).ApolloServer;
82
+ return new Server(options);
83
+ }
84
+ catch (er) {
85
+ this.logger.error(`Platform "${this.platformName}" not supported by @tsed/apollo`);
86
+ }
87
+ }
88
+ getPlugins(serverSettings) {
89
+ const playground = serverSettings.playground || serverSettings.playground === undefined;
90
+ return [
91
+ playground && process.env.NODE_ENV === "production"
92
+ ? ApolloServerPluginLandingPageDisabled()
93
+ : ApolloServerPluginLandingPageGraphQLPlayground(),
94
+ this.httpServer &&
95
+ ApolloServerPluginDrainHttpServer({
96
+ httpServer: this.httpServer
97
+ }),
98
+ this.httpsServer &&
99
+ ApolloServerPluginDrainHttpServer({
100
+ httpServer: this.httpsServer
101
+ }),
102
+ ...(serverSettings.plugins || [])
103
+ ].filter(Boolean);
104
+ }
105
+ };
106
+ __decorate([
107
+ Constant("PLATFORM_NAME"),
108
+ __metadata("design:type", String)
109
+ ], ApolloService.prototype, "platformName", void 0);
110
+ __decorate([
111
+ Inject(),
112
+ __metadata("design:type", Logger)
113
+ ], ApolloService.prototype, "logger", void 0);
114
+ __decorate([
115
+ Inject(),
116
+ __metadata("design:type", PlatformApplication)
117
+ ], ApolloService.prototype, "app", void 0);
118
+ __decorate([
119
+ Inject(Http.Server),
120
+ __metadata("design:type", Object)
121
+ ], ApolloService.prototype, "httpServer", void 0);
122
+ __decorate([
123
+ Inject(Https.Server),
124
+ __metadata("design:type", Object)
125
+ ], ApolloService.prototype, "httpsServer", void 0);
126
+ ApolloService = __decorate([
127
+ Service()
128
+ ], ApolloService);
129
+ export { ApolloService };
130
+ //# 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,mBAAmB,EAAC,MAAM,cAAc,CAAC;AAEjD,OAAO,EAEL,iCAAiC,EACjC,qCAAqC,EACrC,8CAA8C,EAC/C,MAAM,oBAAoB,CAAC;AAI5B,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,KAAK,MAAM,OAAO,CAAC;AAG1B,IAAa,aAAa,GAA1B,MAAa,aAAa;IAA1B;QAOE;;;;WAIG;QACO,YAAO,GAMb,IAAI,GAAG,EAAE,CAAC;IAiHhB,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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BAAiB,IAAI,CAAC,YAAY,IAAI,SAAS,EAAE,CAAC,CAAC,YAAY,CAAC;YAEvF,OAAO,IAAI,MAAM,CAAC,OAAO,CAAC,CAAC;SAC5B;QAAC,OAAO,EAAE,EAAE;YACX,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,aAAa,IAAI,CAAC,YAAY,iCAAiC,CAAC,CAAC;SACpF;IACH,CAAC;IAEO,UAAU,CAAC,cAA8B;QAC/C,MAAM,UAAU,GAAG,cAAc,CAAC,UAAU,IAAI,cAAc,CAAC,UAAU,KAAK,SAAS,CAAC;QAExF,OAAO;YACL,UAAU,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY;gBACjD,CAAC,CAAC,qCAAqC,EAAE;gBACzC,CAAC,CAAC,8CAA8C,EAAE;YACpD,IAAI,CAAC,UAAU;gBACb,iCAAiC,CAAC;oBAChC,UAAU,EAAE,IAAI,CAAC,UAAU;iBAC5B,CAAC;YACJ,IAAI,CAAC,WAAW;gBACd,iCAAiC,CAAC;oBAChC,UAAU,EAAE,IAAI,CAAC,WAAW;iBAC7B,CAAC;YACJ,GAAG,CAAC,cAAc,CAAC,OAAO,IAAI,EAAE,CAAC;SAClC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IACpB,CAAC;CACF,CAAA;AAjIC;IADC,QAAQ,CAAC,eAAe,CAAC;;mDACL;AAGrB;IADC,MAAM,EAAE;8BACS,MAAM;6CAAC;AAgBzB;IADC,MAAM,EAAE;8BACI,mBAAmB;0CAAC;AAGjC;IADC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC;;iDACmB;AAGvC;IADC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC;;kDACoB;AA3B9B,aAAa;IADzB,OAAO,EAAE;GACG,aAAa,CAmIzB;SAnIY,aAAa","sourcesContent":["import {Constant, Inject, Service} from \"@tsed/di\";\nimport {Logger} from \"@tsed/logger\";\nimport {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\";\nimport Http from \"http\";\nimport Https from \"https\";\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 @Inject()\n private app: PlatformApplication;\n\n @Inject(Http.Server)\n private httpServer: Http.Server | null;\n\n @Inject(Https.Server)\n private httpsServer: Https.Server | null;\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.httpServer &&\n ApolloServerPluginDrainHttpServer({\n httpServer: this.httpServer\n }),\n this.httpsServer &&\n ApolloServerPluginDrainHttpServer({\n httpServer: this.httpsServer\n }),\n ...(serverSettings.plugins || [])\n ].filter(Boolean);\n }\n}\n"]}
File without changes
@@ -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
+ }
@@ -1,22 +1,30 @@
1
- import { Logger } from "@tsed/common";
1
+ import { Logger } from "@tsed/logger";
2
2
  import type { Config } from "apollo-server-core";
3
+ import { ApolloServerBase } from "apollo-server-core";
3
4
  import type { GraphQLSchema } from "graphql";
4
5
  import type { ApolloServer, ApolloSettings } from "../interfaces/ApolloSettings";
6
+ import { ApolloCustomServerCB } from "../interfaces/ApolloSettings";
5
7
  export declare class ApolloService {
6
- #private;
7
8
  platformName: string;
8
- private httpPort;
9
- private httpsPort;
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
+ }>;
10
19
  private app;
11
20
  private httpServer;
12
21
  private httpsServer;
13
- protected logger: Logger;
14
22
  createServer(id: string, settings: ApolloSettings): Promise<any>;
15
23
  /**
16
24
  * Get an instance of ApolloServer from his id
17
25
  * @returns ApolloServer
18
26
  */
19
- get(id?: string): ApolloServer | undefined;
27
+ get(id?: string): ApolloServerBase | undefined;
20
28
  /**
21
29
  * Get an instance of GraphQL schema from his id
22
30
  * @returns GraphQLSchema
@@ -28,6 +36,6 @@ export declare class ApolloService {
28
36
  * @returns {boolean}
29
37
  */
30
38
  has(id?: string): boolean;
31
- protected bindServer(server: ApolloServer, options: ApolloSettings): Promise<ApolloServer>;
32
- protected createInstance(options: Config, server?: (config: Config) => ApolloServer): ApolloServer | undefined;
39
+ protected createInstance(options: Config, server?: ApolloCustomServerCB): ApolloServer | undefined;
40
+ private getPlugins;
33
41
  }
package/package.json CHANGED
@@ -1,29 +1,49 @@
1
1
  {
2
2
  "name": "@tsed/apollo",
3
- "version": "7.0.0-alpha.2",
3
+ "version": "7.0.0-beta.1",
4
4
  "description": "Apollo package for Ts.ED framework",
5
- "main": "./lib/index.js",
6
- "typings": "./lib/index.d.ts",
5
+ "source": "./src/index.ts",
6
+ "main": "./lib/cjs/index.js",
7
+ "typings": "./lib/types/index.d.ts",
8
+ "exports": {
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"
13
+ },
7
14
  "scripts": {
8
- "build": "tsc --build tsconfig.compile.json",
9
- "start": "ts-node -r tsconfig-paths/register test/app/index.ts",
10
- "test": "cross-env NODE_ENV=test nyc mocha"
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",
18
+ "test": "cross-env NODE_ENV=test jest"
11
19
  },
12
20
  "dependencies": {
13
21
  "tslib": "2.2.0"
14
22
  },
15
23
  "private": false,
16
24
  "devDependencies": {
17
- "@tsed/common": "7.0.0-alpha.2",
18
- "@tsed/core": "7.0.0-alpha.2",
25
+ "@tsed/common": "7.0.0-beta.1",
26
+ "@tsed/core": "7.0.0-beta.1",
27
+ "@tsed/di": "7.0.0-beta.1",
28
+ "@tsed/logger": "^6.0.0",
19
29
  "@types/graphql": "14.5.0",
20
- "apollo-server-core": "^2.17.0",
21
- "apollo-server-testing": "2.18.2",
22
- "graphql": "15.5.0"
30
+ "apollo-datasource": "^3.2.0",
31
+ "apollo-datasource-rest": "^3.3.0",
32
+ "apollo-server-core": "^3.4.0",
33
+ "apollo-server-express": "^3.4.0",
34
+ "apollo-server-koa": "^3.4.0",
35
+ "apollo-server-testing": "^2.25.2",
36
+ "graphql": "15.7.2"
23
37
  },
24
38
  "peerDependencies": {
25
- "apollo-server-core": "^2.17.0",
26
- "graphql": "15.5.0"
39
+ "@tsed/common": "^7.0.0-beta.1",
40
+ "@tsed/core": "^7.0.0-beta.1",
41
+ "@tsed/di": "^7.0.0-beta.1",
42
+ "@tsed/logger": ">6.0.0",
43
+ "apollo-datasource": ">=3.0.0",
44
+ "apollo-datasource-rest": ">=3.0.0",
45
+ "apollo-server-core": ">=3.0.0",
46
+ "graphql": ">15.0.0"
27
47
  },
28
48
  "repository": "https://github.com/tsedio/tsed",
29
49
  "bugs": {
@@ -31,8 +51,5 @@
31
51
  },
32
52
  "homepage": "https://github.com/tsedio/tsed/tree/alpha/packages/graphql/apollo",
33
53
  "author": "Romain Lenzotti",
34
- "license": "MIT",
35
- "publishConfig": {
36
- "tag": "alpha"
37
- }
54
+ "license": "MIT"
38
55
  }
@@ -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/.mocharc.js DELETED
@@ -1,5 +0,0 @@
1
- module.exports = {
2
- ...require('@tsed/mocha-config')(),
3
- spec: ["{src,test}/**/*.spec.ts"],
4
- exclude: [],
5
- };
@@ -1 +0,0 @@
1
- {"version":3,"file":"ApolloModule.js","sourceRoot":"","sources":["../src/ApolloModule.ts"],"names":[],"mappings":";;;;AAAA,yCAA6G;AAC7G,iCAAiE;AAEjE,4DAAuD;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,EAAC,QAAQ,EAAC,GAAG,IAAI,CAAC;QAExB,IAAI,QAAQ,EAAE;YACZ,MAAM,IAAI,GAAG,IAAI,CAAC,aAAa,CAAC,WAAW,EAAE,CAAC;YAE9C,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;gBACvB,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,GAAG,0CAA0C,IAAI,CAAC,OAAO,IAAI,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC;YAC5H,CAAC,CAAC,CAAC;SACJ;IACH,CAAC;CACF,CAAA;AApCC;IADC,WAAM,EAAE;sCACS,eAAM;4CAAC;AAGzB;IADC,WAAM,EAAE;sCACU,6BAAa;6CAAC;AAGjC;IADC,kBAAa,EAAE;;mDACuB;AAR5B,YAAY;IADxB,WAAM,EAAE;GACI,YAAY,CAsCxB;AAtCY,oCAAY","sourcesContent":["import {AfterListen, HttpServer, HttpsServer, Logger, OnRoutesInit, PlatformApplication} from \"@tsed/common\";\nimport {Configuration, Constant, Inject, Module} from \"@tsed/di\";\nimport {ApolloServer, 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 {settings} = this;\n\n if (settings) {\n const host = this.configuration.getHttpPort();\n\n Object.entries(settings).map(async ([key, options]) => {\n const {path} = options;\n this.logger.info(`[${key}] Apollo server is available on http://${host.address}:${host.port}/${path.replace(/^\\//, \"\")}`);\n });\n }\n }\n}\n"]}
package/lib/index.js DELETED
@@ -1,6 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
- tslib_1.__exportStar(require("./interfaces/ApolloSettings"), exports);
5
- tslib_1.__exportStar(require("./services/ApolloService"), exports);
6
- //# sourceMappingURL=index.js.map
package/lib/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,sEAA4C;AAC5C,mEAAyC","sourcesContent":["export * from \"./interfaces/ApolloSettings\";\nexport * from \"./services/ApolloService\";\n"]}
@@ -1,11 +0,0 @@
1
- import type { Config } from "apollo-server-core";
2
- import type { ApolloServerBase } from "apollo-server-core";
3
- export declare type ApolloServer = ApolloServerBase & {
4
- applyMiddleware(settings: any): any;
5
- };
6
- export interface ApolloSettings extends Config {
7
- path: string;
8
- server?: (config: Config) => ApolloServer;
9
- serverRegistration?: Record<string, any>;
10
- installSubscriptionHandlers?: boolean;
11
- }
@@ -1 +0,0 @@
1
- {"version":3,"file":"ApolloSettings.js","sourceRoot":"","sources":["../../src/interfaces/ApolloSettings.ts"],"names":[],"mappings":"","sourcesContent":["import type {Config} from \"apollo-server-core\";\nimport type {ApolloServerBase} from \"apollo-server-core\";\n\nexport type ApolloServer = ApolloServerBase & {applyMiddleware(settings: any): any};\n\nexport interface ApolloSettings extends Config {\n // Basic options\n path: string;\n server?: (config: Config) => ApolloServer;\n\n // apollo-server-express\n // apollo-server-express options\n // See options descriptions on https://www.apollographql.com/docs/apollo-server/api/apollo-server.html\n serverRegistration?: Record<string, any>;\n installSubscriptionHandlers?: boolean;\n}\n"]}
@@ -1,120 +0,0 @@
1
- "use strict";
2
- var _servers;
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.ApolloService = void 0;
5
- const tslib_1 = require("tslib");
6
- const common_1 = require("@tsed/common");
7
- let ApolloService = class ApolloService {
8
- constructor() {
9
- /**
10
- *
11
- * @type {Map<any, any>}
12
- * @private
13
- */
14
- _servers.set(this, new Map());
15
- }
16
- async createServer(id, settings) {
17
- if (this.has(id)) {
18
- return this.get(id);
19
- }
20
- try {
21
- const { path, installSubscriptionHandlers, serverRegistration = {}, server: customServer, ...serverSettings } = settings;
22
- this.logger.info(`Create server with Apollo for: ${id}`);
23
- this.logger.debug(`options: ${JSON.stringify({ path })}`);
24
- const server = this.createInstance(serverSettings, customServer);
25
- if (server) {
26
- tslib_1.__classPrivateFieldGet(this, _servers).set(id || "default", {
27
- instance: server,
28
- schema: settings.schema
29
- });
30
- return this.bindServer(server, settings);
31
- }
32
- }
33
- catch (err) {
34
- /* istanbul ignore next */
35
- this.logger.error(err);
36
- /* istanbul ignore next */
37
- process.exit(-1);
38
- }
39
- }
40
- /**
41
- * Get an instance of ApolloServer from his id
42
- * @returns ApolloServer
43
- */
44
- get(id = "default") {
45
- var _a;
46
- return (_a = tslib_1.__classPrivateFieldGet(this, _servers).get(id)) === null || _a === void 0 ? void 0 : _a.instance;
47
- }
48
- /**
49
- * Get an instance of GraphQL schema from his id
50
- * @returns GraphQLSchema
51
- */
52
- getSchema(id = "default") {
53
- var _a;
54
- return (_a = tslib_1.__classPrivateFieldGet(this, _servers).get(id)) === null || _a === void 0 ? void 0 : _a.schema;
55
- }
56
- /**
57
- *
58
- * @param {string} id
59
- * @returns {boolean}
60
- */
61
- has(id = "default") {
62
- return tslib_1.__classPrivateFieldGet(this, _servers).has(id);
63
- }
64
- async bindServer(server, options) {
65
- await server.applyMiddleware({ path: options.path, ...options.serverRegistration, app: this.app.raw });
66
- if (options.installSubscriptionHandlers && (this.httpPort || this.httpsPort)) {
67
- this.httpPort && server.installSubscriptionHandlers(this.httpServer);
68
- this.httpPort && server.installSubscriptionHandlers(this.httpsServer);
69
- }
70
- return server;
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
- };
87
- _servers = new WeakMap();
88
- tslib_1.__decorate([
89
- common_1.Constant("PLATFORM_NAME"),
90
- tslib_1.__metadata("design:type", String)
91
- ], ApolloService.prototype, "platformName", void 0);
92
- tslib_1.__decorate([
93
- common_1.Constant("httpPort"),
94
- tslib_1.__metadata("design:type", Object)
95
- ], ApolloService.prototype, "httpPort", void 0);
96
- tslib_1.__decorate([
97
- common_1.Constant("httpsPort"),
98
- tslib_1.__metadata("design:type", Object)
99
- ], ApolloService.prototype, "httpsPort", void 0);
100
- tslib_1.__decorate([
101
- common_1.Inject(),
102
- tslib_1.__metadata("design:type", common_1.PlatformApplication)
103
- ], ApolloService.prototype, "app", void 0);
104
- tslib_1.__decorate([
105
- common_1.Inject(common_1.HttpServer),
106
- tslib_1.__metadata("design:type", Object)
107
- ], ApolloService.prototype, "httpServer", void 0);
108
- tslib_1.__decorate([
109
- common_1.Inject(common_1.HttpsServer),
110
- tslib_1.__metadata("design:type", Object)
111
- ], ApolloService.prototype, "httpsServer", void 0);
112
- tslib_1.__decorate([
113
- common_1.Inject(),
114
- tslib_1.__metadata("design:type", common_1.Logger)
115
- ], ApolloService.prototype, "logger", void 0);
116
- ApolloService = tslib_1.__decorate([
117
- common_1.Service()
118
- ], ApolloService);
119
- exports.ApolloService = ApolloService;
120
- //# sourceMappingURL=ApolloService.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ApolloService.js","sourceRoot":"","sources":["../../src/services/ApolloService.ts"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IACH,CAAC;CACF,CAAA;;AAjHC;IADC,iBAAQ,CAAC,eAAe,CAAC;;mDACL;AAGrB;IADC,iBAAQ,CAAC,UAAU,CAAC;;+CACa;AAGlC;IADC,iBAAQ,CAAC,WAAW,CAAC;;gDACa;AAGnC;IADC,eAAM,EAAE;sCACI,4BAAmB;0CAAC;AAGjC;IADC,eAAM,CAAC,mBAAU,CAAC;;iDACY;AAG/B;IADC,eAAM,CAAC,oBAAW,CAAC;;kDACa;AAGjC;IADC,eAAM,EAAE;sCACS,eAAM;6CAAC;AApBd,aAAa;IADzB,gBAAO,EAAE;GACG,aAAa,CAmHzB;AAnHY,sCAAa","sourcesContent":["import {Constant, HttpServer, HttpsServer, Inject, Logger, PlatformApplication, Service} from \"@tsed/common\";\nimport type {Config} from \"apollo-server-core\";\nimport type {GraphQLSchema} from \"graphql\";\nimport type {ApolloServer, ApolloSettings} from \"../interfaces/ApolloSettings\";\n\n@Service()\nexport class ApolloService {\n @Constant(\"PLATFORM_NAME\")\n platformName: string;\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 @Inject()\n protected logger: Logger;\n\n /**\n *\n * @type {Map<any, any>}\n * @private\n */\n #servers: Map<\n string,\n {\n instance: ApolloServer;\n schema: GraphQLSchema | undefined;\n }\n > = new Map();\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, installSubscriptionHandlers, serverRegistration = {}, server: customServer, ...serverSettings} = 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(serverSettings, customServer);\n\n if (server) {\n this.#servers.set(id || \"default\", {\n instance: server,\n schema: settings.schema\n });\n\n return this.bindServer(server, settings);\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\"): ApolloServer | 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 async bindServer(server: ApolloServer, options: ApolloSettings) {\n await server.applyMiddleware({path: options.path, ...options.serverRegistration, app: this.app.raw});\n\n if (options.installSubscriptionHandlers && (this.httpPort || this.httpsPort)) {\n this.httpPort && server.installSubscriptionHandlers(this.httpServer);\n this.httpPort && server.installSubscriptionHandlers(this.httpsServer);\n }\n\n return server;\n }\n\n protected createInstance(options: Config, server?: (config: Config) => ApolloServer): 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"]}