@common-stack/server-stack 5.0.4-alpha.7 → 5.0.5-alpha.2
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/lib/MainStackServer.cjs +222 -0
- package/lib/MainStackServer.cjs.map +1 -0
- package/lib/MainStackServer.d.ts +28 -0
- package/lib/MainStackServer.mjs +222 -0
- package/lib/MainStackServer.mjs.map +1 -0
- package/lib/{stack-server.cjs → StackServer.cjs} +1 -1
- package/lib/StackServer.cjs.map +1 -0
- package/lib/{stack-server.mjs → StackServer.mjs} +1 -1
- package/lib/StackServer.mjs.map +1 -0
- package/lib/api/remote-config.cjs +6 -0
- package/lib/api/remote-config.cjs.map +1 -0
- package/lib/api/remote-config.d.ts +6 -0
- package/lib/api/remote-config.mjs +6 -0
- package/lib/api/remote-config.mjs.map +1 -0
- package/lib/api/resolver.cjs +11 -0
- package/lib/api/resolver.cjs.map +1 -0
- package/lib/api/resolver.d.ts +10 -0
- package/lib/api/resolver.mjs +11 -0
- package/lib/api/resolver.mjs.map +1 -0
- package/lib/api/root-schema.graphqls.cjs +2 -0
- package/lib/api/root-schema.graphqls.cjs.map +1 -0
- package/lib/api/root-schema.graphqls.mjs +2 -0
- package/lib/api/root-schema.graphqls.mjs.map +1 -0
- package/lib/api/scalar.cjs +14 -0
- package/lib/api/scalar.cjs.map +1 -0
- package/lib/api/scalar.d.ts +2 -0
- package/lib/api/scalar.mjs +14 -0
- package/lib/api/scalar.mjs.map +1 -0
- package/lib/api/schema-builder.cjs +148 -0
- package/lib/api/schema-builder.cjs.map +1 -0
- package/lib/api/schema-builder.d.ts +24 -0
- package/lib/api/schema-builder.mjs +148 -0
- package/lib/api/schema-builder.mjs.map +1 -0
- package/lib/api/utils.cjs +27 -0
- package/lib/api/utils.cjs.map +1 -0
- package/lib/api/utils.d.ts +4 -0
- package/lib/api/utils.mjs +27 -0
- package/lib/api/utils.mjs.map +1 -0
- package/lib/config/env-config.cjs +4 -0
- package/lib/config/env-config.cjs.map +1 -1
- package/lib/config/env-config.d.ts +4 -0
- package/lib/config/env-config.mjs +4 -0
- package/lib/config/env-config.mjs.map +1 -1
- package/lib/connectors/connection-broker.d.ts +2 -2
- package/lib/connectors/redis-connector.d.ts +1 -1
- package/lib/index.cjs +1 -1
- package/lib/index.d.ts +2 -1
- package/lib/index.mjs +1 -1
- package/lib/interfaces/dbMigration.d.ts +8 -0
- package/lib/interfaces/index.d.ts +3 -0
- package/lib/interfaces/module-interface.d.ts +13 -0
- package/lib/middleware/cors.cjs +26 -0
- package/lib/middleware/cors.cjs.map +1 -0
- package/lib/middleware/cors.d.ts +6 -0
- package/lib/middleware/cors.mjs +26 -0
- package/lib/middleware/cors.mjs.map +1 -0
- package/lib/middleware/error.cjs +48 -0
- package/lib/middleware/error.cjs.map +1 -0
- package/lib/middleware/error.d.ts +10 -0
- package/lib/middleware/error.mjs +48 -0
- package/lib/middleware/error.mjs.map +1 -0
- package/lib/middleware/sentry.cjs +3 -0
- package/lib/middleware/sentry.cjs.map +1 -0
- package/lib/middleware/sentry.d.ts +3 -0
- package/lib/middleware/sentry.mjs +3 -0
- package/lib/middleware/sentry.mjs.map +1 -0
- package/lib/middleware/services.cjs +12 -0
- package/lib/middleware/services.cjs.map +1 -0
- package/lib/middleware/services.d.ts +2 -0
- package/lib/middleware/services.mjs +12 -0
- package/lib/middleware/services.mjs.map +1 -0
- package/lib/plugins/index.d.ts +1 -0
- package/lib/plugins/invalidateCachePlugin.cjs +29 -0
- package/lib/plugins/invalidateCachePlugin.cjs.map +1 -0
- package/lib/plugins/invalidateCachePlugin.d.ts +5 -0
- package/lib/plugins/invalidateCachePlugin.mjs +29 -0
- package/lib/plugins/invalidateCachePlugin.mjs.map +1 -0
- package/lib/servers/ExpressApp.cjs +40 -0
- package/lib/servers/ExpressApp.cjs.map +1 -0
- package/lib/servers/ExpressApp.d.ts +3 -0
- package/lib/servers/ExpressApp.mjs +40 -0
- package/lib/servers/ExpressApp.mjs.map +1 -0
- package/lib/servers/GraphqlServer.cjs +146 -0
- package/lib/servers/GraphqlServer.cjs.map +1 -0
- package/lib/servers/GraphqlServer.d.ts +18 -0
- package/lib/servers/GraphqlServer.mjs +146 -0
- package/lib/servers/GraphqlServer.mjs.map +1 -0
- package/lib/servers/GraphqlWs.cjs +95 -0
- package/lib/servers/GraphqlWs.cjs.map +1 -0
- package/lib/servers/GraphqlWs.d.ts +14 -0
- package/lib/servers/GraphqlWs.mjs +95 -0
- package/lib/servers/GraphqlWs.mjs.map +1 -0
- package/lib/servers/WebsocketMultipathUpdate.cjs +63 -0
- package/lib/servers/WebsocketMultipathUpdate.cjs.map +1 -0
- package/lib/servers/WebsocketMultipathUpdate.d.ts +19 -0
- package/lib/servers/WebsocketMultipathUpdate.mjs +63 -0
- package/lib/servers/WebsocketMultipathUpdate.mjs.map +1 -0
- package/lib/servers/mongodb-migration-update.d.ts +36 -0
- package/lib/servers/utils.d.ts +14 -0
- package/lib/utils/migrations.cjs +26 -0
- package/lib/utils/migrations.cjs.map +1 -0
- package/lib/utils/migrations.d.ts +32 -0
- package/lib/utils/migrations.mjs +26 -0
- package/lib/utils/migrations.mjs.map +1 -0
- package/package.json +40 -3
- package/lib/stack-server.cjs.map +0 -1
- package/lib/stack-server.mjs.map +0 -1
- /package/lib/{stack-server.d.ts → StackServer.d.ts} +0 -0
- /package/lib/{interface.d.ts → interfaces/moleculer.d.ts} +0 -0
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import {ApolloServer}from'apollo-server-express';import {ApolloServerPluginDrainHttpServer,ApolloServerPluginCacheControl}from'apollo-server-core';import responseCachePluginSource from'apollo-server-plugin-response-cache';import'isomorphic-fetch';import Keyv from'keyv';import KeyvRedis from'@keyv/redis';import {KeyvAdapter}from'@apollo/utils.keyvadapter';import {invalidateCachePlugin}from'../plugins/invalidateCachePlugin.mjs';import {WebSocketServer}from'ws';import {GraphqlWs}from'./GraphqlWs.mjs';import {config}from'../config/env-config.mjs';const responseCachePlugin = responseCachePluginSource.default ?? responseCachePluginSource;
|
|
2
|
+
let debug = false;
|
|
3
|
+
if ((process.env.LOG_LEVEL && process.env.LOG_LEVEL === 'trace') || process.env.LOG_LEVEL === 'debug') {
|
|
4
|
+
debug = true;
|
|
5
|
+
}
|
|
6
|
+
// @workaround as the `dataSources` not available in Subscription (websocket) Context.
|
|
7
|
+
// https://github.com/apollographql/apollo-server/issues/1526 need to revisit in Apollo-Server v3.
|
|
8
|
+
const constructDataSourcesForSubscriptions = (context, cache, dataSources) => {
|
|
9
|
+
const intializeDataSource = (instance) => {
|
|
10
|
+
instance.initialize({ context, cache });
|
|
11
|
+
};
|
|
12
|
+
// tslint:disable-next-line:forin
|
|
13
|
+
// eslint-disable-next-line guard-for-in
|
|
14
|
+
for (const prop in dataSources) {
|
|
15
|
+
intializeDataSource(dataSources[prop]);
|
|
16
|
+
}
|
|
17
|
+
return dataSources;
|
|
18
|
+
};
|
|
19
|
+
let wsServerCleanup;
|
|
20
|
+
class GraphqlServer {
|
|
21
|
+
app;
|
|
22
|
+
httpServer;
|
|
23
|
+
cache;
|
|
24
|
+
moduleService;
|
|
25
|
+
enableSubscription;
|
|
26
|
+
logger;
|
|
27
|
+
constructor(app, httpServer, cache, moduleService, enableSubscription = true) {
|
|
28
|
+
this.app = app;
|
|
29
|
+
this.httpServer = httpServer;
|
|
30
|
+
this.cache = cache;
|
|
31
|
+
this.moduleService = moduleService;
|
|
32
|
+
this.enableSubscription = enableSubscription;
|
|
33
|
+
this.logger = this.moduleService.logger.child({ className: 'GraphqlServer' });
|
|
34
|
+
if (enableSubscription) {
|
|
35
|
+
const wsServer = new WebSocketServer({
|
|
36
|
+
server: this.httpServer,
|
|
37
|
+
path: config.GRAPHQL_ENDPOINT,
|
|
38
|
+
});
|
|
39
|
+
const graphqlWs = new GraphqlWs(wsServer, this.moduleService, this.cache);
|
|
40
|
+
wsServerCleanup = graphqlWs.create();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
async initialize() {
|
|
44
|
+
this.logger.info('GraphqlServer initializing...');
|
|
45
|
+
const apolloServer = this.configureApolloServer();
|
|
46
|
+
await apolloServer.start();
|
|
47
|
+
const corsOptions = {
|
|
48
|
+
origin: [config.CLIENT_URL],
|
|
49
|
+
credentials: true,
|
|
50
|
+
};
|
|
51
|
+
apolloServer.applyMiddleware({
|
|
52
|
+
app: this.app,
|
|
53
|
+
cors: corsOptions,
|
|
54
|
+
path: config.GRAPHQL_ENDPOINT,
|
|
55
|
+
});
|
|
56
|
+
this.logger.info('GraphqlServer initialized - ', config.CLIENT_URL);
|
|
57
|
+
}
|
|
58
|
+
getUserIpAddress(req) {
|
|
59
|
+
let ip = (req?.headers['x-forwarded-for'] || '').split(',')[0] || req?.connection?.remoteAddress;
|
|
60
|
+
if (ip.substr(0, 7) === '::ffff:') {
|
|
61
|
+
ip = ip.substr(7);
|
|
62
|
+
}
|
|
63
|
+
if (ip === '::1') {
|
|
64
|
+
ip = '127.0.0.1';
|
|
65
|
+
}
|
|
66
|
+
return ip;
|
|
67
|
+
}
|
|
68
|
+
configureApolloServer() {
|
|
69
|
+
this.logger.info('-- Configuring Apollo Server --');
|
|
70
|
+
// @ts-ignore
|
|
71
|
+
const cache = new KeyvAdapter(new Keyv({ store: new KeyvRedis(this.cache.client) }), {
|
|
72
|
+
disableBatchReads: true,
|
|
73
|
+
});
|
|
74
|
+
const serverConfig = {
|
|
75
|
+
debug,
|
|
76
|
+
cache,
|
|
77
|
+
schema: this.moduleService.schema,
|
|
78
|
+
dataSources: () => this.moduleService.dataSource,
|
|
79
|
+
context: async ({ req, res, connection, }) => {
|
|
80
|
+
let context;
|
|
81
|
+
let addons = {};
|
|
82
|
+
try {
|
|
83
|
+
if (connection) {
|
|
84
|
+
context = connection.context;
|
|
85
|
+
if (!context.dataSources) {
|
|
86
|
+
addons = {
|
|
87
|
+
// @workaround for apollo server issue #1526
|
|
88
|
+
dataSources: constructDataSourcesForSubscriptions(connection.context, this.cache, this.moduleService.dataSource),
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
addons = {
|
|
93
|
+
// @workaround for apollo server issue #1526
|
|
94
|
+
dataSources: context.dataSources,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
const pureContext = await this.moduleService.createContext(req, res);
|
|
100
|
+
const contextServices = await this.moduleService.serviceContext(req, res);
|
|
101
|
+
context = {
|
|
102
|
+
...pureContext,
|
|
103
|
+
...contextServices,
|
|
104
|
+
preferences: this.moduleService.defaultPreferences,
|
|
105
|
+
// update: updateContainers,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
context.userIp = this.getUserIpAddress(req);
|
|
109
|
+
}
|
|
110
|
+
catch (err) {
|
|
111
|
+
this.logger.error('Adding context to GraphQL failed', { error: err });
|
|
112
|
+
throw err;
|
|
113
|
+
}
|
|
114
|
+
return {
|
|
115
|
+
req,
|
|
116
|
+
res,
|
|
117
|
+
...context,
|
|
118
|
+
...addons,
|
|
119
|
+
};
|
|
120
|
+
},
|
|
121
|
+
plugins: [
|
|
122
|
+
ApolloServerPluginDrainHttpServer({ httpServer: this.httpServer }),
|
|
123
|
+
ApolloServerPluginCacheControl(),
|
|
124
|
+
responseCachePlugin({
|
|
125
|
+
sessionId: ({ context }) => context?.userContext?.accountId ?? null,
|
|
126
|
+
generateCacheKey({ queryHash, operationName }, keyData) {
|
|
127
|
+
return `${operationName}:${queryHash}`;
|
|
128
|
+
},
|
|
129
|
+
}),
|
|
130
|
+
invalidateCachePlugin({ cache: this.cache }),
|
|
131
|
+
],
|
|
132
|
+
};
|
|
133
|
+
if (this.enableSubscription) {
|
|
134
|
+
serverConfig.plugins.push({
|
|
135
|
+
async serverWillStart() {
|
|
136
|
+
return {
|
|
137
|
+
drainServer: async () => {
|
|
138
|
+
await wsServerCleanup.dispose();
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
return new ApolloServer(serverConfig);
|
|
145
|
+
}
|
|
146
|
+
}export{GraphqlServer};//# sourceMappingURL=GraphqlServer.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GraphqlServer.mjs","sources":["../../src/servers/GraphqlServer.ts"],"sourcesContent":[null],"names":[],"mappings":"qiBAqBA,MAAM,mBAAmB,GAAI,yBAAiC,CAAC,OAAO,IAAI,yBAAyB,CAAC;AAGpG,IAAI,KAAK,GAAG,KAAK,CAAC;AAClB,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,SAAS,IAAI,OAAO,CAAC,GAAG,CAAC,SAAS,KAAK,OAAO,KAAK,OAAO,CAAC,GAAG,CAAC,SAAS,KAAK,OAAO,EAAE;IACnG,KAAK,GAAG,IAAI,CAAC;AACjB,CAAC;AAED;AACA;AACA,MAAM,oCAAoC,GAAG,CAAC,OAAO,EAAE,KAAK,EAAE,WAAW,KAAI;AACzE,IAAA,MAAM,mBAAmB,GAAG,CAAC,QAAQ,KAAI;QACrC,QAAQ,CAAC,UAAU,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC;AAC5C,KAAC,CAAC;;;AAGF,IAAA,KAAK,MAAM,IAAI,IAAI,WAAW,EAAE;AAC5B,QAAA,mBAAmB,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC;KAC1C;AACD,IAAA,OAAO,WAAW,CAAC;AACvB,CAAC,CAAC;AAEF,IAAI,eAAoB,CAAC;MAIZ,aAAa,CAAA;AAIV,IAAA,GAAA,CAAA;AACA,IAAA,UAAA,CAAA;AACA,IAAA,KAAA,CAAA;AACA,IAAA,aAAA,CAAA;AACA,IAAA,kBAAA,CAAA;AAPJ,IAAA,MAAM,CAAU;IAExB,WACY,CAAA,GAAY,EACZ,UAAuB,EACvB,KAAqC,EACrC,aAA6B,EAC7B,kBAAA,GAAqB,IAAI,EAAA;QAJzB,IAAG,CAAA,GAAA,GAAH,GAAG,CAA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nE,oBAAA,gBAAgB,CAAC,EACI,SAAS,EACT,aAAa,EAC4B,EAAE,OAAO,EAAA;AACnE,wBAAA,OAAO,CAAG,EAAA,aAAa,CAAI,CAAA,EAAA,SAAS,EAAE,CAAC;qBAC1C;iBACJ,CAAC;gBACF,qBAAqB,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,CAAC;AAC/C,aAAA;SACJ,CAAC;AACF,QAAA,IAAI,IAAI,CAAC,kBAAkB,EAAE;AACzB,YAAA,YAAY,CAAC,OAAO,CAAC,IAAI,CAAC;AACtB,gBAAA,MAAM,eAAe,GAAA;oBACjB,OAAO;wBACH,WAAW,EAAE,YAAW;AACpB,4BAAA,MAAM,eAAe,CAAC,OAAO,EAAE,CAAC;yBACnC;qBACJ,CAAC;iBACL;AACJ,aAAA,CAAC,CAAC;SACN;AACD,QAAA,OAAO,IAAI,YAAY,CAAC,YAAY,CAAC,CAAC;KACzC;AACJ"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
'use strict';var ws=require('graphql-ws/lib/use/ws');require('graphql');// @workaround as the `dataSources` not available in Subscription (websocket) Context.
|
|
2
|
+
// https://github.com/apollographql/apollo-server/issues/1526 need to revisit in Apollo-Server v3.
|
|
3
|
+
const constructDataSourcesForSubscriptions = (context, cache, dataSources) => {
|
|
4
|
+
const intializeDataSource = (instance) => {
|
|
5
|
+
instance.initialize({ context, cache });
|
|
6
|
+
};
|
|
7
|
+
// tslint:disable-next-line:forin
|
|
8
|
+
for (const prop in dataSources) {
|
|
9
|
+
// tslint:disable-next-line:no-console
|
|
10
|
+
intializeDataSource(dataSources[prop]);
|
|
11
|
+
}
|
|
12
|
+
return dataSources;
|
|
13
|
+
};
|
|
14
|
+
class GraphqlWs {
|
|
15
|
+
wsServer;
|
|
16
|
+
moduleService;
|
|
17
|
+
cache;
|
|
18
|
+
subscriptionServer;
|
|
19
|
+
logger;
|
|
20
|
+
constructor(wsServer, moduleService, cache) {
|
|
21
|
+
this.wsServer = wsServer;
|
|
22
|
+
this.moduleService = moduleService;
|
|
23
|
+
this.cache = cache;
|
|
24
|
+
}
|
|
25
|
+
create() {
|
|
26
|
+
this.subscriptionServer = ws.useServer({
|
|
27
|
+
schema: this.moduleService.schema,
|
|
28
|
+
// Adding a context property lets you add data to your GraphQL operation context
|
|
29
|
+
context: async (ctx) => {
|
|
30
|
+
// Returning an object here will add that information to our
|
|
31
|
+
// GraphQL context, which all of our resolvers have access to.
|
|
32
|
+
// ctx is the `graphql-ws` Context where connectionParams live
|
|
33
|
+
try {
|
|
34
|
+
const pureContext = await this.moduleService.createContext(ctx.connectionParams, null);
|
|
35
|
+
const contextServices = await this.moduleService.serviceContext(ctx.connectionParams, null);
|
|
36
|
+
const context = {
|
|
37
|
+
...contextServices,
|
|
38
|
+
...pureContext,
|
|
39
|
+
preferences: this.moduleService.defaultPreferences,
|
|
40
|
+
// update: updateContainers,
|
|
41
|
+
...ctx,
|
|
42
|
+
};
|
|
43
|
+
const addons = {
|
|
44
|
+
dataSources: constructDataSourcesForSubscriptions(context, this.cache, this.moduleService.dataSource),
|
|
45
|
+
};
|
|
46
|
+
return {
|
|
47
|
+
...context,
|
|
48
|
+
...addons,
|
|
49
|
+
...ctx,
|
|
50
|
+
...pureContext,
|
|
51
|
+
preferences: this.moduleService.defaultPreferences,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
catch (err) {
|
|
55
|
+
this.logger.error(err);
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
// onConnect: async (ctx) => {
|
|
59
|
+
// // do anything when connected
|
|
60
|
+
// },
|
|
61
|
+
// onOperation: (ctx, msg) => {
|
|
62
|
+
// }
|
|
63
|
+
// onOperation: async (message: { payload: any }, connection: ExecutionParams) => {
|
|
64
|
+
// connection.formatResponse = (value: ExecutionResult) => ({
|
|
65
|
+
// ...value,
|
|
66
|
+
// errors:
|
|
67
|
+
// value.errors &&
|
|
68
|
+
// formatApolloErrors([...value.errors], {
|
|
69
|
+
// formatter: this.requestOptions.formatError,
|
|
70
|
+
// debug: this.requestOptions.debug,
|
|
71
|
+
// }),
|
|
72
|
+
// });
|
|
73
|
+
// let context: Context = this.context ? this.context : { connection };
|
|
74
|
+
// try {
|
|
75
|
+
// context =
|
|
76
|
+
// typeof this.context === 'function'
|
|
77
|
+
// ? await this.context({ connection, payload: message.payload })
|
|
78
|
+
// : context;
|
|
79
|
+
// } catch (e) {
|
|
80
|
+
// throw formatApolloErrors([e], {
|
|
81
|
+
// formatter: this.requestOptions.formatError,
|
|
82
|
+
// debug: this.requestOptions.debug,
|
|
83
|
+
// })[0];
|
|
84
|
+
// }
|
|
85
|
+
// return { ...connection }; // TODO: we didn't add `context`
|
|
86
|
+
// },
|
|
87
|
+
}, this.wsServer);
|
|
88
|
+
return this.subscriptionServer;
|
|
89
|
+
}
|
|
90
|
+
disconnect() {
|
|
91
|
+
if (this.subscriptionServer) {
|
|
92
|
+
this.subscriptionServer.dispose();
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}exports.GraphqlWs=GraphqlWs;//# sourceMappingURL=GraphqlWs.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GraphqlWs.cjs","sources":["../../src/servers/GraphqlWs.ts"],"sourcesContent":[null],"names":["useServer"],"mappings":"wEAaA;AACA;AACA,MAAM,oCAAoC,GAAG,CAAC,OAAO,EAAE,KAAK,EAAE,WAAW,KAAI;AACzE,IAAA,MAAM,mBAAmB,GAAG,CAAC,QAAQ,KAAI;QACrC,QAAQ,CAAC,UAAU,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC;AAC5C,KAAC,CAAC;;AAEF,IAAA,KAAK,MAAM,IAAI,IAAI,WAAW,EAAE;;AAE5B,QAAA,mBAAmB,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC;KAC1C;AACD,IAAA,OAAO,WAAW,CAAC;AACvB,CAAC,CAAC;MA0CW,SAAS,CAAA;AAMN,IA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BAAkB;qBACrD,CAAC;iBACL;gBAAC,OAAO,GAAG,EAAE;AACV,oBAAA,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;iBAC1B;aACJ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgCJ,SAAA,EACD,IAAI,CAAC,QAAQ,CAChB,CAAC;QACF,OAAO,IAAI,CAAC,kBAAkB,CAAC;KAClC;IAEM,UAAU,GAAA;AACb,QAAA,IAAI,IAAI,CAAC,kBAAkB,EAAE;AACzB,YAAA,IAAI,CAAC,kBAAkB,CAAC,OAAO,EAAE,CAAC;SACrC;KACJ;AACJ"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { RedisClusterCache, RedisCache } from 'apollo-server-cache-redis';
|
|
2
|
+
import { Disposable } from 'graphql-ws';
|
|
3
|
+
import { WebSocketServer } from 'ws';
|
|
4
|
+
import { IModuleService } from '../interfaces';
|
|
5
|
+
export declare class GraphqlWs {
|
|
6
|
+
private wsServer;
|
|
7
|
+
private moduleService;
|
|
8
|
+
protected cache: RedisCache | RedisClusterCache;
|
|
9
|
+
private subscriptionServer;
|
|
10
|
+
private logger;
|
|
11
|
+
constructor(wsServer: WebSocketServer, moduleService: IModuleService, cache: RedisCache | RedisClusterCache);
|
|
12
|
+
create(): Disposable;
|
|
13
|
+
disconnect(): void;
|
|
14
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import {useServer}from'graphql-ws/lib/use/ws';import'graphql';// @workaround as the `dataSources` not available in Subscription (websocket) Context.
|
|
2
|
+
// https://github.com/apollographql/apollo-server/issues/1526 need to revisit in Apollo-Server v3.
|
|
3
|
+
const constructDataSourcesForSubscriptions = (context, cache, dataSources) => {
|
|
4
|
+
const intializeDataSource = (instance) => {
|
|
5
|
+
instance.initialize({ context, cache });
|
|
6
|
+
};
|
|
7
|
+
// tslint:disable-next-line:forin
|
|
8
|
+
for (const prop in dataSources) {
|
|
9
|
+
// tslint:disable-next-line:no-console
|
|
10
|
+
intializeDataSource(dataSources[prop]);
|
|
11
|
+
}
|
|
12
|
+
return dataSources;
|
|
13
|
+
};
|
|
14
|
+
class GraphqlWs {
|
|
15
|
+
wsServer;
|
|
16
|
+
moduleService;
|
|
17
|
+
cache;
|
|
18
|
+
subscriptionServer;
|
|
19
|
+
logger;
|
|
20
|
+
constructor(wsServer, moduleService, cache) {
|
|
21
|
+
this.wsServer = wsServer;
|
|
22
|
+
this.moduleService = moduleService;
|
|
23
|
+
this.cache = cache;
|
|
24
|
+
}
|
|
25
|
+
create() {
|
|
26
|
+
this.subscriptionServer = useServer({
|
|
27
|
+
schema: this.moduleService.schema,
|
|
28
|
+
// Adding a context property lets you add data to your GraphQL operation context
|
|
29
|
+
context: async (ctx) => {
|
|
30
|
+
// Returning an object here will add that information to our
|
|
31
|
+
// GraphQL context, which all of our resolvers have access to.
|
|
32
|
+
// ctx is the `graphql-ws` Context where connectionParams live
|
|
33
|
+
try {
|
|
34
|
+
const pureContext = await this.moduleService.createContext(ctx.connectionParams, null);
|
|
35
|
+
const contextServices = await this.moduleService.serviceContext(ctx.connectionParams, null);
|
|
36
|
+
const context = {
|
|
37
|
+
...contextServices,
|
|
38
|
+
...pureContext,
|
|
39
|
+
preferences: this.moduleService.defaultPreferences,
|
|
40
|
+
// update: updateContainers,
|
|
41
|
+
...ctx,
|
|
42
|
+
};
|
|
43
|
+
const addons = {
|
|
44
|
+
dataSources: constructDataSourcesForSubscriptions(context, this.cache, this.moduleService.dataSource),
|
|
45
|
+
};
|
|
46
|
+
return {
|
|
47
|
+
...context,
|
|
48
|
+
...addons,
|
|
49
|
+
...ctx,
|
|
50
|
+
...pureContext,
|
|
51
|
+
preferences: this.moduleService.defaultPreferences,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
catch (err) {
|
|
55
|
+
this.logger.error(err);
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
// onConnect: async (ctx) => {
|
|
59
|
+
// // do anything when connected
|
|
60
|
+
// },
|
|
61
|
+
// onOperation: (ctx, msg) => {
|
|
62
|
+
// }
|
|
63
|
+
// onOperation: async (message: { payload: any }, connection: ExecutionParams) => {
|
|
64
|
+
// connection.formatResponse = (value: ExecutionResult) => ({
|
|
65
|
+
// ...value,
|
|
66
|
+
// errors:
|
|
67
|
+
// value.errors &&
|
|
68
|
+
// formatApolloErrors([...value.errors], {
|
|
69
|
+
// formatter: this.requestOptions.formatError,
|
|
70
|
+
// debug: this.requestOptions.debug,
|
|
71
|
+
// }),
|
|
72
|
+
// });
|
|
73
|
+
// let context: Context = this.context ? this.context : { connection };
|
|
74
|
+
// try {
|
|
75
|
+
// context =
|
|
76
|
+
// typeof this.context === 'function'
|
|
77
|
+
// ? await this.context({ connection, payload: message.payload })
|
|
78
|
+
// : context;
|
|
79
|
+
// } catch (e) {
|
|
80
|
+
// throw formatApolloErrors([e], {
|
|
81
|
+
// formatter: this.requestOptions.formatError,
|
|
82
|
+
// debug: this.requestOptions.debug,
|
|
83
|
+
// })[0];
|
|
84
|
+
// }
|
|
85
|
+
// return { ...connection }; // TODO: we didn't add `context`
|
|
86
|
+
// },
|
|
87
|
+
}, this.wsServer);
|
|
88
|
+
return this.subscriptionServer;
|
|
89
|
+
}
|
|
90
|
+
disconnect() {
|
|
91
|
+
if (this.subscriptionServer) {
|
|
92
|
+
this.subscriptionServer.dispose();
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}export{GraphqlWs};//# sourceMappingURL=GraphqlWs.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GraphqlWs.mjs","sources":["../../src/servers/GraphqlWs.ts"],"sourcesContent":[null],"names":[],"mappings":"8DAaA;AACA;AACA,MAAM,oCAAoC,GAAG,CAAC,OAAO,EAAE,KAAK,EAAE,WAAW,KAAI;AACzE,IAAA,MAAM,mBAAmB,GAAG,CAAC,QAAQ,KAAI;QACrC,QAAQ,CAAC,UAAU,CAAC,EAAE,OAAO,EAAE,KA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,EACP,IAAI,CAAC,KAAK,EACV,IAAI,CAAC,aAAa,CAAC,UAAU,CAChC;qBACJ,CAAC;oBACF,OAAO;AACH,wBAAA,GAAG,OAAO;AACV,wBAAA,GAAG,MAAM;AACT,wBAAA,GAAG,GAAG;AACN,wBAAA,GAAG,WAAW;AACd,wBAAA,WAAW,EAAE,IAAI,CAAC,aAAa,CAAC,kBAAkB;qBACrD,CAAC;iBACL;gBAAC,OAAO,GAAG,EAAE;AACV,oBAAA,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;iBAC1B;aACJ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgCJ,SAAA,EACD,IAAI,CAAC,QAAQ,CAChB,CAAC;QACF,OAAO,IAAI,CAAC,kBAAkB,CAAC;KAClC;IAEM,UAAU,GAAA;AACb,QAAA,IAAI,IAAI,CAAC,kBAAkB,EAAE;AACzB,YAAA,IAAI,CAAC,kBAAkB,CAAC,OAAO,EAAE,CAAC;SACrC;KACJ;AACJ"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
'use strict';var url=require('url'),ws=require('ws'),ws$1=require('graphql-ws/lib/use/ws');function _interopNamespaceDefault(e){var n=Object.create(null);if(e){Object.keys(e).forEach(function(k){if(k!=='default'){var d=Object.getOwnPropertyDescriptor(e,k);Object.defineProperty(n,k,d.get?d:{enumerable:true,get:function(){return e[k]}});}})}n.default=e;return Object.freeze(n)}var url__namespace=/*#__PURE__*/_interopNamespaceDefault(url);class WebsocketMultiPathServer {
|
|
2
|
+
moduleService;
|
|
3
|
+
cache;
|
|
4
|
+
webSockets = {};
|
|
5
|
+
// private graphqlSubscriptionServer: GraphqlSubscriptionServer;
|
|
6
|
+
_graphqlWs;
|
|
7
|
+
constructor(moduleService, cache, multiplePathConfig) {
|
|
8
|
+
this.moduleService = moduleService;
|
|
9
|
+
this.cache = cache;
|
|
10
|
+
this._graphqlWs = new ws.WebSocketServer({ noServer: true, path: __GRAPHQL_ENDPOINT__ });
|
|
11
|
+
this.webSockets[__GRAPHQL_ENDPOINT__] = this._graphqlWs;
|
|
12
|
+
this.webSockets[__GRAPHQL_ENDPOINT__].on('connection', (ws, request) => {
|
|
13
|
+
console.log('--REQUESTED CONNECTION--', this._graphqlWs);
|
|
14
|
+
ws$1.useServer({
|
|
15
|
+
schema: moduleService.schema,
|
|
16
|
+
}, this._graphqlWs);
|
|
17
|
+
});
|
|
18
|
+
for (let key in multiplePathConfig) {
|
|
19
|
+
if (!multiplePathConfig.hasOwnProperty(key)) {
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
if (!this.webSockets[key]) {
|
|
23
|
+
this.webSockets[key] = new ws.WebSocketServer({ noServer: true });
|
|
24
|
+
this.webSockets[key].on('connection', (ws, request) => {
|
|
25
|
+
Promise.all([
|
|
26
|
+
moduleService.createContext(request, null),
|
|
27
|
+
moduleService.serviceContext(request, null),
|
|
28
|
+
]).then(multiplePathConfig[key](ws));
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
httpServerUpgrade(httpServer) {
|
|
34
|
+
httpServer.on('upgrade', (request, socket, head) => {
|
|
35
|
+
const pathname = url__namespace.parse(request.url).pathname;
|
|
36
|
+
console.log('--UPGRADE', request.url);
|
|
37
|
+
if (!this.webSockets[pathname]) {
|
|
38
|
+
// in development
|
|
39
|
+
if (pathname !== '/sockjs-node') {
|
|
40
|
+
// need to destroy
|
|
41
|
+
socket.destroy();
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
// code to run when a new connection is made
|
|
45
|
+
this.webSockets[pathname].handleUpgrade(request, socket, head, (ws) => {
|
|
46
|
+
this.webSockets[pathname].emit('connection', ws, request);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
// (new GraphqlWs(this.graphqlWs, this.moduleService, this.cache)).create();
|
|
50
|
+
// useServer({
|
|
51
|
+
// schema: this.moduleService.schema,
|
|
52
|
+
// }, this.graphqlWs);
|
|
53
|
+
return httpServer;
|
|
54
|
+
}
|
|
55
|
+
get graphqlWs() {
|
|
56
|
+
return this._graphqlWs;
|
|
57
|
+
}
|
|
58
|
+
close() {
|
|
59
|
+
for (const key in this.webSockets) {
|
|
60
|
+
this.webSockets[key].close();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}exports.WebsocketMultiPathServer=WebsocketMultiPathServer;//# sourceMappingURL=WebsocketMultipathUpdate.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WebsocketMultipathUpdate.cjs","sources":["../../src/servers/WebsocketMultipathUpdate.ts"],"sourcesContent":[null],"names":["WebSocketServer","useServer","url"],"mappings":"6bAgBa,wBAAwB,CAAA;AAKtB,IAAA,aAAA,CAAA;AACA,IAAA,KAAA,CAAA;IALH,UAAU,GAAoB,EAAE,CAAC;;AAEjC,IAAA,UAAU,CAAkB;AACpC,IAAA,WAAA,CACW,aAA6B,EAC7B,KAAqC,EAC5C,kBAAyC,EAAA;QAFlC,IAAa,CAAA,aAAA,GAAb,aAAa,CAAgB;QAC7B,IAAK,CAAA,KAAA,GAAL,KAAK,CAAgC;AAG5C,QAAA,IAAI,CAAC,UAAU,GAAG,IAAIA,kBAAe,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,IAAI,EAAE,oBAAoB,EAAE,CAAC,CAAC;QACtF,IAAI,CAAC,UAAU,CAAC,oBAAoB,CAAC,GAAG,IAAI,CAAC,UAAU,CAAC;AACxD,QAAA,IAAI,CAAC,UAAU,CAAC,oBAAoB,CAAC,CAAC,EAAE,CAAC,YAAY,EAAE,CAAC,EAAE,EAAE,OAAO,KAAI;YACnE,OAAO,CAAC,GAAG,CAAC,0BAA0B,EAAE,IAAI,CAAC,UAAU,CAAC,CAAA;AACxD,YAAAC,cAAS,CAAC;gBACN,MAAM,EAAE,aAAa,CAAC,MAAM;AAC/B,aAAA,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;AACxB,SAAC,CAA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C,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,EAAE,KAAI;AAClE,gBAAA,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,YAAY,EAAE,EAAE,EAAE,OAAO,CAAC,CAAC;AAC9D,aAAC,CAAC,CAAC;AACP,SAAC,CAAC,CAAC;;;;;AAOH,QAAA,OAAO,UAAU,CAAC;KACrB;AAED,IAAA,IAAW,SAAS,GAAA;QAChB,OAAO,IAAI,CAAC,UAAU,CAAC;KAC1B;IACM,KAAK,GAAA;AACR,QAAA,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,UAAU,EAAE;YAC/B,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC;SAChC;KACJ;AACJ"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { WebSocketServer } from 'ws';
|
|
3
|
+
import { Server } from 'http';
|
|
4
|
+
import { RedisClusterCache, RedisCache } from 'apollo-server-cache-redis';
|
|
5
|
+
import { IModuleService } from '../interfaces';
|
|
6
|
+
interface MultiWebsocketConfig {
|
|
7
|
+
[key: string]: any;
|
|
8
|
+
}
|
|
9
|
+
export declare class WebsocketMultiPathServer {
|
|
10
|
+
moduleService: IModuleService;
|
|
11
|
+
cache: RedisCache | RedisClusterCache;
|
|
12
|
+
private webSockets;
|
|
13
|
+
private _graphqlWs;
|
|
14
|
+
constructor(moduleService: IModuleService, cache: RedisCache | RedisClusterCache, multiplePathConfig?: MultiWebsocketConfig);
|
|
15
|
+
httpServerUpgrade(httpServer: Server): Server<typeof import("http").IncomingMessage, typeof import("http").ServerResponse>;
|
|
16
|
+
get graphqlWs(): WebSocketServer;
|
|
17
|
+
close(): void;
|
|
18
|
+
}
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import*as url from'url';import {WebSocketServer}from'ws';import {useServer}from'graphql-ws/lib/use/ws';class WebsocketMultiPathServer {
|
|
2
|
+
moduleService;
|
|
3
|
+
cache;
|
|
4
|
+
webSockets = {};
|
|
5
|
+
// private graphqlSubscriptionServer: GraphqlSubscriptionServer;
|
|
6
|
+
_graphqlWs;
|
|
7
|
+
constructor(moduleService, cache, multiplePathConfig) {
|
|
8
|
+
this.moduleService = moduleService;
|
|
9
|
+
this.cache = cache;
|
|
10
|
+
this._graphqlWs = new WebSocketServer({ noServer: true, path: __GRAPHQL_ENDPOINT__ });
|
|
11
|
+
this.webSockets[__GRAPHQL_ENDPOINT__] = this._graphqlWs;
|
|
12
|
+
this.webSockets[__GRAPHQL_ENDPOINT__].on('connection', (ws, request) => {
|
|
13
|
+
console.log('--REQUESTED CONNECTION--', this._graphqlWs);
|
|
14
|
+
useServer({
|
|
15
|
+
schema: moduleService.schema,
|
|
16
|
+
}, this._graphqlWs);
|
|
17
|
+
});
|
|
18
|
+
for (let key in multiplePathConfig) {
|
|
19
|
+
if (!multiplePathConfig.hasOwnProperty(key)) {
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
if (!this.webSockets[key]) {
|
|
23
|
+
this.webSockets[key] = new WebSocketServer({ noServer: true });
|
|
24
|
+
this.webSockets[key].on('connection', (ws, request) => {
|
|
25
|
+
Promise.all([
|
|
26
|
+
moduleService.createContext(request, null),
|
|
27
|
+
moduleService.serviceContext(request, null),
|
|
28
|
+
]).then(multiplePathConfig[key](ws));
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
httpServerUpgrade(httpServer) {
|
|
34
|
+
httpServer.on('upgrade', (request, socket, head) => {
|
|
35
|
+
const pathname = url.parse(request.url).pathname;
|
|
36
|
+
console.log('--UPGRADE', request.url);
|
|
37
|
+
if (!this.webSockets[pathname]) {
|
|
38
|
+
// in development
|
|
39
|
+
if (pathname !== '/sockjs-node') {
|
|
40
|
+
// need to destroy
|
|
41
|
+
socket.destroy();
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
// code to run when a new connection is made
|
|
45
|
+
this.webSockets[pathname].handleUpgrade(request, socket, head, (ws) => {
|
|
46
|
+
this.webSockets[pathname].emit('connection', ws, request);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
// (new GraphqlWs(this.graphqlWs, this.moduleService, this.cache)).create();
|
|
50
|
+
// useServer({
|
|
51
|
+
// schema: this.moduleService.schema,
|
|
52
|
+
// }, this.graphqlWs);
|
|
53
|
+
return httpServer;
|
|
54
|
+
}
|
|
55
|
+
get graphqlWs() {
|
|
56
|
+
return this._graphqlWs;
|
|
57
|
+
}
|
|
58
|
+
close() {
|
|
59
|
+
for (const key in this.webSockets) {
|
|
60
|
+
this.webSockets[key].close();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}export{WebsocketMultiPathServer};//# sourceMappingURL=WebsocketMultipathUpdate.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WebsocketMultipathUpdate.mjs","sources":["../../src/servers/WebsocketMultipathUpdate.ts"],"sourcesContent":[null],"names":[],"mappings":"6GAgBa,wBAAwB,CAAA;AAKtB,IAAA,aAAA,CAAA;AACA,IAAA,KAAA,CAAA;IALH,UAAU,GAAoB,EAAE,CAAC;;AAEjC,IAAA,UAAU,CAAkB;AACpC,IAAA,WAAA,CACW,aAA6B,EAC7B,KAAqC,EAC5C,kBAAyC,EAAA;QAFlC,IAAa,CAAA,aAAA,GAAb,aAAa,CAAgB;QAC7B,IAAK,CAAA,KAAA,GAAL,KAAK,CAAgC;AAG5C,QAAA,IAAI,CAAC,UAAU,GAAG,IAAI,eAAe,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,IAAI,EAAE,oBAAoB,EAAE,CAAC,CAAC;QACtF,IAAI,CAAC,UAAU,CAAC,oBAAoB,CAAC,GAAG,IAAI,CAAC,UAAU,CAAC;AACxD,QAAA,IAAI,CAAC,UAAU,CAAC,oBAAoB,CAAC,CAAC,E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AAE,OAAO,CAAC,GAAG,CAAC,CAAA;YAErC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE;;AAE5B,gBAAA,IAAI,QAAQ,KAAK,cAAc,EAAE;;oBAE7B,MAAM,CAAC,OAAO,EAAE,CAAC;iBACpB;aACJ;;AAGD,YAAA,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,EAAE,KAAI;AAClE,gBAAA,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,YAAY,EAAE,EAAE,EAAE,OAAO,CAAC,CAAC;AAC9D,aAAC,CAAC,CAAC;AACP,SAAC,CAAC,CAAC;;;;;AAOH,QAAA,OAAO,UAAU,CAAC;KACrB;AAED,IAAA,IAAW,SAAS,GAAA;QAChB,OAAO,IAAI,CAAC,UAAU,CAAC;KAC1B;IACM,KAAK,GAAA;AACR,QAAA,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,UAAU,EAAE;YAC/B,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC;SAChC;KACJ;AACJ"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/// <reference types="mongoose/types/aggregate" />
|
|
2
|
+
/// <reference types="mongoose/types/callback" />
|
|
3
|
+
/// <reference types="mongoose/types/collection" />
|
|
4
|
+
/// <reference types="mongoose/types/connection" />
|
|
5
|
+
/// <reference types="mongoose/types/cursor" />
|
|
6
|
+
/// <reference types="mongoose/types/document" />
|
|
7
|
+
/// <reference types="mongoose/types/error" />
|
|
8
|
+
/// <reference types="mongoose/types/expressions" />
|
|
9
|
+
/// <reference types="mongoose/types/helpers" />
|
|
10
|
+
/// <reference types="mongoose/types/middlewares" />
|
|
11
|
+
/// <reference types="mongoose/types/indexes" />
|
|
12
|
+
/// <reference types="mongoose/types/models" />
|
|
13
|
+
/// <reference types="mongoose/types/mongooseoptions" />
|
|
14
|
+
/// <reference types="mongoose/types/pipelinestage" />
|
|
15
|
+
/// <reference types="mongoose/types/populate" />
|
|
16
|
+
/// <reference types="mongoose/types/query" />
|
|
17
|
+
/// <reference types="mongoose/types/schemaoptions" />
|
|
18
|
+
/// <reference types="mongoose/types/schematypes" />
|
|
19
|
+
/// <reference types="mongoose/types/session" />
|
|
20
|
+
/// <reference types="mongoose/types/types" />
|
|
21
|
+
/// <reference types="mongoose/types/utility" />
|
|
22
|
+
/// <reference types="mongoose/types/validation" />
|
|
23
|
+
/// <reference types="mongoose/types/virtuals" />
|
|
24
|
+
/// <reference types="mongoose/types/inferschematype" />
|
|
25
|
+
import { IModuleService } from '../interfaces';
|
|
26
|
+
import { Schema, Connection as DBConnection } from 'mongoose';
|
|
27
|
+
export declare const MigrationSchema: Schema<any, import("mongoose").Model<any, any, any, any, any>, {}, {}, {}, {}, import("mongoose").DefaultSchemaOptions, {
|
|
28
|
+
name: string;
|
|
29
|
+
migrated_at?: Date;
|
|
30
|
+
}>;
|
|
31
|
+
export declare class MongoDBMigrationUpgrade {
|
|
32
|
+
private moduleService;
|
|
33
|
+
private mongoDB;
|
|
34
|
+
constructor(moduleService: IModuleService, mongoDB: DBConnection);
|
|
35
|
+
migrate(): Promise<any>;
|
|
36
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { RedisCache } from 'apollo-server-cache-redis';
|
|
2
|
+
import { CdmLogger } from '@cdm-logger/core';
|
|
3
|
+
type ILogger = CdmLogger.ILogger;
|
|
4
|
+
/**
|
|
5
|
+
* It'll get messy below but there's an issue currently with the state of the protocols that can be used (subscriptions-transport-ws vs graphql-ws).
|
|
6
|
+
* Read more from: https://www.apollographql.com/docs/apollo-server/data/subscriptions/#the-graphql-ws-transport-library
|
|
7
|
+
*
|
|
8
|
+
* Bottomline is, if we use the newer and actively maintained `graphql-ws` lib, then the GraphQL Playground will not work because it uses the old protocol.
|
|
9
|
+
*
|
|
10
|
+
* The approach below tries to support both based on the template provided here but adjusted for our setup here.
|
|
11
|
+
* https://github.com/enisdenjo/graphql-ws#ws-backwards-compat
|
|
12
|
+
*/
|
|
13
|
+
export declare function createContextFromConnectionParams(context: any, datasources: any, cache: RedisCache, logger: ILogger): Promise<any>;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
'use strict';var mongoose=require('mongoose');const MigrationSchema = new mongoose.Schema({
|
|
2
|
+
migrated_at: Date,
|
|
3
|
+
name: { required: true, type: String },
|
|
4
|
+
});
|
|
5
|
+
async function migrate(db, container, logger) {
|
|
6
|
+
try {
|
|
7
|
+
const migrations = container.getAll('MongodbMigration');
|
|
8
|
+
const model = db.model('Migration', MigrationSchema);
|
|
9
|
+
migrations.map(async (migration) => {
|
|
10
|
+
const exists = await model.findOne({ name: migration.id });
|
|
11
|
+
if (!exists) {
|
|
12
|
+
try {
|
|
13
|
+
await migration.up();
|
|
14
|
+
await model.create({ name: migration.id, migrated_at: new Date() });
|
|
15
|
+
}
|
|
16
|
+
catch (e) {
|
|
17
|
+
console.log(`Can not process migration ${migration.id}: `, e);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return migration.id;
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
catch (err) {
|
|
24
|
+
console.warn('ignoring migrate database due to ', err.message);
|
|
25
|
+
}
|
|
26
|
+
}exports.MigrationSchema=MigrationSchema;exports.migrate=migrate;//# sourceMappingURL=migrations.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migrations.cjs","sources":["../../src/utils/migrations.ts"],"sourcesContent":[null],"names":["Schema"],"mappings":"8CAKa,MAAA,eAAe,GAAG,IAAIA,eAAM,CAAC;AACtC,IAAA,WAAW,EAAE,IAAI;IACjB,IAAI,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE;AACzC,CAAA,EAAE;AAEI,eAAe,OAAO,CAAC,EAAc,E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