@envelop/response-cache 4.0.4 → 4.1.0-alpha-20221226133727-3afa5d6a
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/cjs/plugin.js +33 -0
- package/esm/plugin.js +34 -1
- package/package.json +1 -1
package/cjs/plugin.js
CHANGED
|
@@ -43,12 +43,45 @@ includeExtensionMetadata = typeof process !== 'undefined' ? process.env['NODE_EN
|
|
|
43
43
|
const ignoredTypesMap = new Set(ignoredTypes);
|
|
44
44
|
// never cache Introspections
|
|
45
45
|
ttlPerSchemaCoordinate = { 'Query.__schema': 0, ...ttlPerSchemaCoordinate };
|
|
46
|
+
const processedSchemas = new WeakSet();
|
|
46
47
|
return {
|
|
47
48
|
onPluginInit({ addPlugin }) {
|
|
48
49
|
if (getDocumentString === cache_document_str_js_1.defaultGetDocumentString) {
|
|
49
50
|
addPlugin((0, cache_document_str_js_1.useCacheDocumentString)());
|
|
50
51
|
}
|
|
51
52
|
},
|
|
53
|
+
onSchemaChange({ schema }) {
|
|
54
|
+
if (processedSchemas.has(schema)) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
// Check if the schema has @cacheControl directive
|
|
58
|
+
const cacheControlDirective = schema.getDirective('cacheControl');
|
|
59
|
+
if (cacheControlDirective) {
|
|
60
|
+
(0, utils_1.mapSchema)(schema, {
|
|
61
|
+
[utils_1.MapperKind.COMPOSITE_TYPE]: type => {
|
|
62
|
+
const cacheControlAnnotations = (0, utils_1.getDirective)(schema, type, 'cacheControl');
|
|
63
|
+
cacheControlAnnotations?.forEach(cacheControl => {
|
|
64
|
+
const ttl = cacheControl.maxAge * 1000;
|
|
65
|
+
if (ttl != null) {
|
|
66
|
+
ttlPerType[type.name] = ttl;
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
return type;
|
|
70
|
+
},
|
|
71
|
+
[utils_1.MapperKind.FIELD]: (fieldConfig, fieldName, typeName) => {
|
|
72
|
+
const cacheControlAnnotations = (0, utils_1.getDirective)(schema, fieldConfig, 'cacheControl');
|
|
73
|
+
cacheControlAnnotations?.forEach(cacheControl => {
|
|
74
|
+
const ttl = cacheControl.maxAge * 1000;
|
|
75
|
+
if (ttl != null) {
|
|
76
|
+
ttlPerSchemaCoordinate[`${typeName}.${fieldName}`] = ttl;
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
return fieldConfig;
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
processedSchemas.add(schema);
|
|
84
|
+
},
|
|
52
85
|
async onExecute(onExecuteParams) {
|
|
53
86
|
let documentChanged = false;
|
|
54
87
|
const newDocument = (0, graphql_1.visit)(onExecuteParams.args.document, {
|
package/esm/plugin.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isAsyncIterable } from '@envelop/core';
|
|
2
|
-
import { visitResult } from '@graphql-tools/utils';
|
|
2
|
+
import { getDirective, MapperKind, mapSchema, visitResult } from '@graphql-tools/utils';
|
|
3
3
|
import { visit, TypeInfo, visitWithTypeInfo, getOperationAST, Kind, } from 'graphql';
|
|
4
4
|
import jsonStableStringify from 'fast-json-stable-stringify';
|
|
5
5
|
import { createInMemoryCache } from './in-memory-cache.js';
|
|
@@ -37,12 +37,45 @@ includeExtensionMetadata = typeof process !== 'undefined' ? process.env['NODE_EN
|
|
|
37
37
|
const ignoredTypesMap = new Set(ignoredTypes);
|
|
38
38
|
// never cache Introspections
|
|
39
39
|
ttlPerSchemaCoordinate = { 'Query.__schema': 0, ...ttlPerSchemaCoordinate };
|
|
40
|
+
const processedSchemas = new WeakSet();
|
|
40
41
|
return {
|
|
41
42
|
onPluginInit({ addPlugin }) {
|
|
42
43
|
if (getDocumentString === defaultGetDocumentString) {
|
|
43
44
|
addPlugin(useCacheDocumentString());
|
|
44
45
|
}
|
|
45
46
|
},
|
|
47
|
+
onSchemaChange({ schema }) {
|
|
48
|
+
if (processedSchemas.has(schema)) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
// Check if the schema has @cacheControl directive
|
|
52
|
+
const cacheControlDirective = schema.getDirective('cacheControl');
|
|
53
|
+
if (cacheControlDirective) {
|
|
54
|
+
mapSchema(schema, {
|
|
55
|
+
[MapperKind.COMPOSITE_TYPE]: type => {
|
|
56
|
+
const cacheControlAnnotations = getDirective(schema, type, 'cacheControl');
|
|
57
|
+
cacheControlAnnotations?.forEach(cacheControl => {
|
|
58
|
+
const ttl = cacheControl.maxAge * 1000;
|
|
59
|
+
if (ttl != null) {
|
|
60
|
+
ttlPerType[type.name] = ttl;
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
return type;
|
|
64
|
+
},
|
|
65
|
+
[MapperKind.FIELD]: (fieldConfig, fieldName, typeName) => {
|
|
66
|
+
const cacheControlAnnotations = getDirective(schema, fieldConfig, 'cacheControl');
|
|
67
|
+
cacheControlAnnotations?.forEach(cacheControl => {
|
|
68
|
+
const ttl = cacheControl.maxAge * 1000;
|
|
69
|
+
if (ttl != null) {
|
|
70
|
+
ttlPerSchemaCoordinate[`${typeName}.${fieldName}`] = ttl;
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
return fieldConfig;
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
processedSchemas.add(schema);
|
|
78
|
+
},
|
|
46
79
|
async onExecute(onExecuteParams) {
|
|
47
80
|
let documentChanged = false;
|
|
48
81
|
const newDocument = visit(onExecuteParams.args.document, {
|