@orion-js/graphql 2.8.2 → 2.8.3

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.
@@ -25,6 +25,8 @@ var _apolloServerCaching = require("apollo-server-caching");
25
25
 
26
26
  var _getOperationName = _interopRequireDefault(require("./getOperationName"));
27
27
 
28
+ var _storePersistedQuery = _interopRequireDefault(require("./storePersistedQuery"));
29
+
28
30
  global.globalMicro = _micro.default;
29
31
 
30
32
  async function _default(options) {
@@ -68,6 +70,8 @@ async function _default(options) {
68
70
  const result = await options.executeGraphQLCache(params, fallback);
69
71
 
70
72
  if (result) {
73
+ await (0, _storePersistedQuery.default)(apolloOptions, params);
74
+
71
75
  if (result.cacheControl && cacheKey) {
72
76
  cacheControlCache.set(cacheKey, result.cacheControl);
73
77
  response.setHeader('Cache-Control', result.cacheControl);
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = storePersistedQuery;
7
+ exports.APQ_CACHE_PREFIX = void 0;
8
+ const APQ_CACHE_PREFIX = 'apq:';
9
+ exports.APQ_CACHE_PREFIX = APQ_CACHE_PREFIX;
10
+
11
+ function createSHA(algorithm) {
12
+ return require('crypto').createHash(algorithm);
13
+ }
14
+
15
+ function computeQueryHash(query) {
16
+ return createSHA('sha256').update(query).digest('hex');
17
+ }
18
+
19
+ async function storePersistedQuery(apolloConfig, params) {
20
+ const queryCache = apolloConfig.persistedQueries.cache;
21
+
22
+ if (!queryCache) {
23
+ return;
24
+ }
25
+
26
+ if (params.request.method !== 'POST') {
27
+ return;
28
+ }
29
+
30
+ const data = await params.getBodyJSON();
31
+
32
+ if (!data?.query) {
33
+ return;
34
+ }
35
+
36
+ const hash = APQ_CACHE_PREFIX + computeQueryHash(data.query);
37
+ const ttl = apolloConfig.persistedQueries?.ttl !== undefined ? apolloConfig.persistedQueries.ttl : 60 * 15;
38
+ await queryCache.set(hash, data.query, {
39
+ ttl
40
+ });
41
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orion-js/graphql",
3
- "version": "2.8.2",
3
+ "version": "2.8.3",
4
4
  "main": "lib/index.js",
5
5
  "author": "nicolaslopezj",
6
6
  "license": "MIT",