@common-stack/server-stack 8.0.1-alpha.2 → 8.2.1-alpha.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/LICENSE +34 -21
- package/lib/MainStackServer.cjs +126 -143
- package/lib/MainStackServer.cjs.map +1 -1
- package/lib/MainStackServer.d.ts +12 -6
- package/lib/MainStackServer.mjs +126 -143
- package/lib/MainStackServer.mjs.map +1 -1
- package/lib/StackServer.cjs +23 -66
- package/lib/StackServer.cjs.map +1 -1
- package/lib/StackServer.d.ts +4 -5
- package/lib/StackServer.mjs +23 -66
- package/lib/StackServer.mjs.map +1 -1
- package/lib/api/schema-builder.cjs +13 -12
- package/lib/api/schema-builder.cjs.map +1 -1
- package/lib/api/schema-builder.mjs +13 -12
- package/lib/api/schema-builder.mjs.map +1 -1
- package/lib/api/sub-graph-schema-builder.cjs +147 -0
- package/lib/api/sub-graph-schema-builder.cjs.map +1 -0
- package/lib/api/sub-graph-schema-builder.d.ts +24 -0
- package/lib/api/sub-graph-schema-builder.mjs +147 -0
- package/lib/api/sub-graph-schema-builder.mjs.map +1 -0
- package/lib/config/env-config.cjs +6 -0
- package/lib/config/env-config.cjs.map +1 -1
- package/lib/config/env-config.d.ts +6 -0
- package/lib/config/env-config.mjs +6 -0
- package/lib/config/env-config.mjs.map +1 -1
- package/lib/examples/tenant-feature-example.d.ts +13 -0
- package/lib/examples/tenant-feature-integration.d.ts +14 -0
- package/lib/features/multi-module-example.d.ts +17 -0
- package/lib/index.cjs +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.mjs +1 -1
- package/lib/infrastructure/container-module.cjs +27 -0
- package/lib/infrastructure/container-module.cjs.map +1 -0
- package/lib/infrastructure/container-module.d.ts +3 -0
- package/lib/infrastructure/container-module.mjs +27 -0
- package/lib/infrastructure/container-module.mjs.map +1 -0
- package/lib/infrastructure/example-workflows.d.ts +16 -0
- package/lib/infrastructure/index.d.ts +5 -0
- package/lib/infrastructure/infrastructure-factory.cjs +172 -0
- package/lib/infrastructure/infrastructure-factory.cjs.map +1 -0
- package/lib/infrastructure/infrastructure-factory.d.ts +36 -0
- package/lib/infrastructure/infrastructure-factory.mjs +172 -0
- package/lib/infrastructure/infrastructure-factory.mjs.map +1 -0
- package/lib/infrastructure/inngest-factory.cjs +47 -0
- package/lib/infrastructure/inngest-factory.cjs.map +1 -0
- package/lib/infrastructure/inngest-factory.d.ts +8 -0
- package/lib/infrastructure/inngest-factory.mjs +47 -0
- package/lib/infrastructure/inngest-factory.mjs.map +1 -0
- package/lib/infrastructure/types.d.ts +28 -0
- package/lib/infrastructure/usage-examples.d.ts +9 -0
- package/lib/infrastructure/workflow-orchestrator.cjs +59 -0
- package/lib/infrastructure/workflow-orchestrator.cjs.map +1 -0
- package/lib/infrastructure/workflow-orchestrator.d.ts +10 -0
- package/lib/infrastructure/workflow-orchestrator.mjs +59 -0
- package/lib/infrastructure/workflow-orchestrator.mjs.map +1 -0
- package/lib/inngest/client-id-generator.d.ts +29 -0
- package/lib/inngest/handler-factory.d.ts +24 -0
- package/lib/inngest/middleware/auto-resolve-invoke.middleware.cjs +275 -0
- package/lib/inngest/middleware/auto-resolve-invoke.middleware.cjs.map +1 -0
- package/lib/inngest/middleware/auto-resolve-invoke.middleware.d.ts +111 -0
- package/lib/inngest/middleware/auto-resolve-invoke.middleware.mjs +275 -0
- package/lib/inngest/middleware/auto-resolve-invoke.middleware.mjs.map +1 -0
- package/lib/inngest/middleware/function-reference-helper.cjs +363 -0
- package/lib/inngest/middleware/function-reference-helper.cjs.map +1 -0
- package/lib/inngest/middleware/function-reference-helper.d.ts +211 -0
- package/lib/inngest/middleware/function-reference-helper.mjs +363 -0
- package/lib/inngest/middleware/function-reference-helper.mjs.map +1 -0
- package/lib/inngest/middleware/index.d.ts +31 -0
- package/lib/inngest/middleware/types.d.ts +162 -0
- package/lib/inngest/multi-module-functions.d.ts +133 -0
- package/lib/inngest/setup.cjs +55 -0
- package/lib/inngest/setup.cjs.map +1 -0
- package/lib/inngest/setup.d.ts +10 -0
- package/lib/inngest/setup.mjs +55 -0
- package/lib/inngest/setup.mjs.map +1 -0
- package/lib/interfaces/graphql-request-context.d.ts +20 -0
- package/lib/interfaces/index.d.ts +1 -0
- package/lib/middleware/index.d.ts +5 -0
- package/lib/middleware/redis-client.cjs +18 -0
- package/lib/middleware/redis-client.cjs.map +1 -0
- package/lib/middleware/redis-client.d.ts +16 -0
- package/lib/middleware/redis-client.mjs +18 -0
- package/lib/middleware/redis-client.mjs.map +1 -0
- package/lib/plugins/index.d.ts +1 -0
- package/lib/plugins/invalidateCachePlugin.cjs +33 -10
- package/lib/plugins/invalidateCachePlugin.cjs.map +1 -1
- package/lib/plugins/invalidateCachePlugin.mjs +33 -10
- package/lib/plugins/invalidateCachePlugin.mjs.map +1 -1
- package/lib/plugins/responseCachePlugin.cjs +63 -0
- package/lib/plugins/responseCachePlugin.cjs.map +1 -0
- package/lib/plugins/responseCachePlugin.d.ts +12 -0
- package/lib/plugins/responseCachePlugin.mjs +63 -0
- package/lib/plugins/responseCachePlugin.mjs.map +1 -0
- package/lib/servers/ExpressApp.cjs +8 -3
- package/lib/servers/ExpressApp.cjs.map +1 -1
- package/lib/servers/ExpressApp.d.ts +1 -1
- package/lib/servers/ExpressApp.mjs +8 -3
- package/lib/servers/ExpressApp.mjs.map +1 -1
- package/lib/servers/GraphqlServer.cjs +10 -36
- package/lib/servers/GraphqlServer.cjs.map +1 -1
- package/lib/servers/GraphqlServer.d.ts +8 -6
- package/lib/servers/GraphqlServer.mjs +10 -36
- package/lib/servers/GraphqlServer.mjs.map +1 -1
- package/lib/servers/GraphqlWs.cjs +3 -44
- package/lib/servers/GraphqlWs.cjs.map +1 -1
- package/lib/servers/GraphqlWs.mjs +3 -44
- package/lib/servers/GraphqlWs.mjs.map +1 -1
- package/lib/servers/WebsocketMultipathUpdate.cjs +4 -2
- package/lib/servers/WebsocketMultipathUpdate.cjs.map +1 -1
- package/lib/servers/WebsocketMultipathUpdate.mjs +4 -2
- package/lib/servers/WebsocketMultipathUpdate.mjs.map +1 -1
- package/lib/servers/mongodb-migration-update.d.ts +1 -1
- package/lib/servers/websocket-context.cjs +70 -0
- package/lib/servers/websocket-context.cjs.map +1 -0
- package/lib/servers/websocket-context.d.ts +30 -0
- package/lib/servers/websocket-context.mjs +70 -0
- package/lib/servers/websocket-context.mjs.map +1 -0
- package/lib/utils/add-shareable-directive-to-schema.cjs +44 -0
- package/lib/utils/add-shareable-directive-to-schema.cjs.map +1 -0
- package/lib/utils/add-shareable-directive-to-schema.d.ts +1 -0
- package/lib/utils/add-shareable-directive-to-schema.mjs +44 -0
- package/lib/utils/add-shareable-directive-to-schema.mjs.map +1 -0
- package/lib/utils/index.d.ts +3 -0
- package/lib/utils/safe-model-factory.d.ts +18 -0
- package/package.json +15 -11
- package/lib/api/resolver.cjs +0 -12
- package/lib/api/resolver.cjs.map +0 -1
- package/lib/api/resolver.mjs +0 -12
- package/lib/api/resolver.mjs.map +0 -1
- package/lib/api/root-schema.graphqls.cjs +0 -2
- package/lib/api/root-schema.graphqls.cjs.map +0 -1
- package/lib/api/root-schema.graphqls.mjs +0 -2
- package/lib/api/root-schema.graphqls.mjs.map +0 -1
- package/lib/api/scalar.cjs +0 -16
- package/lib/api/scalar.cjs.map +0 -1
- package/lib/api/scalar.mjs +0 -16
- package/lib/api/scalar.mjs.map +0 -1
- package/lib/graphql/directives/index.cjs +0 -10
- package/lib/graphql/directives/index.cjs.map +0 -1
- package/lib/graphql/directives/index.d.ts +0 -7
- package/lib/graphql/directives/index.mjs +0 -10
- package/lib/graphql/directives/index.mjs.map +0 -1
- package/lib/graphql/index.d.ts +0 -2
- package/lib/graphql/schema/directives.graphql.cjs +0 -1
- package/lib/graphql/schema/directives.graphql.cjs.map +0 -1
- package/lib/graphql/schema/directives.graphql.mjs +0 -1
- package/lib/graphql/schema/directives.graphql.mjs.map +0 -1
- package/lib/graphql/schema/index.cjs +0 -1
- package/lib/graphql/schema/index.cjs.map +0 -1
- package/lib/graphql/schema/index.d.ts +0 -1
- package/lib/graphql/schema/index.mjs +0 -1
- package/lib/graphql/schema/index.mjs.map +0 -1
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import {getDirectiveArgsFromSchema,CACHE_CONTROL_DIRECTIVE,extractTenantId,generateQueryCacheKey}from'@common-stack/server-core';import {isEmpty}from'lodash-es';import apolloCachePlugin from'@apollo/server-plugin-response-cache';const cachePlugin = apolloCachePlugin.default ?? apolloCachePlugin;
|
|
2
|
+
const isCacheable = (requestContext) => {
|
|
3
|
+
const { document, schema } = requestContext;
|
|
4
|
+
const cache = getDirectiveArgsFromSchema({
|
|
5
|
+
schema,
|
|
6
|
+
document,
|
|
7
|
+
directiveName: CACHE_CONTROL_DIRECTIVE,
|
|
8
|
+
});
|
|
9
|
+
if (!cache)
|
|
10
|
+
return false;
|
|
11
|
+
if (cache.scope && !cache.maxAge) {
|
|
12
|
+
cache.maxAge = 86400;
|
|
13
|
+
}
|
|
14
|
+
if (!requestContext.overallCachePolicy) {
|
|
15
|
+
// eslint-disable-next-line no-param-reassign
|
|
16
|
+
// to support test cases
|
|
17
|
+
requestContext.overallCachePolicy = {};
|
|
18
|
+
}
|
|
19
|
+
// eslint-disable-next-line no-param-reassign
|
|
20
|
+
requestContext.overallCachePolicy.maxAge = cache.maxAge;
|
|
21
|
+
return cache.maxAge > 0;
|
|
22
|
+
};
|
|
23
|
+
const generateCacheKey = ({ logger, cacheKeyGenerator }) => (requestContext) => {
|
|
24
|
+
if (!isCacheable(requestContext)) {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
const { request, contextValue, document, schema } = requestContext;
|
|
28
|
+
const { user, req } = contextValue ?? {};
|
|
29
|
+
const { query, variables } = request;
|
|
30
|
+
const cacheControlDirective = getDirectiveArgsFromSchema({
|
|
31
|
+
schema,
|
|
32
|
+
document,
|
|
33
|
+
directiveName: CACHE_CONTROL_DIRECTIVE,
|
|
34
|
+
});
|
|
35
|
+
const { scope } = cacheControlDirective ?? {};
|
|
36
|
+
const isPrivate = scope?.toLowerCase() === 'private';
|
|
37
|
+
const tenantId = req.tenant ?? extractTenantId(req?.currentPageUriSegments?.authority);
|
|
38
|
+
const cacheKey = generateQueryCacheKey({
|
|
39
|
+
query,
|
|
40
|
+
variables,
|
|
41
|
+
logger,
|
|
42
|
+
userId: isPrivate ? user?.sub || null : null,
|
|
43
|
+
tenantId,
|
|
44
|
+
});
|
|
45
|
+
try {
|
|
46
|
+
if (typeof cacheKeyGenerator === 'function') {
|
|
47
|
+
const generatedKey = cacheKeyGenerator(requestContext, cacheKey);
|
|
48
|
+
return generatedKey || cacheKey;
|
|
49
|
+
}
|
|
50
|
+
return cacheKey;
|
|
51
|
+
}
|
|
52
|
+
catch (e) {
|
|
53
|
+
console.warn('GenerateCacheKey Failed %s', e.message);
|
|
54
|
+
return cacheKey;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
const responseCachePlugin = ({ logger, cacheKeyGenerator }) => cachePlugin({
|
|
58
|
+
sessionId: ({ contextValue }) => Promise.resolve(contextValue?.user?.sub ?? null),
|
|
59
|
+
generateCacheKey: generateCacheKey({ logger, cacheKeyGenerator }),
|
|
60
|
+
shouldWriteToCache: (ctx) =>
|
|
61
|
+
// Cache only successful responses
|
|
62
|
+
isCacheable(ctx) && isEmpty(ctx?.response?.body?.singleResult.errors),
|
|
63
|
+
});export{generateCacheKey,isCacheable,responseCachePlugin};//# sourceMappingURL=responseCachePlugin.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"responseCachePlugin.mjs","sources":["../../src/plugins/responseCachePlugin.ts"],"sourcesContent":[null],"names":[],"mappings":"qOAcA,MAAM,WAAW,GAAI,iBAAyB,CAAC,OAAO,IAAI,iBAAiB,CAAC;AAO/D,MAAA,WAAW,GAAG,CAAC,cAA4D,KAAI;AACxF,IAAA,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,cAAc,CAAC;I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AG,GAAG,CAAC,MAAM,IAAI,eAAe,CAAC,GAAG,EAAE,sBAAsB,EAAE,SAAS,CAAC,CAAC;IACvF,MAAM,QAAQ,GAAG,qBAAqB,CAAC;QACnC,KAAK;QACL,SAAS;QACT,MAAM;AACN,QAAA,MAAM,EAAE,SAAS,GAAG,IAAI,EAAE,GAAG,IAAI,IAAI,GAAG,IAAI;QAC5C,QAAQ;AACX,KAAA,CAAC,CAAC;AACH,IAAA,IAAI;AACA,QAAA,IAAI,OAAO,iBAAiB,KAAK,UAAU,EAAE;YACzC,MAAM,YAAY,GAAG,iBAAiB,CAAC,cAAc,EAAE,QAAQ,CAAC,CAAC;YACjE,OAAO,YAAY,IAAI,QAAQ,CAAC;SACnC;AACD,QAAA,OAAO,QAAQ,CAAC;KACnB;IAAC,OAAO,CAAC,EAAE;QACR,OAAO,CAAC,IAAI,CAAC,4BAA4B,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC;AACtD,QAAA,OAAO,QAAQ,CAAC;KACnB;AACL,EAAE;AACC,MAAM,mBAAmB,GAAG,CAAC,EAAE,MAAM,EAAE,iBAAiB,EAA4B,KACvF,WAAW,CAAC;AACR,IAAA,SAAS,EAAE,CAAC,EAAE,YAAY,EAAE,KAAK,OAAO,CAAC,OAAO,CAAC,YAAY,EAAE,IAAI,EAAE,GAAG,IAAI,IAAI,CAAC;IACjF,gBAAgB,EAAE,gBAAgB,CAAC,EAAE,MAAM,EAAE,iBAAiB,EAAE,CAAC;AACjE,IAAA,kBAAkB,EAAE,CAAC,GAAG;;AAEpB,IAAA,WAAW,CAAC,GAAG,CAAC,IAAI,OAAO,CAAC,GAAG,EAAE,QAAQ,EAAE,IAAI,EAAE,YAAY,CAAC,MAAM,CAAC;AAC5E,CAAA"}
|
|
@@ -1,10 +1,15 @@
|
|
|
1
|
-
'use strict';var express=require('express'),bodyParser=require('body-parser'),error=require('../middleware/error.cjs'),services=require('../middleware/services.cjs'),sentry=require('../middleware/sentry.cjs'),cors=require('../middleware/cors.cjs');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 bodyParser__namespace=/*#__PURE__*/_interopNamespaceDefault(bodyParser);/* eslint-disable @typescript-eslint/no-var-requires */
|
|
2
|
-
function expressApp(modules, options, middlewares, http) {
|
|
1
|
+
'use strict';var express=require('express'),bodyParser=require('body-parser'),error=require('../middleware/error.cjs'),services=require('../middleware/services.cjs'),sentry=require('../middleware/sentry.cjs'),redisClient=require('../middleware/redis-client.cjs'),cors=require('../middleware/cors.cjs');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 bodyParser__namespace=/*#__PURE__*/_interopNamespaceDefault(bodyParser);/* eslint-disable @typescript-eslint/no-var-requires */
|
|
2
|
+
function expressApp(modules, options, middlewares, http, redisClient$1) {
|
|
3
3
|
const app = express();
|
|
4
|
+
// FIRST PRIORITY: Add Redis client middleware at the very top
|
|
5
|
+
// This ensures req.redisClient is available to ALL downstream middleware
|
|
6
|
+
if (redisClient$1) {
|
|
7
|
+
app.use(redisClient.redisClientMiddleware(redisClient$1));
|
|
8
|
+
}
|
|
9
|
+
app.use(services.contextServicesMiddleware(options.createContext, options.serviceContext));
|
|
4
10
|
for (const applyBeforeware of modules.beforewares) {
|
|
5
11
|
applyBeforeware(app);
|
|
6
12
|
}
|
|
7
|
-
app.use(services.contextServicesMiddleware(options.createContext, options.serviceContext));
|
|
8
13
|
// Don't rate limit heroku
|
|
9
14
|
app.enable('trust proxy');
|
|
10
15
|
app.use(sentry.sentryMiddleware);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ExpressApp.cjs","sources":["../../src/servers/ExpressApp.ts"],"sourcesContent":[null],"names":["contextServicesMiddleware","sentryMiddleware","corsMiddleware","bodyParser","errorMiddleware","sentryErrorHandlerMiddleware"],"mappings":"
|
|
1
|
+
{"version":3,"file":"ExpressApp.cjs","sources":["../../src/servers/ExpressApp.ts"],"sourcesContent":[null],"names":["redisClient","redisClientMiddleware","contextServicesMiddleware","sentryMiddleware","corsMiddleware","bodyParser","errorMiddleware","sentryErrorHandlerMiddleware"],"mappings":"wpBAAA;AAWM,SAAU,UAAU,CAAC,OAAgB,EAAE,OAAuB,EAAE,WAAW,EAAE,IAAK,EAAEA,aAAY,EAAA;AAClG,IAAA,MAAM,GAAG,GAAG,OAAO,EAAE,CAAC;;;IAItB,IAAIA,aAAW,EAAE;QACb,GAAG,CAAC,GAAG,CAACC,iCAAqB,CAACD,aAAW,CAAC,CAAC,CAAC;KAC/C;AAED,IAAA,GAAG,CAAC,GAAG,CAACE,kCAAyB,CAAC,OAAO,CAAC,aAAa,EAAE,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC;AAClF,IAAA,KAAK,MAAM,eAAe,IAAI,OAAO,CAAC,WAAW,EAAE;QAC/C,eAAe,CAAC,GAAG,CAAC,CAAC;KACxB;;AAGD,IAAA,GAAG,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC;AAE1B,IAAA,GAAG,CAAC,GAAG,CAACC,uBAAgB,CAAC,CAAC;AAE1B,IAAA,IAAI,WAAW,KAAK,IAAI,EAAE;AACtB,QAAA,GAAG,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;KACxB;AAED,IAAA,GAAG,CAAC,GAAG,CAACC,mBAAc,CAAC,CAAC;IACxB,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,KAAI;AACvB,QAAA,GAAG,CAAC,MAAM,CAAC,kCAAkC,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC;AACrE,QAAA,GAAG,CAAC,MAAM,CAAC,8BAA8B,EAAE,qBAAqB,CAAC,CAAC;AAClE,QAAA,GAAG,CAAC,MAAM,CACN,8BAA8B,EAC9B,6EAA6E,CAChF,CAAC;AACF,QAAA,IAAI,EAAE,CAAC;AACX,KAAC,CAAC,CAAC;AAEH,IAAA,GAAG,CAAC,GAAG,CACHC,qBAAU,CAAC,IAAI,CAAC;AACZ,QAAA,KAAK,EAAE,MAAM;QACb,MAAM,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,KAAI;;AAErB,YAAA,GAAW,CAAC,OAAO,GAAG,GAAG,CAAC;SAC9B;AACJ,KAAA,CAAC,CACL,CAAC;IACF,GAAG,CAAC,GAAG,CAACA,qBAAU,CAAC,UAAU,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,cAAc,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC;AAEzF,IAAA,KAAK,MAAM,eAAe,IAAI,OAAO,CAAC,WAAW,EAAE;QAC/C,eAAe,CAAC,GAAG,CAAC,CAAC;KACxB;IAED,IAAI,OAAO,EAAE;AACT,QAAA,GAAG,CAAC,GAAG,CAACC,qBAAe,CAAC,CAAC;KAC5B;AAED,IAAA,GAAG,CAAC,GAAG,CAACC,mCAA4B,CAAC,CAAC;AAEtC,IAAA,OAAO,GAAG,CAAC;AACf"}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { Feature } from '@common-stack/server-core';
|
|
2
2
|
import { IModuleService } from '../interfaces';
|
|
3
|
-
export declare function expressApp(modules: Feature, options: IModuleService, middlewares: any, http?: any): import("express-serve-static-core").Express;
|
|
3
|
+
export declare function expressApp(modules: Feature, options: IModuleService, middlewares: any, http?: any, redisClient?: any): import("express-serve-static-core").Express;
|
|
@@ -1,10 +1,15 @@
|
|
|
1
|
-
import express from'express';import*as bodyParser from'body-parser';import {errorMiddleware}from'../middleware/error.mjs';import {contextServicesMiddleware}from'../middleware/services.mjs';import {sentryMiddleware,sentryErrorHandlerMiddleware}from'../middleware/sentry.mjs';import {corsMiddleware}from'../middleware/cors.mjs';/* eslint-disable @typescript-eslint/no-var-requires */
|
|
2
|
-
function expressApp(modules, options, middlewares, http) {
|
|
1
|
+
import express from'express';import*as bodyParser from'body-parser';import {errorMiddleware}from'../middleware/error.mjs';import {contextServicesMiddleware}from'../middleware/services.mjs';import {sentryMiddleware,sentryErrorHandlerMiddleware}from'../middleware/sentry.mjs';import {redisClientMiddleware}from'../middleware/redis-client.mjs';import {corsMiddleware}from'../middleware/cors.mjs';/* eslint-disable @typescript-eslint/no-var-requires */
|
|
2
|
+
function expressApp(modules, options, middlewares, http, redisClient) {
|
|
3
3
|
const app = express();
|
|
4
|
+
// FIRST PRIORITY: Add Redis client middleware at the very top
|
|
5
|
+
// This ensures req.redisClient is available to ALL downstream middleware
|
|
6
|
+
if (redisClient) {
|
|
7
|
+
app.use(redisClientMiddleware(redisClient));
|
|
8
|
+
}
|
|
9
|
+
app.use(contextServicesMiddleware(options.createContext, options.serviceContext));
|
|
4
10
|
for (const applyBeforeware of modules.beforewares) {
|
|
5
11
|
applyBeforeware(app);
|
|
6
12
|
}
|
|
7
|
-
app.use(contextServicesMiddleware(options.createContext, options.serviceContext));
|
|
8
13
|
// Don't rate limit heroku
|
|
9
14
|
app.enable('trust proxy');
|
|
10
15
|
app.use(sentryMiddleware);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ExpressApp.mjs","sources":["../../src/servers/ExpressApp.ts"],"sourcesContent":[null],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ExpressApp.mjs","sources":["../../src/servers/ExpressApp.ts"],"sourcesContent":[null],"names":[],"mappings":"yYAAA;AAWM,SAAU,UAAU,CAAC,OAAgB,EAAE,OAAuB,EAAE,WAAW,EAAE,IAAK,EAAE,WAAY,EAAA;AAClG,IAAA,MAAM,GAAG,GAAG,OAAO,EAAE,CAAC;;;IAItB,IAAI,WAAW,EAAE;QACb,GAAG,CAAC,GA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IAAI,EAAE,cAAc,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC;AAEzF,IAAA,KAAK,MAAM,eAAe,IAAI,OAAO,CAAC,WAAW,EAAE;QAC/C,eAAe,CAAC,GAAG,CAAC,CAAC;KACxB;IAED,IAAI,OAAO,EAAE;AACT,QAAA,GAAG,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC;KAC5B;AAED,IAAA,GAAG,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAC;AAEtC,IAAA,OAAO,GAAG,CAAC;AACf"}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
'use strict';var server=require('@apollo/server'),express4=require('@apollo/server/express4'),cacheControl=require('@apollo/server/plugin/cacheControl'),drainHttpServer=require('@apollo/server/plugin/drainHttpServer')
|
|
2
|
-
if ((process.env.LOG_LEVEL && process.env.LOG_LEVEL === 'trace') || process.env.LOG_LEVEL === 'debug') ;
|
|
1
|
+
'use strict';var server=require('@apollo/server'),express4=require('@apollo/server/express4'),cacheControl=require('@apollo/server/plugin/cacheControl'),drainHttpServer=require('@apollo/server/plugin/drainHttpServer');require('isomorphic-fetch');var express=require('express'),cors=require('cors'),Keyv=require('keyv'),KeyvRedis=require('@keyv/redis'),utils_keyvadapter=require('@apollo/utils.keyvadapter'),ws=require('ws'),invalidateCachePlugin=require('../plugins/invalidateCachePlugin.cjs'),responseCachePlugin=require('../plugins/responseCachePlugin.cjs'),GraphqlWs=require('./GraphqlWs.cjs'),envConfig=require('../config/env-config.cjs');if ((process.env.LOG_LEVEL && process.env.LOG_LEVEL === 'trace') || process.env.LOG_LEVEL === 'debug') ;
|
|
3
2
|
// @workaround as the `dataSources` not available in Subscription (websocket) Context.
|
|
4
3
|
// https://github.com/apollographql/apollo-server/issues/1526 need to revisit in Apollo-Server v3.
|
|
5
4
|
const constructDataSourcesForSubscriptions = (context, cache, dataSources) => {
|
|
@@ -19,9 +18,10 @@ class GraphqlServer {
|
|
|
19
18
|
enableSubscription;
|
|
20
19
|
cacheKeyGenerator;
|
|
21
20
|
invalidateCacheKeyGenerator;
|
|
21
|
+
plugins;
|
|
22
22
|
logger;
|
|
23
23
|
graphqlWsServer;
|
|
24
|
-
constructor(app, httpServer, redisClient, moduleService, enableSubscription = true, cacheKeyGenerator, invalidateCacheKeyGenerator) {
|
|
24
|
+
constructor(app, httpServer, redisClient, moduleService, enableSubscription = true, cacheKeyGenerator, invalidateCacheKeyGenerator, plugins) {
|
|
25
25
|
this.app = app;
|
|
26
26
|
this.httpServer = httpServer;
|
|
27
27
|
this.redisClient = redisClient;
|
|
@@ -29,6 +29,7 @@ class GraphqlServer {
|
|
|
29
29
|
this.enableSubscription = enableSubscription;
|
|
30
30
|
this.cacheKeyGenerator = cacheKeyGenerator;
|
|
31
31
|
this.invalidateCacheKeyGenerator = invalidateCacheKeyGenerator;
|
|
32
|
+
this.plugins = plugins;
|
|
32
33
|
this.logger = this.moduleService.logger.child({ className: 'GraphqlServer' });
|
|
33
34
|
if (enableSubscription) {
|
|
34
35
|
const wsServer = new ws.WebSocketServer({
|
|
@@ -40,7 +41,9 @@ class GraphqlServer {
|
|
|
40
41
|
}
|
|
41
42
|
async initialize() {
|
|
42
43
|
this.logger.info('GraphqlServer initializing...');
|
|
43
|
-
this.graphqlWsServer
|
|
44
|
+
if (this.graphqlWsServer) {
|
|
45
|
+
this.graphqlWsServer.create();
|
|
46
|
+
}
|
|
44
47
|
const apolloServer = this.configureApolloServer();
|
|
45
48
|
await apolloServer.start();
|
|
46
49
|
const corsOptions = {
|
|
@@ -108,20 +111,12 @@ class GraphqlServer {
|
|
|
108
111
|
this.logger.error(err, 'Redis connection error:');
|
|
109
112
|
});
|
|
110
113
|
const keyvCache = new Keyv({ store: redisStore, namespace: envConfig.config.APP_NAME });
|
|
111
|
-
const isCacheable = (requestContext) => {
|
|
112
|
-
const cache = requestContext.contextValue.overallCachePolicy;
|
|
113
|
-
if (!cache)
|
|
114
|
-
return false;
|
|
115
|
-
// eslint-disable-next-line no-param-reassign
|
|
116
|
-
requestContext.overallCachePolicy.maxAge = cache.maxAge;
|
|
117
|
-
return requestContext.overallCachePolicy.maxAge > 0;
|
|
118
|
-
};
|
|
119
114
|
const cacheAdapter = new utils_keyvadapter.KeyvAdapter(keyvCache);
|
|
120
115
|
const cacheGet = cacheAdapter.get.bind(cacheAdapter);
|
|
121
116
|
cacheAdapter.get = (key) => cacheGet(key.replaceAll('fqc:', ''));
|
|
122
117
|
const cacheSet = cacheAdapter.set.bind(cacheAdapter);
|
|
123
118
|
cacheAdapter.set = (key, value, opts) => cacheSet(key.replaceAll('fqc:', ''), value, opts);
|
|
124
|
-
const { cacheKeyGenerator, invalidateCacheKeyGenerator } = this;
|
|
119
|
+
const { cacheKeyGenerator, invalidateCacheKeyGenerator, logger } = this;
|
|
125
120
|
const serverConfig = {
|
|
126
121
|
schema: this.moduleService.schema,
|
|
127
122
|
allowBatchedHttpRequests: true,
|
|
@@ -129,30 +124,9 @@ class GraphqlServer {
|
|
|
129
124
|
plugins: [
|
|
130
125
|
drainHttpServer.ApolloServerPluginDrainHttpServer({ httpServer: this.httpServer }),
|
|
131
126
|
cacheControl.ApolloServerPluginCacheControl(),
|
|
132
|
-
responseCachePlugin({
|
|
133
|
-
sessionId: ({ contextValue }) => Promise.resolve(contextValue?.user?.sub ?? null),
|
|
134
|
-
generateCacheKey(requestContext) {
|
|
135
|
-
const { queryHash, request } = requestContext;
|
|
136
|
-
const [, queryName] = request.query?.match(/{\s*(\w+)/) ?? [];
|
|
137
|
-
const cacheKey = `${queryName}:${queryHash}`;
|
|
138
|
-
try {
|
|
139
|
-
if (typeof cacheKeyGenerator === 'function') {
|
|
140
|
-
const generatedKey = cacheKeyGenerator(requestContext, cacheKey);
|
|
141
|
-
return generatedKey || cacheKey;
|
|
142
|
-
}
|
|
143
|
-
return cacheKey;
|
|
144
|
-
}
|
|
145
|
-
catch (e) {
|
|
146
|
-
// `this.logger` won't work here
|
|
147
|
-
console.warn('GenerateCacheKey Failed %s', e.message);
|
|
148
|
-
return cacheKey;
|
|
149
|
-
}
|
|
150
|
-
},
|
|
151
|
-
shouldWriteToCache: (ctx) =>
|
|
152
|
-
// Cache only successful responses
|
|
153
|
-
isCacheable(ctx) && _.isEmpty(ctx?.response?.body?.singleResult.errors),
|
|
154
|
-
}),
|
|
155
127
|
invalidateCachePlugin.invalidateCachePlugin({ cache: this.redisClient, invalidateCacheKeyGenerator }),
|
|
128
|
+
responseCachePlugin.responseCachePlugin({ logger, cacheKeyGenerator }),
|
|
129
|
+
...(this.plugins ?? []),
|
|
156
130
|
],
|
|
157
131
|
};
|
|
158
132
|
if (this.enableSubscription) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GraphqlServer.cjs","sources":["../../src/servers/GraphqlServer.ts"],"sourcesContent":[null],"names":["WebSocketServer","config","GraphqlWs","expressMiddleware","KeyvAdapter","ApolloServerPluginDrainHttpServer","ApolloServerPluginCacheControl","
|
|
1
|
+
{"version":3,"file":"GraphqlServer.cjs","sources":["../../src/servers/GraphqlServer.ts"],"sourcesContent":[null],"names":["WebSocketServer","config","GraphqlWs","expressMiddleware","KeyvAdapter","ApolloServerPluginDrainHttpServer","ApolloServerPluginCacheControl","invalidateCachePlugin","responseCachePlugin","ApolloServer"],"mappings":"moBAuBA,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,SAAS,IAAI,OAAO,CAAC,GAAG,CAAC,SAAS,KAAK,OAAO,KAAK,OAAO,CAAC,GAAG,CAAC,SAAS,KAAK,OAAO,EAAE,CAEtG;AAED;AACA;AACA,MAAM,oCAAoC,GAAG,CAAC,OAAO,EAAE,KAAK,EAAE,WAAW,KAAI;AACzE,IAAA,MAAM,oBAAoB,GAAG,CAAC,QAAQ,KAAI;QACtC,QAAQ,CAAC,UAAU,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC;AAC5C,KAAC,CAAC;AACF,IAAA,KAAK,MAAM,IAAI,IAAI,WAAW,EAAE;AAC5B,QAAA,oBAAo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OAAO,EAAE;gBACLC,iDAAiC,CAAC,EAAE,UAAU,EAAE,IAAI,CAAC,UAAU,EAAE,CAAC;AAClE,gBAAAC,2CAA8B,EAAE;gBAChCC,2CAAqB,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,WAAW,EAAE,2BAA2B,EAAE,CAAC;AAC/E,gBAAAC,uCAAmB,CAAC,EAAE,MAAM,EAAE,iBAAiB,EAAE,CAAC;AAClD,gBAAA,IAAI,IAAI,CAAC,OAAO,IAAI,EAAE,CAAC;AAC1B,aAAA;SACJ,CAAC;AAEF,QAAA,IAAI,IAAI,CAAC,kBAAkB,EAAE;AACzB,YAAA,YAAY,CAAC,OAAO,CAAC,IAAI,CAAC;AACtB,gBAAA,MAAM,eAAe,GAAA;AACjB,oBAAA,MAAM,QAAQ,GAAG;AACb,wBAAA,MAAM,WAAW,GAAA;AACb,4BAAA,MAAM,IAAI,EAAE,eAAe,EAAE,UAAU,EAAE,CAAC;yBAC7C;qBACJ,CAAC;AACF,oBAAA,QAAQ,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAChC,oBAAA,OAAO,QAAQ,CAAC;iBACnB;AACJ,aAAA,CAAC,CAAC;SACN;AAED,QAAA,OAAO,IAAIC,mBAAY,CAAC,YAAY,CAAC,CAAC;KACzC;AACJ"}
|
|
@@ -5,18 +5,20 @@ import { Express } from 'express';
|
|
|
5
5
|
import * as http from 'http';
|
|
6
6
|
import { InvalidationKeyGenerator } from '../plugins';
|
|
7
7
|
import { IModuleService } from '../interfaces';
|
|
8
|
+
import type { ApolloServerPlugin } from '@apollo/server';
|
|
8
9
|
export type KeyGenerator = (context: GraphQLRequestContext<unknown>, cacheKey?: string) => string;
|
|
9
10
|
export declare class GraphqlServer {
|
|
10
|
-
private app;
|
|
11
|
-
private httpServer;
|
|
12
|
-
private redisClient;
|
|
13
|
-
private moduleService;
|
|
14
|
-
private enableSubscription;
|
|
11
|
+
private readonly app;
|
|
12
|
+
private readonly httpServer;
|
|
13
|
+
private readonly redisClient;
|
|
14
|
+
private readonly moduleService;
|
|
15
|
+
private readonly enableSubscription;
|
|
15
16
|
private readonly cacheKeyGenerator?;
|
|
16
17
|
private readonly invalidateCacheKeyGenerator?;
|
|
18
|
+
private readonly plugins?;
|
|
17
19
|
private logger;
|
|
18
20
|
private graphqlWsServer;
|
|
19
|
-
constructor(app: Express, httpServer: http.Server, redisClient: IORedis.Redis | IORedis.Cluster, moduleService: IModuleService, enableSubscription?: boolean, cacheKeyGenerator?: KeyGenerator, invalidateCacheKeyGenerator?: InvalidationKeyGenerator);
|
|
21
|
+
constructor(app: Express, httpServer: http.Server, redisClient: IORedis.Redis | IORedis.Cluster, moduleService: IModuleService, enableSubscription?: boolean, cacheKeyGenerator?: KeyGenerator, invalidateCacheKeyGenerator?: InvalidationKeyGenerator, plugins?: ApolloServerPlugin<any>[]);
|
|
20
22
|
initialize(): Promise<void>;
|
|
21
23
|
getUserIpAddress(req: any): any;
|
|
22
24
|
private configureApolloServer;
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {ApolloServer}from'@apollo/server';import {expressMiddleware}from'@apollo/server/express4';import {ApolloServerPluginCacheControl}from'@apollo/server/plugin/cacheControl';import {ApolloServerPluginDrainHttpServer}from'@apollo/server/plugin/drainHttpServer';import
|
|
2
|
-
if ((process.env.LOG_LEVEL && process.env.LOG_LEVEL === 'trace') || process.env.LOG_LEVEL === 'debug') ;
|
|
1
|
+
import {ApolloServer}from'@apollo/server';import {expressMiddleware}from'@apollo/server/express4';import {ApolloServerPluginCacheControl}from'@apollo/server/plugin/cacheControl';import {ApolloServerPluginDrainHttpServer}from'@apollo/server/plugin/drainHttpServer';import'isomorphic-fetch';import express from'express';import cors from'cors';import Keyv from'keyv';import KeyvRedis from'@keyv/redis';import {KeyvAdapter}from'@apollo/utils.keyvadapter';import {WebSocketServer}from'ws';import {invalidateCachePlugin}from'../plugins/invalidateCachePlugin.mjs';import {responseCachePlugin}from'../plugins/responseCachePlugin.mjs';import {GraphqlWs}from'./GraphqlWs.mjs';import {config}from'../config/env-config.mjs';if ((process.env.LOG_LEVEL && process.env.LOG_LEVEL === 'trace') || process.env.LOG_LEVEL === 'debug') ;
|
|
3
2
|
// @workaround as the `dataSources` not available in Subscription (websocket) Context.
|
|
4
3
|
// https://github.com/apollographql/apollo-server/issues/1526 need to revisit in Apollo-Server v3.
|
|
5
4
|
const constructDataSourcesForSubscriptions = (context, cache, dataSources) => {
|
|
@@ -19,9 +18,10 @@ class GraphqlServer {
|
|
|
19
18
|
enableSubscription;
|
|
20
19
|
cacheKeyGenerator;
|
|
21
20
|
invalidateCacheKeyGenerator;
|
|
21
|
+
plugins;
|
|
22
22
|
logger;
|
|
23
23
|
graphqlWsServer;
|
|
24
|
-
constructor(app, httpServer, redisClient, moduleService, enableSubscription = true, cacheKeyGenerator, invalidateCacheKeyGenerator) {
|
|
24
|
+
constructor(app, httpServer, redisClient, moduleService, enableSubscription = true, cacheKeyGenerator, invalidateCacheKeyGenerator, plugins) {
|
|
25
25
|
this.app = app;
|
|
26
26
|
this.httpServer = httpServer;
|
|
27
27
|
this.redisClient = redisClient;
|
|
@@ -29,6 +29,7 @@ class GraphqlServer {
|
|
|
29
29
|
this.enableSubscription = enableSubscription;
|
|
30
30
|
this.cacheKeyGenerator = cacheKeyGenerator;
|
|
31
31
|
this.invalidateCacheKeyGenerator = invalidateCacheKeyGenerator;
|
|
32
|
+
this.plugins = plugins;
|
|
32
33
|
this.logger = this.moduleService.logger.child({ className: 'GraphqlServer' });
|
|
33
34
|
if (enableSubscription) {
|
|
34
35
|
const wsServer = new WebSocketServer({
|
|
@@ -40,7 +41,9 @@ class GraphqlServer {
|
|
|
40
41
|
}
|
|
41
42
|
async initialize() {
|
|
42
43
|
this.logger.info('GraphqlServer initializing...');
|
|
43
|
-
this.graphqlWsServer
|
|
44
|
+
if (this.graphqlWsServer) {
|
|
45
|
+
this.graphqlWsServer.create();
|
|
46
|
+
}
|
|
44
47
|
const apolloServer = this.configureApolloServer();
|
|
45
48
|
await apolloServer.start();
|
|
46
49
|
const corsOptions = {
|
|
@@ -108,20 +111,12 @@ class GraphqlServer {
|
|
|
108
111
|
this.logger.error(err, 'Redis connection error:');
|
|
109
112
|
});
|
|
110
113
|
const keyvCache = new Keyv({ store: redisStore, namespace: config.APP_NAME });
|
|
111
|
-
const isCacheable = (requestContext) => {
|
|
112
|
-
const cache = requestContext.contextValue.overallCachePolicy;
|
|
113
|
-
if (!cache)
|
|
114
|
-
return false;
|
|
115
|
-
// eslint-disable-next-line no-param-reassign
|
|
116
|
-
requestContext.overallCachePolicy.maxAge = cache.maxAge;
|
|
117
|
-
return requestContext.overallCachePolicy.maxAge > 0;
|
|
118
|
-
};
|
|
119
114
|
const cacheAdapter = new KeyvAdapter(keyvCache);
|
|
120
115
|
const cacheGet = cacheAdapter.get.bind(cacheAdapter);
|
|
121
116
|
cacheAdapter.get = (key) => cacheGet(key.replaceAll('fqc:', ''));
|
|
122
117
|
const cacheSet = cacheAdapter.set.bind(cacheAdapter);
|
|
123
118
|
cacheAdapter.set = (key, value, opts) => cacheSet(key.replaceAll('fqc:', ''), value, opts);
|
|
124
|
-
const { cacheKeyGenerator, invalidateCacheKeyGenerator } = this;
|
|
119
|
+
const { cacheKeyGenerator, invalidateCacheKeyGenerator, logger } = this;
|
|
125
120
|
const serverConfig = {
|
|
126
121
|
schema: this.moduleService.schema,
|
|
127
122
|
allowBatchedHttpRequests: true,
|
|
@@ -129,30 +124,9 @@ class GraphqlServer {
|
|
|
129
124
|
plugins: [
|
|
130
125
|
ApolloServerPluginDrainHttpServer({ httpServer: this.httpServer }),
|
|
131
126
|
ApolloServerPluginCacheControl(),
|
|
132
|
-
responseCachePlugin({
|
|
133
|
-
sessionId: ({ contextValue }) => Promise.resolve(contextValue?.user?.sub ?? null),
|
|
134
|
-
generateCacheKey(requestContext) {
|
|
135
|
-
const { queryHash, request } = requestContext;
|
|
136
|
-
const [, queryName] = request.query?.match(/{\s*(\w+)/) ?? [];
|
|
137
|
-
const cacheKey = `${queryName}:${queryHash}`;
|
|
138
|
-
try {
|
|
139
|
-
if (typeof cacheKeyGenerator === 'function') {
|
|
140
|
-
const generatedKey = cacheKeyGenerator(requestContext, cacheKey);
|
|
141
|
-
return generatedKey || cacheKey;
|
|
142
|
-
}
|
|
143
|
-
return cacheKey;
|
|
144
|
-
}
|
|
145
|
-
catch (e) {
|
|
146
|
-
// `this.logger` won't work here
|
|
147
|
-
console.warn('GenerateCacheKey Failed %s', e.message);
|
|
148
|
-
return cacheKey;
|
|
149
|
-
}
|
|
150
|
-
},
|
|
151
|
-
shouldWriteToCache: (ctx) =>
|
|
152
|
-
// Cache only successful responses
|
|
153
|
-
isCacheable(ctx) && isEmpty(ctx?.response?.body?.singleResult.errors),
|
|
154
|
-
}),
|
|
155
127
|
invalidateCachePlugin({ cache: this.redisClient, invalidateCacheKeyGenerator }),
|
|
128
|
+
responseCachePlugin({ logger, cacheKeyGenerator }),
|
|
129
|
+
...(this.plugins ?? []),
|
|
156
130
|
],
|
|
157
131
|
};
|
|
158
132
|
if (this.enableSubscription) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GraphqlServer.mjs","sources":["../../src/servers/GraphqlServer.ts"],"sourcesContent":[null],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"GraphqlServer.mjs","sources":["../../src/servers/GraphqlServer.ts"],"sourcesContent":[null],"names":[],"mappings":"wsBAuBA,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,SAAS,IAAI,OAAO,CAAC,GAAG,CAAC,SAAS,KAAK,OAAO,KAAK,OAAO,CAAC,GAAG,CAAC,SAAS,KAAK,OAAO,EAAE,CAEtG;AAED;AACA;AACA,MAAM,oCAAoC,GAAG,CAAC,OAAO,EAAE,KAAK,EAAE,WAAW,KAAI;AACzE,IAAA,MAAM,oBAAoB,GAAG,CAAC,QAAQ,KAAI;QACtC,QAAQ,CAAC,UAAU,CAAC,EAAE,OAAO,EAAE,K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Aa,CAAC,MAAM;AACjC,YAAA,wBAAwB,EAAE,IAAI;AAC9B,YAAA,KAAK,EAAE,YAAY;AACnB,YAAA,OAAO,EAAE;gBACL,iCAAiC,CAAC,EAAE,UAAU,EAAE,IAAI,CAAC,UAAU,EAAE,CAAC;AAClE,gBAAA,8BAA8B,EAAE;gBAChC,qBAAqB,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,WAAW,EAAE,2BAA2B,EAAE,CAAC;AAC/E,gBAAA,mBAAmB,CAAC,EAAE,MAAM,EAAE,iBAAiB,EAAE,CAAC;AAClD,gBAAA,IAAI,IAAI,CAAC,OAAO,IAAI,EAAE,CAAC;AAC1B,aAAA;SACJ,CAAC;AAEF,QAAA,IAAI,IAAI,CAAC,kBAAkB,EAAE;AACzB,YAAA,YAAY,CAAC,OAAO,CAAC,IAAI,CAAC;AACtB,gBAAA,MAAM,eAAe,GAAA;AACjB,oBAAA,MAAM,QAAQ,GAAG;AACb,wBAAA,MAAM,WAAW,GAAA;AACb,4BAAA,MAAM,IAAI,EAAE,eAAe,EAAE,UAAU,EAAE,CAAC;yBAC7C;qBACJ,CAAC;AACF,oBAAA,QAAQ,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAChC,oBAAA,OAAO,QAAQ,CAAC;iBACnB;AACJ,aAAA,CAAC,CAAC;SACN;AAED,QAAA,OAAO,IAAI,YAAY,CAAC,YAAY,CAAC,CAAC;KACzC;AACJ"}
|
|
@@ -1,18 +1,4 @@
|
|
|
1
|
-
'use strict';var ws=require('graphql-ws/lib/use/ws');require('graphql')
|
|
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
|
-
if (dataSources[prop] && typeof dataSources[prop]?.initialize === 'function') {
|
|
10
|
-
intializeDataSource(dataSources[prop]);
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
return dataSources;
|
|
14
|
-
};
|
|
15
|
-
class GraphqlWs {
|
|
1
|
+
'use strict';var ws=require('graphql-ws/lib/use/ws');require('graphql');var websocketContext=require('./websocket-context.cjs');class GraphqlWs {
|
|
16
2
|
wsServer;
|
|
17
3
|
moduleService;
|
|
18
4
|
cache;
|
|
@@ -28,35 +14,8 @@ class GraphqlWs {
|
|
|
28
14
|
this.subscriptionServer = ws.useServer({
|
|
29
15
|
schema: this.moduleService.schema,
|
|
30
16
|
// Adding a context property lets you add data to your GraphQL operation context
|
|
31
|
-
context
|
|
32
|
-
|
|
33
|
-
// GraphQL context, which all of our resolvers have access to.
|
|
34
|
-
// ctx is the `graphql-ws` Context where connectionParams live
|
|
35
|
-
try {
|
|
36
|
-
const pureContext = await this.moduleService.createContext(ctx.connectionParams, null);
|
|
37
|
-
const contextServices = await this.moduleService.serviceContext(ctx.connectionParams, null);
|
|
38
|
-
const context = {
|
|
39
|
-
...contextServices,
|
|
40
|
-
...pureContext,
|
|
41
|
-
preferences: this.moduleService.defaultPreferences,
|
|
42
|
-
// update: updateContainers,
|
|
43
|
-
...ctx,
|
|
44
|
-
};
|
|
45
|
-
const addons = {
|
|
46
|
-
dataSources: constructDataSourcesForSubscriptions(context, this.cache, this.moduleService.dataSource),
|
|
47
|
-
};
|
|
48
|
-
return {
|
|
49
|
-
...context,
|
|
50
|
-
...addons,
|
|
51
|
-
...ctx,
|
|
52
|
-
...pureContext,
|
|
53
|
-
preferences: this.moduleService.defaultPreferences,
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
catch (err) {
|
|
57
|
-
this.logger.error(err);
|
|
58
|
-
}
|
|
59
|
-
},
|
|
17
|
+
// Using shared context creation function to maintain consistency
|
|
18
|
+
context: async (ctx) => websocketContext.createWebSocketContext(ctx, this.moduleService, this.cache),
|
|
60
19
|
// onConnect: async (ctx) => {
|
|
61
20
|
// // do anything when connected
|
|
62
21
|
// },
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GraphqlWs.cjs","sources":["../../src/servers/GraphqlWs.ts"],"sourcesContent":[null],"names":["useServer"],"mappings":"
|
|
1
|
+
{"version":3,"file":"GraphqlWs.cjs","sources":["../../src/servers/GraphqlWs.ts"],"sourcesContent":[null],"names":["useServer","createWebSocketContext"],"mappings":"sIAmDa,SAAS,CAAA;AAMN,IAAA,QAAA,CAAA;AACA,IAAA,aAAA,CAAA;AACE,IAAA,KAAA,CAAA;AAPN,IAAA,kBAAkB,CAAa;AAE/B,IAAA,MAAM,CAAU;AAExB,IAAA,WAAA,CACY,QAAyB,EACzB,aAA6B,EAC3B,KAAsC,EAAA;QAFxC,IAAQ,CAAA,QAAA,GAAR,QAAQ,CAAiB;QACzB,IAAa,CAAA,aAAA,GAAb,aAAa,CAAgB;QAC3B,IAAK,CAAA,KAAA,GAAL,KAAK,CAAiC;QAEhD,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC;KAC3C;IAEM,MAAM,GAAA;AACT,QAAA,IAAI,CAAC,kBAAkB,GAAGA,YAAS,CAC/B;AACI,YAAA,MAAM,EAAE,IAAI,CAAC,aAAa,CAAC,MAAM;;;AAGjC,YAAA,OAAO,EAAE,OAAO,GAAG,KAAKC,uCAAsB,CAAC,GAAG,EAAE,IAAI,CAAC,aAAa,EAAE,IAAI,CAAC,KAAK,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgCtF,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"}
|
|
@@ -1,18 +1,4 @@
|
|
|
1
|
-
import {useServer}from'graphql-ws/lib/use/ws';import'graphql'
|
|
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
|
-
if (dataSources[prop] && typeof dataSources[prop]?.initialize === 'function') {
|
|
10
|
-
intializeDataSource(dataSources[prop]);
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
return dataSources;
|
|
14
|
-
};
|
|
15
|
-
class GraphqlWs {
|
|
1
|
+
import {useServer}from'graphql-ws/lib/use/ws';import'graphql';import {createWebSocketContext}from'./websocket-context.mjs';class GraphqlWs {
|
|
16
2
|
wsServer;
|
|
17
3
|
moduleService;
|
|
18
4
|
cache;
|
|
@@ -28,35 +14,8 @@ class GraphqlWs {
|
|
|
28
14
|
this.subscriptionServer = useServer({
|
|
29
15
|
schema: this.moduleService.schema,
|
|
30
16
|
// Adding a context property lets you add data to your GraphQL operation context
|
|
31
|
-
context
|
|
32
|
-
|
|
33
|
-
// GraphQL context, which all of our resolvers have access to.
|
|
34
|
-
// ctx is the `graphql-ws` Context where connectionParams live
|
|
35
|
-
try {
|
|
36
|
-
const pureContext = await this.moduleService.createContext(ctx.connectionParams, null);
|
|
37
|
-
const contextServices = await this.moduleService.serviceContext(ctx.connectionParams, null);
|
|
38
|
-
const context = {
|
|
39
|
-
...contextServices,
|
|
40
|
-
...pureContext,
|
|
41
|
-
preferences: this.moduleService.defaultPreferences,
|
|
42
|
-
// update: updateContainers,
|
|
43
|
-
...ctx,
|
|
44
|
-
};
|
|
45
|
-
const addons = {
|
|
46
|
-
dataSources: constructDataSourcesForSubscriptions(context, this.cache, this.moduleService.dataSource),
|
|
47
|
-
};
|
|
48
|
-
return {
|
|
49
|
-
...context,
|
|
50
|
-
...addons,
|
|
51
|
-
...ctx,
|
|
52
|
-
...pureContext,
|
|
53
|
-
preferences: this.moduleService.defaultPreferences,
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
catch (err) {
|
|
57
|
-
this.logger.error(err);
|
|
58
|
-
}
|
|
59
|
-
},
|
|
17
|
+
// Using shared context creation function to maintain consistency
|
|
18
|
+
context: async (ctx) => createWebSocketContext(ctx, this.moduleService, this.cache),
|
|
60
19
|
// onConnect: async (ctx) => {
|
|
61
20
|
// // do anything when connected
|
|
62
21
|
// },
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GraphqlWs.mjs","sources":["../../src/servers/GraphqlWs.ts"],"sourcesContent":[null],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"GraphqlWs.mjs","sources":["../../src/servers/GraphqlWs.ts"],"sourcesContent":[null],"names":[],"mappings":"iIAmDa,SAAS,CAAA;AAMN,IAAA,QAAA,CAAA;AACA,IAAA,aAAA,CAAA;AACE,IAAA,KAAA,CAAA;AAPN,IAAA,kBAAkB,CAAa;AAE/B,IAAA,MAAM,CAAU;AAExB,IAAA,WAAA,CACY,QAAyB,EACzB,aAA6B,EAC3B,KAAsC,EAAA;QAFxC,IAAQ,CAAA,QAAA,GAAR,QAAQ,CAAiB;QACzB,IAAa,CAAA,aAAA,GAAb,aAAa,CAAgB;QAC3B,IAAK,CAAA,KAAA,GAAL,KAAK,CAAiC;QAEhD,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC;KAC3C;IAEM,MAAM,GAAA;AACT,QAAA,IAAI,CAAC,kBAAkB,GAAG,SAAS,CAC/B;AACI,YAAA,MAAM,EAAE,IAAI,CAAC,aAAa,CAAC,MAAM;;;AAGjC,YAAA,OAAO,EAAE,OAAO,GAAG,KAAK,sBAAsB,CAAC,GAAG,EAAE,IAAI,CAAC,aAAa,EAAE,IAAI,CAAC,KAAK,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgCtF,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"}
|
|
@@ -1,4 +1,4 @@
|
|
|
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 {
|
|
1
|
+
'use strict';var url=require('url'),ws=require('ws'),ws$1=require('graphql-ws/lib/use/ws'),websocketContext=require('./websocket-context.cjs');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
2
|
moduleService;
|
|
3
3
|
cache;
|
|
4
4
|
webSockets = {};
|
|
@@ -9,10 +9,12 @@
|
|
|
9
9
|
this.cache = cache;
|
|
10
10
|
this._graphqlWs = new ws.WebSocketServer({ noServer: true, path: __GRAPHQL_ENDPOINT__ });
|
|
11
11
|
this.webSockets[__GRAPHQL_ENDPOINT__] = this._graphqlWs;
|
|
12
|
-
this.webSockets[__GRAPHQL_ENDPOINT__].on('connection', (
|
|
12
|
+
this.webSockets[__GRAPHQL_ENDPOINT__].on('connection', () => {
|
|
13
13
|
console.log('--REQUESTED CONNECTION--', this._graphqlWs);
|
|
14
14
|
ws$1.useServer({
|
|
15
15
|
schema: moduleService.schema,
|
|
16
|
+
// Using shared context creation function to maintain consistency with GraphqlWs
|
|
17
|
+
context: async (ctx) => websocketContext.createWebSocketContext(ctx, moduleService, cache),
|
|
16
18
|
}, this._graphqlWs);
|
|
17
19
|
});
|
|
18
20
|
for (let key in multiplePathConfig) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WebsocketMultipathUpdate.cjs","sources":["../../src/servers/WebsocketMultipathUpdate.ts"],"sourcesContent":[null],"names":["WebSocketServer","useServer","url"],"mappings":"
|
|
1
|
+
{"version":3,"file":"WebsocketMultipathUpdate.cjs","sources":["../../src/servers/WebsocketMultipathUpdate.ts"],"sourcesContent":[null],"names":["WebSocketServer","useServer","createWebSocketContext","url"],"mappings":"ifAea,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,KAAsC,EAC7C,kBAAyC,EAAA;QAFlC,IAAa,CAAA,aAAA,GAAb,aAAa,CAAgB;QAC7B,IAAK,CAAA,KAAA,GAAL,KAAK,CAAiC;AAG7C,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;QACxD,IAAI,CAAC,UAAU,CAAC,oBAAoB,CAAC,CAAC,EAAE,CAAC,YAAY,EAAE,MAAK;YACxD,OAAO,CAAC,GAAG,CAAC,0BAA0B,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;AACzD,YAAAC,cAAS,CAC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