@orion-js/graphql 2.6.6 → 2.6.13

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.
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = getOperationName;
7
+
8
+ async function getOperationName(params) {
9
+ const {
10
+ request,
11
+ getBodyJSON
12
+ } = params;
13
+
14
+ try {
15
+ if (request.method !== 'GET') {
16
+ const data = await getBodyJSON();
17
+ return data.operationName;
18
+ }
19
+
20
+ if (request.method === 'GET') {
21
+ // handle persisted queries
22
+ const queryParams = new URLSearchParams(request.url.split('?')[1]);
23
+ return queryParams.get('operationName');
24
+ }
25
+ } catch (error) {
26
+ return null;
27
+ }
28
+ }
@@ -7,10 +7,10 @@ Object.defineProperty(exports, "__esModule", {
7
7
  });
8
8
  exports.default = _default;
9
9
 
10
- var _app = require("@orion-js/app");
11
-
12
10
  var _apolloServerMicro = require("apollo-server-micro");
13
11
 
12
+ var _app = require("@orion-js/app");
13
+
14
14
  var _startGraphiQL = _interopRequireDefault(require("./startGraphiQL"));
15
15
 
16
16
  var _getApolloOptions = _interopRequireDefault(require("./getApolloOptions"));
@@ -21,6 +21,12 @@ var _micro = _interopRequireDefault(require("micro"));
21
21
 
22
22
  var _apolloServerCore = require("apollo-server-core");
23
23
 
24
+ var _apolloServerCaching = require("apollo-server-caching");
25
+
26
+ var _getOperationName = _interopRequireDefault(require("./getOperationName"));
27
+
28
+ var _storePersistedQuery = _interopRequireDefault(require("./storePersistedQuery"));
29
+
24
30
  global.globalMicro = _micro.default;
25
31
 
26
32
  async function _default(options) {
@@ -35,6 +41,7 @@ async function _default(options) {
35
41
  await apolloServer.start();
36
42
  const handler = apolloServer.createHandler(); // highlight-line
37
43
 
44
+ const cacheControlCache = apolloOptions.persistedQueries?.cache ? apolloOptions.persistedQueries.cache : new _apolloServerCaching.InMemoryLRUCache();
38
45
  (0, _app.route)('/graphql', async function (params) {
39
46
  const {
40
47
  request,
@@ -42,6 +49,8 @@ async function _default(options) {
42
49
  viewer,
43
50
  getBodyJSON
44
51
  } = params;
52
+ const operationName = await (0, _getOperationName.default)(params);
53
+ const cacheKey = operationName ? `orion-cache-control-${operationName}` : null;
45
54
 
46
55
  if (options.executeGraphQLCache) {
47
56
  try {
@@ -61,6 +70,13 @@ async function _default(options) {
61
70
  const result = await options.executeGraphQLCache(params, fallback);
62
71
 
63
72
  if (result) {
73
+ await (0, _storePersistedQuery.default)(apolloOptions, params);
74
+
75
+ if (result.cacheControl && cacheKey) {
76
+ cacheControlCache.set(cacheKey, result.cacheControl);
77
+ response.setHeader('Cache-Control', result.cacheControl);
78
+ }
79
+
64
80
  return result;
65
81
  }
66
82
  } catch (error) {
@@ -68,6 +84,22 @@ async function _default(options) {
68
84
  }
69
85
  }
70
86
 
87
+ if (cacheKey) {
88
+ let shouldCache = true;
89
+
90
+ if (apolloOptions?.shouldAddCacheControl) {
91
+ shouldCache = apolloOptions.shouldAddCacheControl(operationName);
92
+ }
93
+
94
+ if (shouldCache) {
95
+ const cacheControl = await cacheControlCache.get(cacheKey);
96
+
97
+ if (cacheControl) {
98
+ response.setHeader('Cache-Control', cacheControl);
99
+ }
100
+ }
101
+ }
102
+
71
103
  request._orionjsViewer = viewer;
72
104
  handler(request, response);
73
105
  });
@@ -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.6.6",
3
+ "version": "2.6.13",
4
4
  "main": "lib/index.js",
5
5
  "author": "nicolaslopezj",
6
6
  "license": "MIT",
@@ -11,7 +11,7 @@
11
11
  "test": "jest"
12
12
  },
13
13
  "dependencies": {
14
- "@orion-js/schema": "^2.6.6",
14
+ "@orion-js/schema": "^2.6.7",
15
15
  "apollo-server-core": "3.4.0",
16
16
  "apollo-server-micro": "3.4.0",
17
17
  "deep-sort-object": "^1.0.2",
@@ -26,7 +26,7 @@
26
26
  "@babel/plugin-transform-runtime": "^7.11.0",
27
27
  "@babel/preset-env": "^7.11.0",
28
28
  "@babel/runtime": "^7.11.2",
29
- "@orion-js/app": "^2.6.6",
29
+ "@orion-js/app": "^2.6.13",
30
30
  "babel-jest": "^26.2.2",
31
31
  "graphql": "^15.6.0",
32
32
  "jest": "^26.2.2"
@@ -38,5 +38,5 @@
38
38
  "publishConfig": {
39
39
  "access": "public"
40
40
  },
41
- "gitHead": "543b3ea5d4bf8d4457ae6604dc2db373b9267a58"
41
+ "gitHead": "e2e45fce4bcec54415c75fabd0422e041af79763"
42
42
  }