@tsed/apollo 6.83.0 → 6.86.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.
- package/jest.config.js +14 -0
- package/lib/index.js +168 -6
- package/lib/index.js.map +1 -1
- package/lib/index.modern.js +170 -0
- package/lib/index.modern.js.map +1 -0
- package/lib/interfaces/ApolloSettings.d.ts +10 -7
- package/lib/services/ApolloService.d.ts +12 -10
- package/package.json +27 -11
- package/.mocharc.js +0 -5
- package/lib/ApolloModule.js +0 -52
- package/lib/ApolloModule.js.map +0 -1
- package/lib/interfaces/ApolloSettings.js +0 -3
- package/lib/interfaces/ApolloSettings.js.map +0 -1
- package/lib/services/ApolloService.js +0 -118
- package/lib/services/ApolloService.js.map +0 -1
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
|
+
};
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,168 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","
|
|
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;;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;;AAEA,WAAKQ,MAAL,CAAYC,IAAZ,mCAAmDV,IAAnD;AACA,WAAKS,MAAL,CAAYE,KAAZ,aAA8BC,IAAI,CAACC,SAAL,CAAe;AAACT,QAAAA;AAAD,OAAf,GAA9B;AAEA,YAAME,MAAM,GAAG,KAAKQ,cAAL,cAERN,MAFQ;AAGXO,QAAAA,OAAO,EAAE,KAAKC,UAAL,CAAgBf,QAAhB;AAHE,UAKbM,YALa,CAAf;;AAQA,UAAID,MAAJ,EAAY;AACV,aAAKW,OAAL,CAAaC,GAAb,CAAiBlB,EAAE,IAAI,SAAvB,EAAkC;AAChCmB,U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ACD;AACF;;AAEOjB,EAAAA,UAAU,CAACoB,cAAD;AAChB,UAAMC,UAAU,GAAGD,cAAc,CAACC,UAAf,IAA6BD,cAAc,CAACC,UAAf,KAA8BC,SAA9E;AAEA,WAAO,CACLD,UAAU,IAAIV,OAAO,CAACY,GAAR,CAAYC,QAAZ,KAAyB,YAAvC,GACIC,sDAAqC,EADzC,GAEIC,+DAA8C,EAH7C,EAIL,KAAKC,QAAL,IACEC,kDAAiC,CAAC;AAChCC,MAAAA,UAAU,EAAE,KAAKA;AADe,KAAD,CAL9B,EAQL,KAAKC,SAAL,IACEF,kDAAiC,CAAC;AAChCC,MAAAA,UAAU,EAAE,KAAKE;AADe,KAAD,CAT9B,EAYL,IAAIX,cAAc,CAACrB,OAAf,IAA0B,EAA9B,CAZK,EAaLiC,MAbK,CAaEC,OAbF,CAAP;AAcD;;;;AAtIDC,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;;"}
|
|
@@ -0,0 +1,170 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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;;AAEA,WAAKQ,MAAL,CAAYC,IAAZ,mCAAmDV,IAAnD;AACA,WAAKS,MAAL,CAAYE,KAAZ,aAA8BC,IAAI,CAACC,SAAL,CAAe;AAACT,QAAAA;AAAD,OAAf,GAA9B;AAEA,YAAME,MAAM,GAAG,KAAKQ,cAAL,cAERN,MAFQ;AAGXO,QAAAA,OAAO,EAAE,KAAKC,UAAL,CAAgBf,QAAhB;AAHE,UAKbM,YALa,CAAf;;AAQA,UAAID,MAAJ,EAAY;AACV,aAAKW,OAAL,CAAaC,GAAb,CAAiBlB,EAAE,IAAI,SAAvB,EAAkC;AAChCmB,UAAAA,QAAQ,EAAEb,MADsB;AAEhCc,UAAAA,MAAM,EAAEnB,QAAQ,CAACmB;AAFe,SAAlC;AAKA,cAAMd,MAAM,CAACe,KAAP,EAAN;AAEA,cAA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AAEA,WAAO,CACLD,UAAU,IAAIV,OAAO,CAACY,GAAR,CAAYC,QAAZ,KAAyB,YAAvC,GACIC,qCAAqC,EADzC,GAEIC,8CAA8C,EAH7C,EAIL,KAAKC,QAAL,IACEC,iCAAiC,CAAC;AAChCC,MAAAA,UAAU,EAAE,KAAKA;AADe,KAAD,CAL9B,EAQL,KAAKC,SAAL,IACEF,iCAAiC,CAAC;AAChCC,MAAAA,UAAU,EAAE,KAAKE;AADe,KAAD,CAT9B,EAYL,IAAIX,cAAc,CAACrB,OAAf,IAA0B,EAA9B,CAZK,EAaLiC,MAbK,CAaEC,OAbF,CAAP;AAcD;;;;AAtIDC,YADCC,QAAQ,CAAC,eAAD,uFACT;;AAGAD,YADCE,MAAM,8BACWC,oDAAlB;;AAgBAH,YADCC,QAAQ,CAAC,UAAD,mFACT;;AAGAD,YADCC,QAAQ,CAAC,WAAD,oFACT;;AAGAD,YADCE,MAAM,8BACME,8DAAb;;AAGAJ,YADCE,MAAM,CAACG,UAAD,qFACP;;AAGAL,YADCE,MAAM,CAACI,WAAD,sFACP;;AAjCW5D,aAAa,eADzB6D,OAAO,KACK7D,cAAA;;;;"}
|
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
import type { Config } from "apollo-server-core";
|
|
2
|
-
|
|
1
|
+
import type { ApolloServerBase, Config, ApolloServerPluginLandingPageGraphQLPlaygroundOptions } from "apollo-server-core";
|
|
2
|
+
export declare type ApolloMiddlewareOptions = Record<string, any>;
|
|
3
3
|
export declare type ApolloServer = ApolloServerBase & {
|
|
4
|
-
applyMiddleware(settings:
|
|
4
|
+
applyMiddleware(settings: ApolloMiddlewareOptions): any;
|
|
5
5
|
};
|
|
6
|
-
export
|
|
6
|
+
export declare type ApolloCustomServerCB = (config: ApolloConfig) => ApolloServer;
|
|
7
|
+
export declare type ApolloConfig = Config;
|
|
8
|
+
export interface ApolloSettings extends ApolloConfig {
|
|
7
9
|
path: string;
|
|
8
|
-
server?:
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
server?: ApolloCustomServerCB;
|
|
11
|
+
playground?: boolean | ApolloServerPluginLandingPageGraphQLPlaygroundOptions;
|
|
12
|
+
serverRegistration?: ApolloMiddlewareOptions;
|
|
13
|
+
middlewareOptions?: ApolloMiddlewareOptions;
|
|
11
14
|
}
|
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
import { Logger } from "@tsed/
|
|
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
8
|
platformName: string;
|
|
7
|
-
private httpPort;
|
|
8
|
-
private httpsPort;
|
|
9
|
-
private app;
|
|
10
|
-
private httpServer;
|
|
11
|
-
private httpsServer;
|
|
12
9
|
protected logger: Logger;
|
|
13
10
|
/**
|
|
14
11
|
*
|
|
@@ -16,15 +13,20 @@ export declare class ApolloService {
|
|
|
16
13
|
* @private
|
|
17
14
|
*/
|
|
18
15
|
protected servers: Map<string, {
|
|
19
|
-
instance:
|
|
16
|
+
instance: ApolloServerBase;
|
|
20
17
|
schema: GraphQLSchema | undefined;
|
|
21
18
|
}>;
|
|
19
|
+
private httpPort;
|
|
20
|
+
private httpsPort;
|
|
21
|
+
private app;
|
|
22
|
+
private httpServer;
|
|
23
|
+
private httpsServer;
|
|
22
24
|
createServer(id: string, settings: ApolloSettings): Promise<any>;
|
|
23
25
|
/**
|
|
24
26
|
* Get an instance of ApolloServer from his id
|
|
25
27
|
* @returns ApolloServer
|
|
26
28
|
*/
|
|
27
|
-
get(id?: string):
|
|
29
|
+
get(id?: string): ApolloServerBase | undefined;
|
|
28
30
|
/**
|
|
29
31
|
* Get an instance of GraphQL schema from his id
|
|
30
32
|
* @returns GraphQLSchema
|
|
@@ -36,6 +38,6 @@ export declare class ApolloService {
|
|
|
36
38
|
* @returns {boolean}
|
|
37
39
|
*/
|
|
38
40
|
has(id?: string): boolean;
|
|
39
|
-
protected
|
|
40
|
-
|
|
41
|
+
protected createInstance(options: Config, server?: ApolloCustomServerCB): ApolloServer | undefined;
|
|
42
|
+
private getPlugins;
|
|
41
43
|
}
|
package/package.json
CHANGED
|
@@ -1,29 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsed/apollo",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.86.0",
|
|
4
4
|
"description": "Apollo package for Ts.ED framework",
|
|
5
|
+
"source": "./src/index.ts",
|
|
5
6
|
"main": "./lib/index.js",
|
|
6
7
|
"typings": "./lib/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
"require": "./lib/index.js",
|
|
10
|
+
"default": "./lib/index.modern.js"
|
|
11
|
+
},
|
|
7
12
|
"scripts": {
|
|
8
|
-
"build": "
|
|
9
|
-
"
|
|
10
|
-
"test": "cross-env NODE_ENV=test nyc mocha"
|
|
13
|
+
"build": "microbundle --target node --no-compress --format modern,cjs --tsconfig ./tsconfig.compile.json",
|
|
14
|
+
"test": "cross-env NODE_ENV=test jest"
|
|
11
15
|
},
|
|
12
16
|
"dependencies": {
|
|
13
17
|
"tslib": "2.2.0"
|
|
14
18
|
},
|
|
15
19
|
"private": false,
|
|
16
20
|
"devDependencies": {
|
|
17
|
-
"@tsed/common": "6.
|
|
18
|
-
"@tsed/core": "6.
|
|
21
|
+
"@tsed/common": "6.86.0",
|
|
22
|
+
"@tsed/core": "6.86.0",
|
|
23
|
+
"@tsed/di": "6.86.0",
|
|
24
|
+
"@tsed/logger": "^6.0.0",
|
|
19
25
|
"@types/graphql": "14.5.0",
|
|
20
|
-
"apollo-
|
|
21
|
-
"apollo-
|
|
22
|
-
"
|
|
26
|
+
"apollo-datasource": "^3.2.0",
|
|
27
|
+
"apollo-datasource-rest": "^3.3.0",
|
|
28
|
+
"apollo-server-core": "^3.4.0",
|
|
29
|
+
"apollo-server-express": "^3.4.0",
|
|
30
|
+
"apollo-server-koa": "^3.4.0",
|
|
31
|
+
"apollo-server-testing": "^2.25.2",
|
|
32
|
+
"graphql": "15.7.2"
|
|
23
33
|
},
|
|
24
34
|
"peerDependencies": {
|
|
25
|
-
"
|
|
26
|
-
"
|
|
35
|
+
"@tsed/common": "^6.86.0",
|
|
36
|
+
"@tsed/core": "^6.86.0",
|
|
37
|
+
"@tsed/di": "^6.86.0",
|
|
38
|
+
"@tsed/logger": ">6.0.0",
|
|
39
|
+
"apollo-datasource": ">=3.0.0",
|
|
40
|
+
"apollo-datasource-rest": ">=3.0.0",
|
|
41
|
+
"apollo-server-core": ">=3.0.0",
|
|
42
|
+
"graphql": ">15.0.0"
|
|
27
43
|
},
|
|
28
44
|
"repository": "https://github.com/tsedio/tsed",
|
|
29
45
|
"bugs": {
|
package/.mocharc.js
DELETED
package/lib/ApolloModule.js
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
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
|
-
tslib_1.__decorate([
|
|
37
|
-
di_1.Inject(),
|
|
38
|
-
tslib_1.__metadata("design:type", common_1.Logger)
|
|
39
|
-
], ApolloModule.prototype, "logger", void 0);
|
|
40
|
-
tslib_1.__decorate([
|
|
41
|
-
di_1.Inject(),
|
|
42
|
-
tslib_1.__metadata("design:type", ApolloService_1.ApolloService)
|
|
43
|
-
], ApolloModule.prototype, "service", void 0);
|
|
44
|
-
tslib_1.__decorate([
|
|
45
|
-
di_1.Configuration(),
|
|
46
|
-
tslib_1.__metadata("design:type", Object)
|
|
47
|
-
], ApolloModule.prototype, "configuration", void 0);
|
|
48
|
-
ApolloModule = tslib_1.__decorate([
|
|
49
|
-
di_1.Module()
|
|
50
|
-
], ApolloModule);
|
|
51
|
-
exports.ApolloModule = ApolloModule;
|
|
52
|
-
//# sourceMappingURL=ApolloModule.js.map
|
package/lib/ApolloModule.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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,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S,eAAM;4CAAC;AAGzB;IADC,WAAM,EAAE;sCACU,6BAAa;6CAAC;AAGjC;IADC,kBAAa,EAAE;;mDACuB;AAR5B,YAAY;IADxB,WAAM,EAAE;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"]}
|
|
@@ -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,118 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ApolloService = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const common_1 = require("@tsed/common");
|
|
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, installSubscriptionHandlers, serverRegistration = {}, server: customServer, ...serverSettings } = settings;
|
|
21
|
-
this.logger.info(`Create server with Apollo for: ${id}`);
|
|
22
|
-
this.logger.debug(`options: ${JSON.stringify({ path })}`);
|
|
23
|
-
const server = this.createInstance(serverSettings, customServer);
|
|
24
|
-
if (server) {
|
|
25
|
-
this.servers.set(id || "default", {
|
|
26
|
-
instance: server,
|
|
27
|
-
schema: settings.schema
|
|
28
|
-
});
|
|
29
|
-
return this.bindServer(server, settings);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
catch (err) {
|
|
33
|
-
/* istanbul ignore next */
|
|
34
|
-
this.logger.error(err);
|
|
35
|
-
/* istanbul ignore next */
|
|
36
|
-
process.exit(-1);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Get an instance of ApolloServer from his id
|
|
41
|
-
* @returns ApolloServer
|
|
42
|
-
*/
|
|
43
|
-
get(id = "default") {
|
|
44
|
-
var _a;
|
|
45
|
-
return (_a = this.servers.get(id)) === null || _a === void 0 ? void 0 : _a.instance;
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Get an instance of GraphQL schema from his id
|
|
49
|
-
* @returns GraphQLSchema
|
|
50
|
-
*/
|
|
51
|
-
getSchema(id = "default") {
|
|
52
|
-
var _a;
|
|
53
|
-
return (_a = this.servers.get(id)) === null || _a === void 0 ? void 0 : _a.schema;
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
*
|
|
57
|
-
* @param {string} id
|
|
58
|
-
* @returns {boolean}
|
|
59
|
-
*/
|
|
60
|
-
has(id = "default") {
|
|
61
|
-
return this.servers.has(id);
|
|
62
|
-
}
|
|
63
|
-
async bindServer(server, options) {
|
|
64
|
-
await server.applyMiddleware({ path: options.path, ...options.serverRegistration, app: this.app.raw });
|
|
65
|
-
if (options.installSubscriptionHandlers && (this.httpPort || this.httpsPort)) {
|
|
66
|
-
this.httpPort && server.installSubscriptionHandlers(this.httpServer);
|
|
67
|
-
this.httpPort && server.installSubscriptionHandlers(this.httpsServer);
|
|
68
|
-
}
|
|
69
|
-
return server;
|
|
70
|
-
}
|
|
71
|
-
createInstance(options, server) {
|
|
72
|
-
// istanbul ignore next
|
|
73
|
-
if (server) {
|
|
74
|
-
return server(options);
|
|
75
|
-
}
|
|
76
|
-
// istanbul ignore next
|
|
77
|
-
try {
|
|
78
|
-
const Server = require(`apollo-server-${this.platformName || "express"}`).ApolloServer;
|
|
79
|
-
return new Server(options);
|
|
80
|
-
}
|
|
81
|
-
catch (er) {
|
|
82
|
-
this.logger.error(`Platform "${this.platformName}" not supported by @tsed/apollo`);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
tslib_1.__decorate([
|
|
87
|
-
common_1.Constant("PLATFORM_NAME"),
|
|
88
|
-
tslib_1.__metadata("design:type", String)
|
|
89
|
-
], ApolloService.prototype, "platformName", void 0);
|
|
90
|
-
tslib_1.__decorate([
|
|
91
|
-
common_1.Constant("httpPort"),
|
|
92
|
-
tslib_1.__metadata("design:type", Object)
|
|
93
|
-
], ApolloService.prototype, "httpPort", void 0);
|
|
94
|
-
tslib_1.__decorate([
|
|
95
|
-
common_1.Constant("httpsPort"),
|
|
96
|
-
tslib_1.__metadata("design:type", Object)
|
|
97
|
-
], ApolloService.prototype, "httpsPort", void 0);
|
|
98
|
-
tslib_1.__decorate([
|
|
99
|
-
common_1.Inject(),
|
|
100
|
-
tslib_1.__metadata("design:type", common_1.PlatformApplication)
|
|
101
|
-
], ApolloService.prototype, "app", void 0);
|
|
102
|
-
tslib_1.__decorate([
|
|
103
|
-
common_1.Inject(common_1.HttpServer),
|
|
104
|
-
tslib_1.__metadata("design:type", Object)
|
|
105
|
-
], ApolloService.prototype, "httpServer", void 0);
|
|
106
|
-
tslib_1.__decorate([
|
|
107
|
-
common_1.Inject(common_1.HttpsServer),
|
|
108
|
-
tslib_1.__metadata("design:type", Object)
|
|
109
|
-
], ApolloService.prototype, "httpsServer", void 0);
|
|
110
|
-
tslib_1.__decorate([
|
|
111
|
-
common_1.Inject(),
|
|
112
|
-
tslib_1.__metadata("design:type", common_1.Logger)
|
|
113
|
-
], ApolloService.prototype, "logger", void 0);
|
|
114
|
-
ApolloService = tslib_1.__decorate([
|
|
115
|
-
common_1.Service()
|
|
116
|
-
], ApolloService);
|
|
117
|
-
exports.ApolloService = ApolloService;
|
|
118
|
-
//# sourceMappingURL=ApolloService.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ApolloService.js","sourceRoot":"","sources":["../../src/services/ApolloService.ts"],"names":[],"mappings":";;;;AAAA,yCAA6G;AAM7G,IAAa,aAAa,GAA1B,MAAa,aAAa;IAA1B;QAsBE;;;;WAIG;QACO,YAAO,GAMb,IAAI,GAAG,EAAE,CAAC;IAkFhB,CAAC;IAhFC,KAAK,CAAC,YAAY,CAAC,EAAU,EAAE,QAAwB;QACrD,IAAI,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE;YAChB,OAAO,IAAI,CAAC,GAAG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ADC,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 protected 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"]}
|