@tsed/apollo 7.22.2 → 7.23.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 +3 -3
- package/lib/cjs/constants/constants.js +5 -0
- package/lib/cjs/constants/constants.js.map +1 -0
- package/lib/cjs/decorators/dataSource.js +69 -0
- package/lib/cjs/decorators/dataSource.js.map +1 -0
- package/lib/cjs/index.js +2 -0
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/services/ApolloService.js +63 -33
- package/lib/cjs/services/ApolloService.js.map +1 -1
- package/lib/esm/constants/constants.js +2 -0
- package/lib/esm/constants/constants.js.map +1 -0
- package/lib/esm/decorators/dataSource.js +65 -0
- package/lib/esm/decorators/dataSource.js.map +1 -0
- package/lib/esm/index.js +2 -0
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/services/ApolloService.js +64 -34
- package/lib/esm/services/ApolloService.js.map +1 -1
- package/lib/tsconfig.esm.tsbuildinfo +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/types/constants/constants.d.ts +1 -0
- package/lib/types/decorators/dataSource.d.ts +56 -0
- package/lib/types/index.d.ts +2 -0
- package/lib/types/services/ApolloService.d.ts +9 -1
- package/package.json +9 -9
package/jest.config.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/constants/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,qBAAqB,GAAG,qBAAqB,CAAC"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DataSourceService = exports.DataSource = void 0;
|
|
4
|
+
const core_1 = require("@tsed/core");
|
|
5
|
+
const di_1 = require("@tsed/di");
|
|
6
|
+
const constants_1 = require("../constants/constants");
|
|
7
|
+
/**
|
|
8
|
+
* Create a new injectable DataSource.
|
|
9
|
+
*
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { DataSourceService } from "@tsed/graphql";
|
|
12
|
+
* import { RESTDataSource } from 'apollo-datasource-rest';
|
|
13
|
+
*
|
|
14
|
+
* @DataSource()
|
|
15
|
+
* export class MyDataSource extends RESTDataSource {
|
|
16
|
+
* constructor() {
|
|
17
|
+
* super();
|
|
18
|
+
* this.baseURL = 'https://awesome-api.example.com';
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* willSendRequest(request) {
|
|
22
|
+
* request.headers.set('Authorization', this.context.token);
|
|
23
|
+
* }
|
|
24
|
+
*
|
|
25
|
+
* getMyData(id: string) {
|
|
26
|
+
* return this.get(`/v1/my_rest_data/${id}`);
|
|
27
|
+
* }
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @decorator
|
|
32
|
+
* @graphql
|
|
33
|
+
*/
|
|
34
|
+
function DataSource(name) {
|
|
35
|
+
return (0, core_1.useDecorators)((0, di_1.Injectable)({
|
|
36
|
+
type: constants_1.DATASOURCES_PROVIDERS,
|
|
37
|
+
scope: di_1.ProviderScope.INSTANCE
|
|
38
|
+
}), (0, core_1.StoreSet)(constants_1.DATASOURCES_PROVIDERS, { name }));
|
|
39
|
+
}
|
|
40
|
+
exports.DataSource = DataSource;
|
|
41
|
+
/**
|
|
42
|
+
* Create a new injectable DataSource.
|
|
43
|
+
*
|
|
44
|
+
* ```typescript
|
|
45
|
+
* import { DataSourceService } from "@tsed/graphql";
|
|
46
|
+
* import { RESTDataSource } from 'apollo-datasource-rest';
|
|
47
|
+
*
|
|
48
|
+
* @DataSource()
|
|
49
|
+
* export class MyDataSource extends RESTDataSource {
|
|
50
|
+
* constructor() {
|
|
51
|
+
* super();
|
|
52
|
+
* this.baseURL = 'https://awesome-api.example.com';
|
|
53
|
+
* }
|
|
54
|
+
*
|
|
55
|
+
* willSendRequest(request) {
|
|
56
|
+
* request.headers.set('Authorization', this.context.token);
|
|
57
|
+
* }
|
|
58
|
+
*
|
|
59
|
+
* getMyData(id: string) {
|
|
60
|
+
* return this.get(`/v1/my_rest_data/${id}`);
|
|
61
|
+
* }
|
|
62
|
+
* }
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* @decorator
|
|
66
|
+
* @graphql
|
|
67
|
+
*/
|
|
68
|
+
exports.DataSourceService = DataSource;
|
|
69
|
+
//# sourceMappingURL=dataSource.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dataSource.js","sourceRoot":"","sources":["../../../src/decorators/dataSource.ts"],"names":[],"mappings":";;;AAAA,qCAAmD;AACnD,iCAAmD;AACnD,sDAA6D;AAE7D;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,SAAgB,UAAU,CAAC,IAAa;IACtC,OAAO,IAAA,oBAAa,EAClB,IAAA,eAAU,EAAC;QACT,IAAI,EAAE,iCAAqB;QAC3B,KAAK,EAAE,kBAAa,CAAC,QAAQ;KAC9B,CAAC,EACF,IAAA,eAAQ,EAAC,iCAAqB,EAAE,EAAC,IAAI,EAAC,CAAC,CACxC,CAAC;AACJ,CAAC;AARD,gCAQC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACU,QAAA,iBAAiB,GAAG,UAAU,CAAC"}
|
package/lib/cjs/index.js
CHANGED
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const tslib_1 = require("tslib");
|
|
7
7
|
tslib_1.__exportStar(require("./ApolloModule"), exports);
|
|
8
|
+
tslib_1.__exportStar(require("./constants/constants"), exports);
|
|
9
|
+
tslib_1.__exportStar(require("./decorators/dataSource"), exports);
|
|
8
10
|
tslib_1.__exportStar(require("./interfaces/ApolloSettings"), exports);
|
|
9
11
|
tslib_1.__exportStar(require("./services/ApolloService"), exports);
|
|
10
12
|
//# sourceMappingURL=index.js.map
|
package/lib/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH,yDAA+B;AAC/B,sEAA4C;AAC5C,mEAAyC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH,yDAA+B;AAC/B,gEAAsC;AACtC,kEAAwC;AACxC,sEAA4C;AAC5C,mEAAyC"}
|
|
@@ -3,12 +3,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.ApolloService = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const common_1 = require("@tsed/common");
|
|
6
|
+
const core_1 = require("@tsed/core");
|
|
6
7
|
const di_1 = require("@tsed/di");
|
|
7
8
|
const logger_1 = require("@tsed/logger");
|
|
8
9
|
const apollo_server_core_1 = require("apollo-server-core");
|
|
9
10
|
const http_1 = tslib_1.__importDefault(require("http"));
|
|
10
11
|
const https_1 = tslib_1.__importDefault(require("https"));
|
|
12
|
+
const constants_1 = require("../constants/constants");
|
|
11
13
|
let ApolloService = class ApolloService {
|
|
14
|
+
dataSources;
|
|
12
15
|
platformName;
|
|
13
16
|
logger;
|
|
14
17
|
/**
|
|
@@ -20,42 +23,45 @@ let ApolloService = class ApolloService {
|
|
|
20
23
|
app;
|
|
21
24
|
httpServer;
|
|
22
25
|
httpsServer;
|
|
26
|
+
constructor(dataSources) {
|
|
27
|
+
this.dataSources = dataSources;
|
|
28
|
+
}
|
|
23
29
|
async createServer(id, settings) {
|
|
24
|
-
if (this.has(id)) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
30
|
+
if (!this.has(id)) {
|
|
31
|
+
try {
|
|
32
|
+
const { dataSources, path, middlewareOptions = {}, server: customServer, ...config } = settings;
|
|
33
|
+
this.logger.info(`Create server with Apollo for: ${id}`);
|
|
34
|
+
this.logger.debug(`options: ${JSON.stringify({ path })}`);
|
|
35
|
+
const server = await this.createInstance({
|
|
36
|
+
...config,
|
|
37
|
+
plugins: this.getPlugins(settings),
|
|
38
|
+
dataSources: this.createDataSources(dataSources)
|
|
39
|
+
}, customServer);
|
|
40
|
+
if (server) {
|
|
41
|
+
this.servers.set(id || "default", {
|
|
42
|
+
instance: server,
|
|
43
|
+
schema: settings.schema
|
|
44
|
+
});
|
|
45
|
+
await server.start();
|
|
46
|
+
const middleware = server.getMiddleware({
|
|
47
|
+
path: settings.path,
|
|
48
|
+
...middlewareOptions
|
|
49
|
+
});
|
|
50
|
+
this.app.use(middleware);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
catch (er) {
|
|
54
|
+
this.logger.error({
|
|
55
|
+
event: "APOLLO_BOOTSTRAP_ERROR",
|
|
56
|
+
error_name: er.name,
|
|
57
|
+
message: er.message,
|
|
58
|
+
stack: er.stack
|
|
44
59
|
});
|
|
45
|
-
|
|
46
|
-
|
|
60
|
+
/* istanbul ignore next */
|
|
61
|
+
process.exit(-1);
|
|
47
62
|
}
|
|
48
63
|
}
|
|
49
|
-
|
|
50
|
-
this.logger.error({
|
|
51
|
-
event: "APOLLO_BOOTSTRAP_ERROR",
|
|
52
|
-
error_name: er.name,
|
|
53
|
-
message: er.message,
|
|
54
|
-
stack: er.stack
|
|
55
|
-
});
|
|
56
|
-
/* istanbul ignore next */
|
|
57
|
-
process.exit(-1);
|
|
58
|
-
}
|
|
64
|
+
return this.get(id);
|
|
59
65
|
}
|
|
60
66
|
/**
|
|
61
67
|
* Get an instance of ApolloServer from his id
|
|
@@ -96,6 +102,28 @@ let ApolloService = class ApolloService {
|
|
|
96
102
|
const Server = await importServer();
|
|
97
103
|
return new Server(options);
|
|
98
104
|
}
|
|
105
|
+
/**
|
|
106
|
+
* create a new dataSources function to use with apollo server config
|
|
107
|
+
* @param dataSources
|
|
108
|
+
* @param serverConfigSources
|
|
109
|
+
*/
|
|
110
|
+
createDataSources(dataSources) {
|
|
111
|
+
const dataSourcesHash = this.dataSources.reduce((map, instance) => {
|
|
112
|
+
const klass = (0, core_1.classOf)(instance);
|
|
113
|
+
const store = core_1.Store.from(klass);
|
|
114
|
+
let { name } = store.get(constants_1.DATASOURCES_PROVIDERS);
|
|
115
|
+
name = name || (0, core_1.nameOf)(klass);
|
|
116
|
+
const sourceName = `${name[0].toLowerCase()}${name.slice(1)}`;
|
|
117
|
+
map[sourceName] = instance;
|
|
118
|
+
return map;
|
|
119
|
+
}, {});
|
|
120
|
+
return () => {
|
|
121
|
+
return {
|
|
122
|
+
...dataSourcesHash,
|
|
123
|
+
...(dataSources ? dataSources() : {})
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
}
|
|
99
127
|
getPlugins(serverSettings) {
|
|
100
128
|
const playground = serverSettings.playground || (serverSettings.playground === undefined && process.env.NODE_ENV !== "production");
|
|
101
129
|
return [
|
|
@@ -133,7 +161,9 @@ tslib_1.__decorate([
|
|
|
133
161
|
tslib_1.__metadata("design:type", Object)
|
|
134
162
|
], ApolloService.prototype, "httpsServer", void 0);
|
|
135
163
|
ApolloService = tslib_1.__decorate([
|
|
136
|
-
(0, di_1.Service)()
|
|
164
|
+
(0, di_1.Service)(),
|
|
165
|
+
tslib_1.__param(0, (0, di_1.Inject)(constants_1.DATASOURCES_PROVIDERS)),
|
|
166
|
+
tslib_1.__metadata("design:paramtypes", [Array])
|
|
137
167
|
], ApolloService);
|
|
138
168
|
exports.ApolloService = ApolloService;
|
|
139
169
|
//# sourceMappingURL=ApolloService.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ApolloService.js","sourceRoot":"","sources":["../../../src/services/ApolloService.ts"],"names":[],"mappings":";;;;A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M,qBAAqB,GAAG,qBAAqB,CAAC"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { StoreSet, useDecorators } from "@tsed/core";
|
|
2
|
+
import { Injectable, ProviderScope } from "@tsed/di";
|
|
3
|
+
import { DATASOURCES_PROVIDERS } from "../constants/constants.js";
|
|
4
|
+
/**
|
|
5
|
+
* Create a new injectable DataSource.
|
|
6
|
+
*
|
|
7
|
+
* ```typescript
|
|
8
|
+
* import { DataSourceService } from "@tsed/graphql";
|
|
9
|
+
* import { RESTDataSource } from 'apollo-datasource-rest';
|
|
10
|
+
*
|
|
11
|
+
* @DataSource()
|
|
12
|
+
* export class MyDataSource extends RESTDataSource {
|
|
13
|
+
* constructor() {
|
|
14
|
+
* super();
|
|
15
|
+
* this.baseURL = 'https://awesome-api.example.com';
|
|
16
|
+
* }
|
|
17
|
+
*
|
|
18
|
+
* willSendRequest(request) {
|
|
19
|
+
* request.headers.set('Authorization', this.context.token);
|
|
20
|
+
* }
|
|
21
|
+
*
|
|
22
|
+
* getMyData(id: string) {
|
|
23
|
+
* return this.get(`/v1/my_rest_data/${id}`);
|
|
24
|
+
* }
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @decorator
|
|
29
|
+
* @graphql
|
|
30
|
+
*/
|
|
31
|
+
export function DataSource(name) {
|
|
32
|
+
return useDecorators(Injectable({
|
|
33
|
+
type: DATASOURCES_PROVIDERS,
|
|
34
|
+
scope: ProviderScope.INSTANCE
|
|
35
|
+
}), StoreSet(DATASOURCES_PROVIDERS, { name }));
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Create a new injectable DataSource.
|
|
39
|
+
*
|
|
40
|
+
* ```typescript
|
|
41
|
+
* import { DataSourceService } from "@tsed/graphql";
|
|
42
|
+
* import { RESTDataSource } from 'apollo-datasource-rest';
|
|
43
|
+
*
|
|
44
|
+
* @DataSource()
|
|
45
|
+
* export class MyDataSource extends RESTDataSource {
|
|
46
|
+
* constructor() {
|
|
47
|
+
* super();
|
|
48
|
+
* this.baseURL = 'https://awesome-api.example.com';
|
|
49
|
+
* }
|
|
50
|
+
*
|
|
51
|
+
* willSendRequest(request) {
|
|
52
|
+
* request.headers.set('Authorization', this.context.token);
|
|
53
|
+
* }
|
|
54
|
+
*
|
|
55
|
+
* getMyData(id: string) {
|
|
56
|
+
* return this.get(`/v1/my_rest_data/${id}`);
|
|
57
|
+
* }
|
|
58
|
+
* }
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* @decorator
|
|
62
|
+
* @graphql
|
|
63
|
+
*/
|
|
64
|
+
export const DataSourceService = DataSource;
|
|
65
|
+
//# sourceMappingURL=dataSource.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dataSource.js","sourceRoot":"","sources":["../../../src/decorators/dataSource.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAE,aAAa,EAAC,MAAM,YAAY,CAAC;AACnD,OAAO,EAAC,UAAU,EAAE,aAAa,EAAC,MAAM,UAAU,CAAC;AACnD,OAAO,EAAC,qBAAqB,EAAC,MAAM,wBAAwB,CAAC;AAE7D;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,UAAU,UAAU,CAAC,IAAa;IACtC,OAAO,aAAa,CAClB,UAAU,CAAC;QACT,IAAI,EAAE,qBAAqB;QAC3B,KAAK,EAAE,aAAa,CAAC,QAAQ;KAC9B,CAAC,EACF,QAAQ,CAAC,qBAAqB,EAAE,EAAC,IAAI,EAAC,CAAC,CACxC,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,UAAU,CAAC"}
|
package/lib/esm/index.js
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
* @file Automatically generated by barrelsby.
|
|
3
3
|
*/
|
|
4
4
|
export * from "./ApolloModule.js";
|
|
5
|
+
export * from "./constants/constants.js";
|
|
6
|
+
export * from "./decorators/dataSource.js";
|
|
5
7
|
export * from "./interfaces/ApolloSettings.js";
|
|
6
8
|
export * from "./services/ApolloService.js";
|
|
7
9
|
//# sourceMappingURL=index.js.map
|
package/lib/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,gBAAgB,CAAC;AAC/B,cAAc,6BAA6B,CAAC;AAC5C,cAAc,0BAA0B,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,gBAAgB,CAAC;AAC/B,cAAc,uBAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC;AACxC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,0BAA0B,CAAC"}
|
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
import { __decorate, __metadata } from "tslib";
|
|
1
|
+
import { __decorate, __metadata, __param } from "tslib";
|
|
2
2
|
import { PlatformApplication } from "@tsed/common";
|
|
3
|
+
import { classOf, nameOf, Store } from "@tsed/core";
|
|
3
4
|
import { Constant, Inject, Service } from "@tsed/di";
|
|
4
5
|
import { Logger } from "@tsed/logger";
|
|
5
6
|
import { ApolloServerPluginDrainHttpServer, ApolloServerPluginLandingPageDisabled, ApolloServerPluginLandingPageGraphQLPlayground } from "apollo-server-core";
|
|
6
7
|
import Http from "http";
|
|
7
8
|
import Https from "https";
|
|
9
|
+
import { DATASOURCES_PROVIDERS } from "../constants/constants.js";
|
|
8
10
|
let ApolloService = class ApolloService {
|
|
11
|
+
dataSources;
|
|
9
12
|
platformName;
|
|
10
13
|
logger;
|
|
11
14
|
/**
|
|
@@ -17,42 +20,45 @@ let ApolloService = class ApolloService {
|
|
|
17
20
|
app;
|
|
18
21
|
httpServer;
|
|
19
22
|
httpsServer;
|
|
23
|
+
constructor(dataSources) {
|
|
24
|
+
this.dataSources = dataSources;
|
|
25
|
+
}
|
|
20
26
|
async createServer(id, settings) {
|
|
21
|
-
if (this.has(id)) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
27
|
+
if (!this.has(id)) {
|
|
28
|
+
try {
|
|
29
|
+
const { dataSources, path, middlewareOptions = {}, server: customServer, ...config } = settings;
|
|
30
|
+
this.logger.info(`Create server with Apollo for: ${id}`);
|
|
31
|
+
this.logger.debug(`options: ${JSON.stringify({ path })}`);
|
|
32
|
+
const server = await this.createInstance({
|
|
33
|
+
...config,
|
|
34
|
+
plugins: this.getPlugins(settings),
|
|
35
|
+
dataSources: this.createDataSources(dataSources)
|
|
36
|
+
}, customServer);
|
|
37
|
+
if (server) {
|
|
38
|
+
this.servers.set(id || "default", {
|
|
39
|
+
instance: server,
|
|
40
|
+
schema: settings.schema
|
|
41
|
+
});
|
|
42
|
+
await server.start();
|
|
43
|
+
const middleware = server.getMiddleware({
|
|
44
|
+
path: settings.path,
|
|
45
|
+
...middlewareOptions
|
|
46
|
+
});
|
|
47
|
+
this.app.use(middleware);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
catch (er) {
|
|
51
|
+
this.logger.error({
|
|
52
|
+
event: "APOLLO_BOOTSTRAP_ERROR",
|
|
53
|
+
error_name: er.name,
|
|
54
|
+
message: er.message,
|
|
55
|
+
stack: er.stack
|
|
41
56
|
});
|
|
42
|
-
|
|
43
|
-
|
|
57
|
+
/* istanbul ignore next */
|
|
58
|
+
process.exit(-1);
|
|
44
59
|
}
|
|
45
60
|
}
|
|
46
|
-
|
|
47
|
-
this.logger.error({
|
|
48
|
-
event: "APOLLO_BOOTSTRAP_ERROR",
|
|
49
|
-
error_name: er.name,
|
|
50
|
-
message: er.message,
|
|
51
|
-
stack: er.stack
|
|
52
|
-
});
|
|
53
|
-
/* istanbul ignore next */
|
|
54
|
-
process.exit(-1);
|
|
55
|
-
}
|
|
61
|
+
return this.get(id);
|
|
56
62
|
}
|
|
57
63
|
/**
|
|
58
64
|
* Get an instance of ApolloServer from his id
|
|
@@ -93,6 +99,28 @@ let ApolloService = class ApolloService {
|
|
|
93
99
|
const Server = await importServer();
|
|
94
100
|
return new Server(options);
|
|
95
101
|
}
|
|
102
|
+
/**
|
|
103
|
+
* create a new dataSources function to use with apollo server config
|
|
104
|
+
* @param dataSources
|
|
105
|
+
* @param serverConfigSources
|
|
106
|
+
*/
|
|
107
|
+
createDataSources(dataSources) {
|
|
108
|
+
const dataSourcesHash = this.dataSources.reduce((map, instance) => {
|
|
109
|
+
const klass = classOf(instance);
|
|
110
|
+
const store = Store.from(klass);
|
|
111
|
+
let { name } = store.get(DATASOURCES_PROVIDERS);
|
|
112
|
+
name = name || nameOf(klass);
|
|
113
|
+
const sourceName = `${name[0].toLowerCase()}${name.slice(1)}`;
|
|
114
|
+
map[sourceName] = instance;
|
|
115
|
+
return map;
|
|
116
|
+
}, {});
|
|
117
|
+
return () => {
|
|
118
|
+
return {
|
|
119
|
+
...dataSourcesHash,
|
|
120
|
+
...(dataSources ? dataSources() : {})
|
|
121
|
+
};
|
|
122
|
+
};
|
|
123
|
+
}
|
|
96
124
|
getPlugins(serverSettings) {
|
|
97
125
|
const playground = serverSettings.playground || (serverSettings.playground === undefined && process.env.NODE_ENV !== "production");
|
|
98
126
|
return [
|
|
@@ -130,7 +158,9 @@ __decorate([
|
|
|
130
158
|
__metadata("design:type", Object)
|
|
131
159
|
], ApolloService.prototype, "httpsServer", void 0);
|
|
132
160
|
ApolloService = __decorate([
|
|
133
|
-
Service()
|
|
161
|
+
Service(),
|
|
162
|
+
__param(0, Inject(DATASOURCES_PROVIDERS)),
|
|
163
|
+
__metadata("design:paramtypes", [Array])
|
|
134
164
|
], ApolloService);
|
|
135
165
|
export { ApolloService };
|
|
136
166
|
//# sourceMappingURL=ApolloService.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ApolloService.js","sourceRoot":"","sources":["../../../src/services/ApolloService.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,mBAAmB,EAAC,MAAM,cAAc,CAAC;AACjD,OAAO,EAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAC,MAAM,UAAU,CAAC;AACnD,OAAO,EAAC,MAAM,EAAC,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"ApolloService.js","sourceRoot":"","sources":["../../../src/services/ApolloService.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,mBAAmB,EAAC,MAAM,cAAc,CAAC;AACjD,OAAO,EAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAC,MAAM,YAAY,CAAC;AAClD,OAAO,EAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAC,MAAM,UAAU,CAAC;AACnD,OAAO,EAAC,MAAM,EAAC,MAAM,cAAc,CAAC;AAGpC,OAAO,EAEL,iCAAiC,EACjC,qCAAqC,EACrC,8CAA8C,EAC/C,MAAM,oBAAoB,CAAC;AAE5B,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAC,qBAAqB,EAAC,MAAM,wBAAwB,CAAC;AAKtD,IAAM,aAAa,GAAnB,MAAM,aAAa;IA6B6B;IA3BrD,YAAY,CAAS;IAGX,MAAM,CAAS;IAEzB;;;;OAIG;IACO,OAAO,GAMb,IAAI,GAAG,EAAE,CAAC;IAGN,GAAG,CAA2B;IAG9B,UAAU,CAAqB;IAG/B,WAAW,CAAsB;IAEzC,YAAqD,WAAkB;QAAlB,gBAAW,GAAX,WAAW,CAAO;IAAG,CAAC;IAE3E,KAAK,CAAC,YAAY,CAAC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