@common-stack/server-stack 7.0.4-alpha.9 → 7.1.1-alpha.14
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 +6 -3
- package/lib/MainStackServer.cjs.map +1 -1
- package/lib/MainStackServer.d.ts +2 -1
- package/lib/MainStackServer.mjs +6 -3
- package/lib/MainStackServer.mjs.map +1 -1
- package/lib/api/root-schema.graphqls.cjs +1 -1
- package/lib/api/root-schema.graphqls.mjs +1 -1
- package/lib/api/sub-graph-schema-builder.cjs +149 -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 +149 -0
- package/lib/api/sub-graph-schema-builder.mjs.map +1 -0
- package/lib/interfaces/graphql-request-context.d.ts +1 -0
- package/lib/plugins/index.d.ts +1 -0
- package/lib/plugins/invalidateCachePlugin.cjs +31 -18
- package/lib/plugins/invalidateCachePlugin.cjs.map +1 -1
- package/lib/plugins/invalidateCachePlugin.mjs +31 -18
- package/lib/plugins/invalidateCachePlugin.mjs.map +1 -1
- package/lib/plugins/invalidateCachePlugin.test.d.ts +1 -0
- package/lib/plugins/response-cache-plugin.test.d.ts +1 -0
- package/lib/plugins/responseCachePlugin.cjs +64 -0
- package/lib/plugins/responseCachePlugin.cjs.map +1 -0
- package/lib/plugins/responseCachePlugin.d.ts +12 -0
- package/lib/plugins/responseCachePlugin.mjs +64 -0
- package/lib/plugins/responseCachePlugin.mjs.map +1 -0
- package/lib/servers/GraphqlServer.cjs +2 -48
- package/lib/servers/GraphqlServer.cjs.map +1 -1
- package/lib/servers/GraphqlServer.mjs +2 -48
- package/lib/servers/GraphqlServer.mjs.map +1 -1
- 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/add-shareable-directive-to-schema.test.d.ts +1 -0
- package/lib/utils/index.d.ts +1 -0
- package/package.json +8 -7
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
'use strict';var
|
|
1
|
+
'use strict';var _=require('lodash-es'),serverCore=require('@common-stack/server-core'),envConfig=require('../config/env-config.cjs');const invalidateCachePlugin = ({ cache: redisClient, invalidateCacheKeyGenerator, }) => ({
|
|
2
2
|
requestDidStart(requestContext) {
|
|
3
3
|
return {
|
|
4
4
|
willSendResponse: async (responseContext) => {
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
}
|
|
9
9
|
try {
|
|
10
10
|
const hasErrors = !!requestContext.errors?.length;
|
|
11
|
+
const { schema } = requestContext;
|
|
11
12
|
const { queriesToInvalidate, user, req } = requestContext.contextValue;
|
|
12
13
|
const tenantId = serverCore.extractTenantId(req?.currentPageUriSegments?.authority);
|
|
13
14
|
const [{ operation }] = responseContext.document.definitions;
|
|
@@ -15,29 +16,41 @@
|
|
|
15
16
|
if (hasErrors || !queriesToInvalidate?.length || !isMutation) {
|
|
16
17
|
return;
|
|
17
18
|
}
|
|
18
|
-
// this to get the
|
|
19
19
|
const nestedKeys = await Promise.all(queriesToInvalidate.map(async (query) => {
|
|
20
|
-
|
|
21
|
-
let
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
20
|
+
// Build keys in order of specificity
|
|
21
|
+
let keys = [];
|
|
22
|
+
const cachePolicy = serverCore.getDirectiveArgsFromSchema({
|
|
23
|
+
schema,
|
|
24
|
+
queryName: query,
|
|
25
|
+
directiveName: serverCore.CACHE_CONTROL_DIRECTIVE,
|
|
26
|
+
});
|
|
27
|
+
const isPrivate = cachePolicy.scope?.toLowerCase() === 'private';
|
|
28
|
+
// Add tenant-specific key if tenant exists
|
|
29
|
+
if (tenantId) {
|
|
30
|
+
keys.push(`${envConfig.config.APP_NAME}:${tenantId}:${query}:*`);
|
|
31
|
+
// Add user-specific key if user exists
|
|
32
|
+
if (user?.sub && isPrivate) {
|
|
33
|
+
keys.push(`${envConfig.config.APP_NAME}:${tenantId}:${user.sub}:${query}:*`);
|
|
28
34
|
}
|
|
29
35
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
36
|
+
// Add global key as fallback
|
|
37
|
+
keys.push(`${envConfig.config.APP_NAME}:${query}:*`);
|
|
38
|
+
// Allow custom key generation if provided
|
|
39
|
+
if (typeof invalidateCacheKeyGenerator === 'function') {
|
|
40
|
+
keys = keys.map((key) => invalidateCacheKeyGenerator(requestContext, responseContext, key));
|
|
41
|
+
}
|
|
42
|
+
const matchedKeys = await Promise.all(keys.map(async (key) => {
|
|
43
|
+
const matchingKeys = await redisClient.keys(key);
|
|
44
|
+
if (matchingKeys.length) {
|
|
45
|
+
return matchingKeys;
|
|
46
|
+
}
|
|
47
|
+
return [];
|
|
48
|
+
}));
|
|
49
|
+
return matchedKeys.flat();
|
|
37
50
|
}));
|
|
38
51
|
const keys = nestedKeys.flat();
|
|
39
52
|
if (keys?.length) {
|
|
40
|
-
await redisClient.del(keys);
|
|
53
|
+
await redisClient.del(_.uniq(keys));
|
|
41
54
|
}
|
|
42
55
|
}
|
|
43
56
|
catch (e) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"invalidateCachePlugin.cjs","sources":["../../src/plugins/invalidateCachePlugin.ts"],"sourcesContent":[null],"names":["extractTenantId","config"],"mappings":"
|
|
1
|
+
{"version":3,"file":"invalidateCachePlugin.cjs","sources":["../../src/plugins/invalidateCachePlugin.ts"],"sourcesContent":[null],"names":["extractTenantId","getDirectiveArgsFromSchema","CACHE_CONTROL_DIRECTIVE","config","uniq"],"mappings":"sIAcO,MAAM,qBAAqB,GAAG,CAAC,EAClC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A,cAAc,CAAC,MAAM,CAAC,KAAK,CAAC,yCAAyC,CAAC,CAAC;AACvE,oBAAA,cAAc,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;iBAClC;aACJ;SACJ,CAAC;KACL;AACJ,CAAA"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {uniq}from'lodash-es';import {extractTenantId,getDirectiveArgsFromSchema,CACHE_CONTROL_DIRECTIVE}from'@common-stack/server-core';import {config}from'../config/env-config.mjs';const invalidateCachePlugin = ({ cache: redisClient, invalidateCacheKeyGenerator, }) => ({
|
|
2
2
|
requestDidStart(requestContext) {
|
|
3
3
|
return {
|
|
4
4
|
willSendResponse: async (responseContext) => {
|
|
@@ -8,6 +8,7 @@ import {config}from'../config/env-config.mjs';import {extractTenantId}from'@comm
|
|
|
8
8
|
}
|
|
9
9
|
try {
|
|
10
10
|
const hasErrors = !!requestContext.errors?.length;
|
|
11
|
+
const { schema } = requestContext;
|
|
11
12
|
const { queriesToInvalidate, user, req } = requestContext.contextValue;
|
|
12
13
|
const tenantId = extractTenantId(req?.currentPageUriSegments?.authority);
|
|
13
14
|
const [{ operation }] = responseContext.document.definitions;
|
|
@@ -15,29 +16,41 @@ import {config}from'../config/env-config.mjs';import {extractTenantId}from'@comm
|
|
|
15
16
|
if (hasErrors || !queriesToInvalidate?.length || !isMutation) {
|
|
16
17
|
return;
|
|
17
18
|
}
|
|
18
|
-
// this to get the
|
|
19
19
|
const nestedKeys = await Promise.all(queriesToInvalidate.map(async (query) => {
|
|
20
|
-
|
|
21
|
-
let
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
20
|
+
// Build keys in order of specificity
|
|
21
|
+
let keys = [];
|
|
22
|
+
const cachePolicy = getDirectiveArgsFromSchema({
|
|
23
|
+
schema,
|
|
24
|
+
queryName: query,
|
|
25
|
+
directiveName: CACHE_CONTROL_DIRECTIVE,
|
|
26
|
+
});
|
|
27
|
+
const isPrivate = cachePolicy.scope?.toLowerCase() === 'private';
|
|
28
|
+
// Add tenant-specific key if tenant exists
|
|
29
|
+
if (tenantId) {
|
|
30
|
+
keys.push(`${config.APP_NAME}:${tenantId}:${query}:*`);
|
|
31
|
+
// Add user-specific key if user exists
|
|
32
|
+
if (user?.sub && isPrivate) {
|
|
33
|
+
keys.push(`${config.APP_NAME}:${tenantId}:${user.sub}:${query}:*`);
|
|
28
34
|
}
|
|
29
35
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
36
|
+
// Add global key as fallback
|
|
37
|
+
keys.push(`${config.APP_NAME}:${query}:*`);
|
|
38
|
+
// Allow custom key generation if provided
|
|
39
|
+
if (typeof invalidateCacheKeyGenerator === 'function') {
|
|
40
|
+
keys = keys.map((key) => invalidateCacheKeyGenerator(requestContext, responseContext, key));
|
|
41
|
+
}
|
|
42
|
+
const matchedKeys = await Promise.all(keys.map(async (key) => {
|
|
43
|
+
const matchingKeys = await redisClient.keys(key);
|
|
44
|
+
if (matchingKeys.length) {
|
|
45
|
+
return matchingKeys;
|
|
46
|
+
}
|
|
47
|
+
return [];
|
|
48
|
+
}));
|
|
49
|
+
return matchedKeys.flat();
|
|
37
50
|
}));
|
|
38
51
|
const keys = nestedKeys.flat();
|
|
39
52
|
if (keys?.length) {
|
|
40
|
-
await redisClient.del(keys);
|
|
53
|
+
await redisClient.del(uniq(keys));
|
|
41
54
|
}
|
|
42
55
|
}
|
|
43
56
|
catch (e) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"invalidateCachePlugin.mjs","sources":["../../src/plugins/invalidateCachePlugin.ts"],"sourcesContent":[null],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"invalidateCachePlugin.mjs","sources":["../../src/plugins/invalidateCachePlugin.ts"],"sourcesContent":[null],"names":[],"mappings":"sLAcO,MAA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EAAE;AACR,oBAAA,cAAc,CAAC,MAAM,CAAC,KAAK,CAAC,yCAAyC,CAAC,CAAC;AACvE,oBAAA,cAAc,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;iBAClC;aACJ;SACJ,CAAC;KACL;AACJ,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
'use strict';var serverCore=require('@common-stack/server-core'),_=require('lodash-es'),apolloCachePlugin=require('@apollo/server-plugin-response-cache');const cachePlugin = apolloCachePlugin.default ?? apolloCachePlugin;
|
|
2
|
+
const isCacheable = (requestContext) => {
|
|
3
|
+
const { document, schema } = requestContext;
|
|
4
|
+
const cache = serverCore.getDirectiveArgsFromSchema({
|
|
5
|
+
schema,
|
|
6
|
+
document,
|
|
7
|
+
directiveName: serverCore.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 = serverCore.getDirectiveArgsFromSchema({
|
|
31
|
+
schema,
|
|
32
|
+
document,
|
|
33
|
+
directiveName: serverCore.CACHE_CONTROL_DIRECTIVE,
|
|
34
|
+
});
|
|
35
|
+
const { scope } = cacheControlDirective ?? {};
|
|
36
|
+
const isPrivate = scope?.toLowerCase() === 'private';
|
|
37
|
+
const tenantId = serverCore.extractTenantId(req?.currentPageUriSegments?.authority);
|
|
38
|
+
const cacheKey = serverCore.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
|
+
// `this.logger` won't work here
|
|
54
|
+
console.warn('GenerateCacheKey Failed %s', e.message);
|
|
55
|
+
return cacheKey;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
const responseCachePlugin = ({ logger, cacheKeyGenerator }) => cachePlugin({
|
|
59
|
+
sessionId: ({ contextValue }) => Promise.resolve(contextValue?.user?.sub ?? null),
|
|
60
|
+
generateCacheKey: generateCacheKey({ logger, cacheKeyGenerator }),
|
|
61
|
+
shouldWriteToCache: (ctx) =>
|
|
62
|
+
// Cache only successful responses
|
|
63
|
+
isCacheable(ctx) && _.isEmpty(ctx?.response?.body?.singleResult.errors),
|
|
64
|
+
});exports.generateCacheKey=generateCacheKey;exports.isCacheable=isCacheable;exports.responseCachePlugin=responseCachePlugin;//# sourceMappingURL=responseCachePlugin.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"responseCachePlugin.cjs","sources":["../../src/plugins/responseCachePlugin.ts"],"sour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GAAG,CAAC,IAAIC,SAAO,CAAC,GAAG,EAAE,QAAQ,EAAE,IAAI,EAAE,YAAY,CAAC,MAAM,CAAC;AAC5E,CAAA"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { GraphQLRequestContext } from '@apollo/server';
|
|
2
|
+
import { ILogger } from '@cdm-logger/core/lib/interface';
|
|
3
|
+
import { GraphqlRequestContext } from '../interfaces';
|
|
4
|
+
import { KeyGenerator } from '../servers/GraphqlServer';
|
|
5
|
+
type ApolloCachePluginOptions = {
|
|
6
|
+
logger: ILogger;
|
|
7
|
+
cacheKeyGenerator: KeyGenerator;
|
|
8
|
+
};
|
|
9
|
+
export declare const isCacheable: (requestContext: GraphQLRequestContext<GraphqlRequestContext>) => boolean;
|
|
10
|
+
export declare const generateCacheKey: ({ logger, cacheKeyGenerator }: ApolloCachePluginOptions) => (requestContext: GraphQLRequestContext<GraphqlRequestContext>) => string;
|
|
11
|
+
export declare const responseCachePlugin: ({ logger, cacheKeyGenerator }: ApolloCachePluginOptions) => any;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,64 @@
|
|
|
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 = 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
|
+
// `this.logger` won't work here
|
|
54
|
+
console.warn('GenerateCacheKey Failed %s', e.message);
|
|
55
|
+
return cacheKey;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
const responseCachePlugin = ({ logger, cacheKeyGenerator }) => cachePlugin({
|
|
59
|
+
sessionId: ({ contextValue }) => Promise.resolve(contextValue?.user?.sub ?? null),
|
|
60
|
+
generateCacheKey: generateCacheKey({ logger, cacheKeyGenerator }),
|
|
61
|
+
shouldWriteToCache: (ctx) =>
|
|
62
|
+
// Cache only successful responses
|
|
63
|
+
isCacheable(ctx) && isEmpty(ctx?.response?.body?.singleResult.errors),
|
|
64
|
+
});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;IAC5C,MAAM,KAAK,GAAG,0BAA0B,CAAC;QACrC,MAAM;QACN,QAAQ;AACR,QAAA,aAAa,EAAE,uBAAuB;AACzC,KAAA,CAAC,CAAC;AACH,IAAA,IAAI,CAAC,KAAK;AAAE,QAAA,OAAO,KAAK,CAAC;IACzB,IAAI,KAAK,CAAC,KAAK,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE;AAC9B,QAAA,KAAK,CAAC,MAAM,GAAG,KAAK,CAAC;KACxB;AACD,IAAA,IAAI,CAAC,cAAc,CAAC,kBAAkB,EAAE;;;AAGnC,QAAA,cAAsB,CAAC,kBAAkB,GAAG,EAAE,CAAC;KACnD;;IAED,cAAc,CAAC,kBAAkB,CAAC,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC;AACxD,IAAA,OAAO,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;AAC5B,EAAE;AAEW,MAAA,gBAAgB,GACzB,CAAC,EAAE,MAAM,EAAE,iBAAiB,EAA4B,KACxD,CAAC,cAA4D,KAAY;AACrE,IAAA,IAAI,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BAAiB,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,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) => {
|
|
@@ -108,14 +107,6 @@ class GraphqlServer {
|
|
|
108
107
|
this.logger.error(err, 'Redis connection error:');
|
|
109
108
|
});
|
|
110
109
|
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
110
|
const cacheAdapter = new utils_keyvadapter.KeyvAdapter(keyvCache);
|
|
120
111
|
const cacheGet = cacheAdapter.get.bind(cacheAdapter);
|
|
121
112
|
cacheAdapter.get = (key) => cacheGet(key.replaceAll('fqc:', ''));
|
|
@@ -129,45 +120,8 @@ class GraphqlServer {
|
|
|
129
120
|
plugins: [
|
|
130
121
|
drainHttpServer.ApolloServerPluginDrainHttpServer({ httpServer: this.httpServer }),
|
|
131
122
|
cacheControl.ApolloServerPluginCacheControl(),
|
|
132
|
-
responseCachePlugin({
|
|
133
|
-
sessionId: ({ contextValue }) => Promise.resolve(contextValue?.user?.sub ?? null),
|
|
134
|
-
generateCacheKey(requestContext) {
|
|
135
|
-
const { request, contextValue, document, schema } = requestContext;
|
|
136
|
-
const { user, req } = contextValue;
|
|
137
|
-
const { query, variables } = request;
|
|
138
|
-
const cacheControlDirective = serverCore.getDirectiveArgsFromSchema({
|
|
139
|
-
schema,
|
|
140
|
-
document,
|
|
141
|
-
directiveName: serverCore.CACHE_CONTROL_DIRECTIVE,
|
|
142
|
-
});
|
|
143
|
-
const { scope } = cacheControlDirective ?? {};
|
|
144
|
-
const isPrivate = scope?.toLowerCase() === 'private';
|
|
145
|
-
const tenantId = serverCore.extractTenantId(req?.currentPageUriSegments?.authority);
|
|
146
|
-
const cacheKey = serverCore.generateQueryCacheKey({
|
|
147
|
-
query,
|
|
148
|
-
variables,
|
|
149
|
-
logger,
|
|
150
|
-
userId: isPrivate ? user.sub : null,
|
|
151
|
-
tenantId,
|
|
152
|
-
});
|
|
153
|
-
try {
|
|
154
|
-
if (typeof cacheKeyGenerator === 'function') {
|
|
155
|
-
const generatedKey = cacheKeyGenerator(requestContext, cacheKey);
|
|
156
|
-
return generatedKey || cacheKey;
|
|
157
|
-
}
|
|
158
|
-
return cacheKey;
|
|
159
|
-
}
|
|
160
|
-
catch (e) {
|
|
161
|
-
// `this.logger` won't work here
|
|
162
|
-
console.warn('GenerateCacheKey Failed %s', e.message);
|
|
163
|
-
return cacheKey;
|
|
164
|
-
}
|
|
165
|
-
},
|
|
166
|
-
shouldWriteToCache: (ctx) =>
|
|
167
|
-
// Cache only successful responses
|
|
168
|
-
isCacheable(ctx) && _.isEmpty(ctx?.response?.body?.singleResult.errors),
|
|
169
|
-
}),
|
|
170
123
|
invalidateCachePlugin.invalidateCachePlugin({ cache: this.redisClient, invalidateCacheKeyGenerator }),
|
|
124
|
+
responseCachePlugin.responseCachePlugin({ logger, cacheKeyGenerator }),
|
|
171
125
|
],
|
|
172
126
|
};
|
|
173
127
|
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":"moBAsBA,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,SAAS,IAAI,OAAO,CAAC,GAAG,CAAC,SAAS,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G,MAAM,EAAE,iBAAiB,EAAE,2BAA2B,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;AACxE,QAAA,MAAM,YAAY,GAAqC;AACnD,YAAA,MAAM,EAAE,IAAI,CAAC,aAAa,CAAC,MAAM;AACjC,YAAA,wBAAwB,EAAE,IAAI;AAC9B,YAAA,KAAK,EAAE,YAAY;AACnB,YAAA,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;AACrD,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"}
|
|
@@ -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) => {
|
|
@@ -108,14 +107,6 @@ class GraphqlServer {
|
|
|
108
107
|
this.logger.error(err, 'Redis connection error:');
|
|
109
108
|
});
|
|
110
109
|
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
110
|
const cacheAdapter = new KeyvAdapter(keyvCache);
|
|
120
111
|
const cacheGet = cacheAdapter.get.bind(cacheAdapter);
|
|
121
112
|
cacheAdapter.get = (key) => cacheGet(key.replaceAll('fqc:', ''));
|
|
@@ -129,45 +120,8 @@ class GraphqlServer {
|
|
|
129
120
|
plugins: [
|
|
130
121
|
ApolloServerPluginDrainHttpServer({ httpServer: this.httpServer }),
|
|
131
122
|
ApolloServerPluginCacheControl(),
|
|
132
|
-
responseCachePlugin({
|
|
133
|
-
sessionId: ({ contextValue }) => Promise.resolve(contextValue?.user?.sub ?? null),
|
|
134
|
-
generateCacheKey(requestContext) {
|
|
135
|
-
const { request, contextValue, document, schema } = requestContext;
|
|
136
|
-
const { user, req } = contextValue;
|
|
137
|
-
const { query, variables } = request;
|
|
138
|
-
const cacheControlDirective = getDirectiveArgsFromSchema({
|
|
139
|
-
schema,
|
|
140
|
-
document,
|
|
141
|
-
directiveName: CACHE_CONTROL_DIRECTIVE,
|
|
142
|
-
});
|
|
143
|
-
const { scope } = cacheControlDirective ?? {};
|
|
144
|
-
const isPrivate = scope?.toLowerCase() === 'private';
|
|
145
|
-
const tenantId = extractTenantId(req?.currentPageUriSegments?.authority);
|
|
146
|
-
const cacheKey = generateQueryCacheKey({
|
|
147
|
-
query,
|
|
148
|
-
variables,
|
|
149
|
-
logger,
|
|
150
|
-
userId: isPrivate ? user.sub : null,
|
|
151
|
-
tenantId,
|
|
152
|
-
});
|
|
153
|
-
try {
|
|
154
|
-
if (typeof cacheKeyGenerator === 'function') {
|
|
155
|
-
const generatedKey = cacheKeyGenerator(requestContext, cacheKey);
|
|
156
|
-
return generatedKey || cacheKey;
|
|
157
|
-
}
|
|
158
|
-
return cacheKey;
|
|
159
|
-
}
|
|
160
|
-
catch (e) {
|
|
161
|
-
// `this.logger` won't work here
|
|
162
|
-
console.warn('GenerateCacheKey Failed %s', e.message);
|
|
163
|
-
return cacheKey;
|
|
164
|
-
}
|
|
165
|
-
},
|
|
166
|
-
shouldWriteToCache: (ctx) =>
|
|
167
|
-
// Cache only successful responses
|
|
168
|
-
isCacheable(ctx) && isEmpty(ctx?.response?.body?.singleResult.errors),
|
|
169
|
-
}),
|
|
170
123
|
invalidateCachePlugin({ cache: this.redisClient, invalidateCacheKeyGenerator }),
|
|
124
|
+
responseCachePlugin({ logger, cacheKeyGenerator }),
|
|
171
125
|
],
|
|
172
126
|
};
|
|
173
127
|
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":"wsBAsBA,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,oBAAoB,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC;KAC3C;AACD,IAAA,OAAO,WAAW,CAAC;AACvB,CAAC,CAAC;MAIW,aAAa,CAAA;AAMV,IAAA,GAAA,CAAA;AACA,IAAA,UAAA,CAAA;AACA,IAAA,WAAA,CAAA;AACA,IAAA,aAAA,CAAA;AACA,IAAA,kBAAA,CAAA;AACS,IAAA,iBAAA,CAAA;AACA,IAAA,2BAAA,CAAA;AAXb,IAAA,MAAM,CAAU;AAEhB,IAAA,eAAe,CAAmB;AAE1C,IAAA,WAAA,CACY,GAAY,EACZ,UAAuB,EACvB,WAA4C,EAC5C,aAA6B,EAC7B,kBAAqB,GAAA,IAAI,EAChB,iBAAgC,EAChC,2BAAsD,EAAA;QAN/D,IAA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AAChC,oBAAA,OAAO,QAAQ,CAAC;iBACnB;AACJ,aAAA,CAAC,CAAC;SACN;AAED,QAAA,OAAO,IAAI,YAAY,CAAC,YAAY,CAAC,CAAC;KACzC;AACJ"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
'use strict';var graphql=require('graphql');const addShareableDirectiveToSchema = (schemaString) => {
|
|
2
|
+
const ast = graphql.parse(schemaString);
|
|
3
|
+
const modifiedAst = graphql.visit(ast, {
|
|
4
|
+
ObjectTypeDefinition(node) {
|
|
5
|
+
// Add @shareable directive to each type
|
|
6
|
+
const shareableDirective = {
|
|
7
|
+
kind: graphql.Kind.DIRECTIVE,
|
|
8
|
+
name: { kind: graphql.Kind.NAME, value: 'shareable' },
|
|
9
|
+
};
|
|
10
|
+
return {
|
|
11
|
+
...node,
|
|
12
|
+
directives: [...(node.directives || []), shareableDirective],
|
|
13
|
+
};
|
|
14
|
+
},
|
|
15
|
+
ObjectTypeExtension(node) {
|
|
16
|
+
// Add @shareable directive to each extended type
|
|
17
|
+
const shareableDirective = {
|
|
18
|
+
kind: graphql.Kind.DIRECTIVE,
|
|
19
|
+
name: { kind: graphql.Kind.NAME, value: 'shareable' },
|
|
20
|
+
};
|
|
21
|
+
return {
|
|
22
|
+
...node,
|
|
23
|
+
directives: [...(node.directives || []), shareableDirective],
|
|
24
|
+
};
|
|
25
|
+
},
|
|
26
|
+
FieldDefinition(node, _key, _parent, _path, ancestors) {
|
|
27
|
+
// Get the parent type definition from ancestors
|
|
28
|
+
const parent = ancestors[2];
|
|
29
|
+
// @ts-expect-error - parent is an object, but we need to check if it has a kind property
|
|
30
|
+
if (parent?.kind === graphql.Kind.OBJECT_TYPE_DEFINITION && parent.name.value === 'Query') {
|
|
31
|
+
const shareableDirective = {
|
|
32
|
+
kind: graphql.Kind.DIRECTIVE,
|
|
33
|
+
name: { kind: graphql.Kind.NAME, value: 'shareable' },
|
|
34
|
+
};
|
|
35
|
+
return {
|
|
36
|
+
...node,
|
|
37
|
+
directives: [...(node.directives || []), shareableDirective],
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
return node;
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
return graphql.print(modifiedAst);
|
|
44
|
+
};exports.addShareableDirectiveToSchema=addShareableDirectiveToSchema;//# sourceMappingURL=add-shareable-directive-to-schema.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"add-shareable-directive-to-schema.cjs","sources":["../../src/utils/add-shareable-directive-to-schema.ts"],"sourcesContent":[null],"names":["parse","visit","Kind","print"],"mappings":"4CAEa,MAAA,6BAA6B,GAAG,CAAC,YAAoB,KAAI;AAClE,IAAA,MAAM,GAAG,GAAGA,aAAK,CAAC,YAAY,CAAC,CAAC;AAEhC,IAAA,MAAM,WAAW,GAAGC,aAAK,CAAC,GAAG,EAAE;AAC3B,QAAA,oBAAoB,CAAC,IAAI,EAAA;;AAErB,YAAA,MAAM,kBAAkB,GAAG;gBACvB,IAAI,EAAEC,YAAI,CAAC,SAAS;gBACpB,IAAI,EAAE,EAAE,IAAI,EAAEA,YAAI,CAAC,IAAI,EAAE,KAAK,EAAE,WAAW,EAAE;aAChD,CAAC;YAEF,OAAO;AACH,gBAAA,GAAG,IAAI;AACP,gBAAA,UAAU,EAAE,CAAC,IAAI,IAAI,CAAC,UAAU,IAAI,EAAE,CAAC,EAAE,kBAAkB,CAAC;aAC/D,CAAC;SACL;AACD,QAAA,mBAAmB,CAAC,IAAI,EAAA;;AAEpB,YAAA,MAAM,kBAAkB,GAAG;gBACvB,IAAI,EAAEA,YAAI,CAAC,SAAS;gBACpB,IAAI,EAAE,EAAE,IAAI,EAAEA,YAAI,CAAC,IAAI,EAAE,KAAK,EAAE,WAAW,EAAE;aAChD,CAAC;YAEF,OAAO;AACH,gBAAA,GAAG,IAAI;AACP,gBAAA,UAAU,EAAE,CAAC,IAAI,IAAI,CAAC,UAAU,IAAI,EAAE,CAAC,EAAE,kBAAkB,CAAC;aAC/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