@orion-js/graphql 2.8.0 → 2.8.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.
@@ -0,0 +1,24 @@
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
+ if (request.method !== 'GET') {
15
+ const data = await getBodyJSON();
16
+ return data.operationName;
17
+ }
18
+
19
+ if (request.method === 'GET') {
20
+ // handle persisted queries
21
+ const queryParams = new URLSearchParams(request.url.split('?')[1]);
22
+ return queryParams.get('operationName');
23
+ }
24
+ }
@@ -19,6 +19,10 @@ var _startWebsocket = _interopRequireDefault(require("./startWebsocket"));
19
19
 
20
20
  var _apolloServerCore = require("apollo-server-core");
21
21
 
22
+ var _apolloServerCaching = require("apollo-server-caching");
23
+
24
+ var _getOperationName = _interopRequireDefault(require("./getOperationName"));
25
+
22
26
  async function _default(options) {
23
27
  const apolloOptions = await (0, _getApolloOptions.default)(options);
24
28
  (0, _startGraphiQL.default)(apolloOptions, options);
@@ -31,6 +35,7 @@ async function _default(options) {
31
35
  await apolloServer.start();
32
36
  const handler = apolloServer.createHandler(); // highlight-line
33
37
 
38
+ const cacheControlCache = apolloOptions.persistedQueries?.cache ? apolloOptions.persistedQueries.cache : new _apolloServerCaching.InMemoryLRUCache();
34
39
  (0, _app.route)('/graphql', async function (params) {
35
40
  const {
36
41
  request,
@@ -38,6 +43,8 @@ async function _default(options) {
38
43
  viewer,
39
44
  getBodyJSON
40
45
  } = params;
46
+ const operationName = await (0, _getOperationName.default)(params);
47
+ const cacheKey = operationName ? `orion-cache-control-${operationName}` : null;
41
48
 
42
49
  if (options.executeGraphQLCache) {
43
50
  try {
@@ -57,6 +64,11 @@ async function _default(options) {
57
64
  const result = await options.executeGraphQLCache(params, fallback);
58
65
 
59
66
  if (result) {
67
+ if (result.cacheControl && cacheKey) {
68
+ cacheControlCache.set(cacheKey, result.cacheControl);
69
+ response.setHeader('Cache-Control', result.cacheControl);
70
+ }
71
+
60
72
  return result;
61
73
  }
62
74
  } catch (error) {
@@ -64,6 +76,22 @@ async function _default(options) {
64
76
  }
65
77
  }
66
78
 
79
+ if (cacheKey) {
80
+ let shouldCache = true;
81
+
82
+ if (apolloOptions?.shouldAddCacheControl) {
83
+ shouldCache = apolloOptions.shouldAddCacheControl(operationName);
84
+ }
85
+
86
+ if (shouldCache) {
87
+ const cacheControl = await cacheControlCache.get(cacheKey);
88
+
89
+ if (cacheControl) {
90
+ response.setHeader('Cache-Control', cacheControl);
91
+ }
92
+ }
93
+ }
94
+
67
95
  request._orionjsViewer = viewer;
68
96
  handler(request, response);
69
97
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orion-js/graphql",
3
- "version": "2.8.0",
3
+ "version": "2.8.1",
4
4
  "main": "lib/index.js",
5
5
  "author": "nicolaslopezj",
6
6
  "license": "MIT",
@@ -13,7 +13,7 @@
13
13
  "dependencies": {
14
14
  "@orion-js/schema": "^2.6.6",
15
15
  "apollo-server-core": "3.4.0",
16
- "apollo-server-micro": "3.13.0",
16
+ "apollo-server-micro": "3.8.0",
17
17
  "deep-sort-object": "^1.0.2",
18
18
  "graphql-iso-date": "^3.6.1",
19
19
  "graphql-subscriptions": "^1.2.1",