@graphql-yoga/plugin-response-cache 3.15.0 → 3.15.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/index.js +45 -42
- package/esm/index.js +45 -42
- package/package.json +3 -2
package/cjs/index.js
CHANGED
|
@@ -5,6 +5,7 @@ exports.useResponseCache = useResponseCache;
|
|
|
5
5
|
const graphql_1 = require("graphql");
|
|
6
6
|
const core_1 = require("@envelop/core");
|
|
7
7
|
const response_cache_1 = require("@envelop/response-cache");
|
|
8
|
+
const promise_helpers_1 = require("@whatwg-node/promise-helpers");
|
|
8
9
|
var response_cache_2 = require("@envelop/response-cache");
|
|
9
10
|
Object.defineProperty(exports, "cacheControlDirective", { enumerable: true, get: function () { return response_cache_2.cacheControlDirective; } });
|
|
10
11
|
Object.defineProperty(exports, "hashSHA256", { enumerable: true, get: function () { return response_cache_2.hashSHA256; } });
|
|
@@ -13,7 +14,7 @@ const sessionByRequest = new WeakMap();
|
|
|
13
14
|
function sessionFactoryForEnvelop({ request }) {
|
|
14
15
|
return sessionByRequest.get(request);
|
|
15
16
|
}
|
|
16
|
-
const cacheKeyFactoryForEnvelop =
|
|
17
|
+
const cacheKeyFactoryForEnvelop = function cacheKeyFactoryForEnvelop({ context }) {
|
|
17
18
|
const operationId = operationIdByContext.get(context);
|
|
18
19
|
if (operationId == null) {
|
|
19
20
|
throw new Error('[useResponseCache] This plugin is not configured correctly. Make sure you use this plugin with GraphQL Yoga');
|
|
@@ -63,61 +64,63 @@ function useResponseCache(options) {
|
|
|
63
64
|
},
|
|
64
65
|
}));
|
|
65
66
|
},
|
|
66
|
-
|
|
67
|
+
onRequest({ request, serverContext, fetchAPI, endResponse }) {
|
|
67
68
|
if (enabled(request, serverContext)) {
|
|
68
69
|
const operationId = request.headers.get('If-None-Match');
|
|
69
70
|
if (operationId) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
71
|
+
return (0, promise_helpers_1.handleMaybePromise)(() => cache.get(operationId), cachedResponse => {
|
|
72
|
+
if (cachedResponse) {
|
|
73
|
+
const lastModifiedFromClient = request.headers.get('If-Modified-Since');
|
|
74
|
+
const lastModifiedFromCache = cachedResponse.extensions?.http?.headers?.['Last-Modified'];
|
|
75
|
+
if (
|
|
76
|
+
// This should be in the extensions already but we check it here to make sure
|
|
77
|
+
lastModifiedFromCache != null &&
|
|
78
|
+
// If the client doesn't send If-Modified-Since header, we assume the cache is valid
|
|
79
|
+
(lastModifiedFromClient == null ||
|
|
80
|
+
new Date(lastModifiedFromClient).getTime() >=
|
|
81
|
+
new Date(lastModifiedFromCache).getTime())) {
|
|
82
|
+
const okResponse = new fetchAPI.Response(null, {
|
|
83
|
+
status: 304,
|
|
84
|
+
headers: {
|
|
85
|
+
ETag: operationId,
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
endResponse(okResponse);
|
|
89
|
+
}
|
|
88
90
|
}
|
|
89
|
-
}
|
|
91
|
+
});
|
|
90
92
|
}
|
|
91
93
|
}
|
|
92
94
|
},
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
const operationId = await buildResponseCacheKey({
|
|
95
|
+
onParams({ params, request, context, setResult }) {
|
|
96
|
+
return (0, promise_helpers_1.handleMaybePromise)(() => options.session(request, context), sessionId => (0, promise_helpers_1.handleMaybePromise)(() => buildResponseCacheKey({
|
|
96
97
|
documentString: params.query || '',
|
|
97
98
|
variableValues: params.variables,
|
|
98
99
|
operationName: params.operationName,
|
|
99
100
|
sessionId,
|
|
100
101
|
request,
|
|
101
102
|
context,
|
|
102
|
-
})
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
103
|
+
}), operationId => {
|
|
104
|
+
operationIdByContext.set(context, operationId);
|
|
105
|
+
sessionByRequest.set(request, sessionId);
|
|
106
|
+
if (enabled(request, context)) {
|
|
107
|
+
return (0, promise_helpers_1.handleMaybePromise)(() => cache.get(operationId), cachedResponse => {
|
|
108
|
+
if (cachedResponse) {
|
|
109
|
+
const responseWithSymbol = {
|
|
110
|
+
...cachedResponse,
|
|
111
|
+
[Symbol.for('servedFromResponseCache')]: true,
|
|
112
|
+
};
|
|
113
|
+
if (options.includeExtensionMetadata) {
|
|
114
|
+
setResult((0, response_cache_1.resultWithMetadata)(responseWithSymbol, { hit: true }));
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
setResult(responseWithSymbol);
|
|
118
|
+
}
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
});
|
|
119
122
|
}
|
|
120
|
-
}
|
|
123
|
+
}));
|
|
121
124
|
},
|
|
122
125
|
};
|
|
123
126
|
}
|
package/esm/index.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { print } from 'graphql';
|
|
2
2
|
import { getDocumentString } from '@envelop/core';
|
|
3
3
|
import { defaultBuildResponseCacheKey, createInMemoryCache as envelopCreateInMemoryCache, resultWithMetadata, useResponseCache as useEnvelopResponseCache, } from '@envelop/response-cache';
|
|
4
|
+
import { handleMaybePromise } from '@whatwg-node/promise-helpers';
|
|
4
5
|
export { cacheControlDirective, hashSHA256 } from '@envelop/response-cache';
|
|
5
6
|
const operationIdByContext = new WeakMap();
|
|
6
7
|
const sessionByRequest = new WeakMap();
|
|
7
8
|
function sessionFactoryForEnvelop({ request }) {
|
|
8
9
|
return sessionByRequest.get(request);
|
|
9
10
|
}
|
|
10
|
-
const cacheKeyFactoryForEnvelop =
|
|
11
|
+
const cacheKeyFactoryForEnvelop = function cacheKeyFactoryForEnvelop({ context }) {
|
|
11
12
|
const operationId = operationIdByContext.get(context);
|
|
12
13
|
if (operationId == null) {
|
|
13
14
|
throw new Error('[useResponseCache] This plugin is not configured correctly. Make sure you use this plugin with GraphQL Yoga');
|
|
@@ -57,61 +58,63 @@ export function useResponseCache(options) {
|
|
|
57
58
|
},
|
|
58
59
|
}));
|
|
59
60
|
},
|
|
60
|
-
|
|
61
|
+
onRequest({ request, serverContext, fetchAPI, endResponse }) {
|
|
61
62
|
if (enabled(request, serverContext)) {
|
|
62
63
|
const operationId = request.headers.get('If-None-Match');
|
|
63
64
|
if (operationId) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
65
|
+
return handleMaybePromise(() => cache.get(operationId), cachedResponse => {
|
|
66
|
+
if (cachedResponse) {
|
|
67
|
+
const lastModifiedFromClient = request.headers.get('If-Modified-Since');
|
|
68
|
+
const lastModifiedFromCache = cachedResponse.extensions?.http?.headers?.['Last-Modified'];
|
|
69
|
+
if (
|
|
70
|
+
// This should be in the extensions already but we check it here to make sure
|
|
71
|
+
lastModifiedFromCache != null &&
|
|
72
|
+
// If the client doesn't send If-Modified-Since header, we assume the cache is valid
|
|
73
|
+
(lastModifiedFromClient == null ||
|
|
74
|
+
new Date(lastModifiedFromClient).getTime() >=
|
|
75
|
+
new Date(lastModifiedFromCache).getTime())) {
|
|
76
|
+
const okResponse = new fetchAPI.Response(null, {
|
|
77
|
+
status: 304,
|
|
78
|
+
headers: {
|
|
79
|
+
ETag: operationId,
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
endResponse(okResponse);
|
|
83
|
+
}
|
|
82
84
|
}
|
|
83
|
-
}
|
|
85
|
+
});
|
|
84
86
|
}
|
|
85
87
|
}
|
|
86
88
|
},
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
const operationId = await buildResponseCacheKey({
|
|
89
|
+
onParams({ params, request, context, setResult }) {
|
|
90
|
+
return handleMaybePromise(() => options.session(request, context), sessionId => handleMaybePromise(() => buildResponseCacheKey({
|
|
90
91
|
documentString: params.query || '',
|
|
91
92
|
variableValues: params.variables,
|
|
92
93
|
operationName: params.operationName,
|
|
93
94
|
sessionId,
|
|
94
95
|
request,
|
|
95
96
|
context,
|
|
96
|
-
})
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
97
|
+
}), operationId => {
|
|
98
|
+
operationIdByContext.set(context, operationId);
|
|
99
|
+
sessionByRequest.set(request, sessionId);
|
|
100
|
+
if (enabled(request, context)) {
|
|
101
|
+
return handleMaybePromise(() => cache.get(operationId), cachedResponse => {
|
|
102
|
+
if (cachedResponse) {
|
|
103
|
+
const responseWithSymbol = {
|
|
104
|
+
...cachedResponse,
|
|
105
|
+
[Symbol.for('servedFromResponseCache')]: true,
|
|
106
|
+
};
|
|
107
|
+
if (options.includeExtensionMetadata) {
|
|
108
|
+
setResult(resultWithMetadata(responseWithSymbol, { hit: true }));
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
setResult(responseWithSymbol);
|
|
112
|
+
}
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
});
|
|
113
116
|
}
|
|
114
|
-
}
|
|
117
|
+
}));
|
|
115
118
|
},
|
|
116
119
|
};
|
|
117
120
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-yoga/plugin-response-cache",
|
|
3
|
-
"version": "3.15.
|
|
3
|
+
"version": "3.15.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"peerDependencies": {
|
|
6
6
|
"graphql": "^15.2.0 || ^16.0.0",
|
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
10
|
"@envelop/core": "^5.2.1",
|
|
11
|
-
"@envelop/response-cache": "^7.1.1"
|
|
11
|
+
"@envelop/response-cache": "^7.1.1",
|
|
12
|
+
"@whatwg-node/promise-helpers": "^1.2.4"
|
|
12
13
|
},
|
|
13
14
|
"repository": {
|
|
14
15
|
"type": "git",
|