@envelop/parser-cache 4.4.0-alpha-aca44e1.0 → 5.0.0-alpha-d0d0776.0
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/index.d.ts +8 -3
- package/index.js +14 -7
- package/index.mjs +10 -7
- package/package.json +22 -7
package/index.d.ts
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
|
-
import { Plugin
|
|
1
|
+
import { Plugin } from '@envelop/core';
|
|
2
2
|
import { DocumentNode } from 'graphql';
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
interface Cache<T> {
|
|
4
|
+
get(key: string): T | undefined;
|
|
5
|
+
set(key: string, value: T): void;
|
|
6
|
+
}
|
|
7
|
+
export declare type DocumentCache = Cache<DocumentNode>;
|
|
8
|
+
export declare type ErrorCache = Cache<Error>;
|
|
5
9
|
export declare type ParserCacheOptions = {
|
|
6
10
|
documentCache?: DocumentCache;
|
|
7
11
|
errorCache?: ErrorCache;
|
|
8
12
|
};
|
|
9
13
|
export declare const useParserCache: (pluginOptions?: ParserCacheOptions) => Plugin;
|
|
14
|
+
export {};
|
package/index.js
CHANGED
|
@@ -2,16 +2,24 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
const
|
|
5
|
+
const graphql = require('graphql');
|
|
6
|
+
const lru = require('tiny-lru');
|
|
6
7
|
|
|
8
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
9
|
+
|
|
10
|
+
const lru__default = /*#__PURE__*/_interopDefaultLegacy(lru);
|
|
11
|
+
|
|
12
|
+
const DEFAULT_MAX = 1000;
|
|
13
|
+
const DEFAULT_TTL = 3600000;
|
|
7
14
|
const useParserCache = (pluginOptions = {}) => {
|
|
8
|
-
const documentCache =
|
|
9
|
-
|
|
15
|
+
const documentCache = typeof pluginOptions.documentCache !== 'undefined'
|
|
16
|
+
? pluginOptions.documentCache
|
|
17
|
+
: lru__default["default"](DEFAULT_MAX, DEFAULT_TTL);
|
|
18
|
+
const errorCache = typeof pluginOptions.errorCache !== 'undefined' ? pluginOptions.errorCache : lru__default["default"](DEFAULT_MAX, DEFAULT_TTL);
|
|
10
19
|
return {
|
|
11
20
|
onParse({ params, setParsedDocument }) {
|
|
12
|
-
var _a;
|
|
13
21
|
const { source } = params;
|
|
14
|
-
const key =
|
|
22
|
+
const key = source instanceof graphql.Source ? source.body : source;
|
|
15
23
|
const cachedError = errorCache.get(key);
|
|
16
24
|
if (cachedError !== undefined) {
|
|
17
25
|
throw cachedError;
|
|
@@ -19,13 +27,12 @@ const useParserCache = (pluginOptions = {}) => {
|
|
|
19
27
|
const cachedDocument = documentCache.get(key);
|
|
20
28
|
if (cachedDocument !== undefined) {
|
|
21
29
|
setParsedDocument(cachedDocument);
|
|
22
|
-
return;
|
|
23
30
|
}
|
|
24
31
|
return ({ result }) => {
|
|
25
32
|
if (result instanceof Error) {
|
|
26
33
|
errorCache.set(key, result);
|
|
27
34
|
}
|
|
28
|
-
else if (result
|
|
35
|
+
else if (result !== null) {
|
|
29
36
|
documentCache.set(key, result);
|
|
30
37
|
}
|
|
31
38
|
};
|
package/index.mjs
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Source } from 'graphql';
|
|
2
|
+
import lru from 'tiny-lru';
|
|
2
3
|
|
|
4
|
+
const DEFAULT_MAX = 1000;
|
|
5
|
+
const DEFAULT_TTL = 3600000;
|
|
3
6
|
const useParserCache = (pluginOptions = {}) => {
|
|
4
|
-
const documentCache =
|
|
5
|
-
|
|
7
|
+
const documentCache = typeof pluginOptions.documentCache !== 'undefined'
|
|
8
|
+
? pluginOptions.documentCache
|
|
9
|
+
: lru(DEFAULT_MAX, DEFAULT_TTL);
|
|
10
|
+
const errorCache = typeof pluginOptions.errorCache !== 'undefined' ? pluginOptions.errorCache : lru(DEFAULT_MAX, DEFAULT_TTL);
|
|
6
11
|
return {
|
|
7
12
|
onParse({ params, setParsedDocument }) {
|
|
8
|
-
var _a;
|
|
9
13
|
const { source } = params;
|
|
10
|
-
const key =
|
|
14
|
+
const key = source instanceof Source ? source.body : source;
|
|
11
15
|
const cachedError = errorCache.get(key);
|
|
12
16
|
if (cachedError !== undefined) {
|
|
13
17
|
throw cachedError;
|
|
@@ -15,13 +19,12 @@ const useParserCache = (pluginOptions = {}) => {
|
|
|
15
19
|
const cachedDocument = documentCache.get(key);
|
|
16
20
|
if (cachedDocument !== undefined) {
|
|
17
21
|
setParsedDocument(cachedDocument);
|
|
18
|
-
return;
|
|
19
22
|
}
|
|
20
23
|
return ({ result }) => {
|
|
21
24
|
if (result instanceof Error) {
|
|
22
25
|
errorCache.set(key, result);
|
|
23
26
|
}
|
|
24
|
-
else if (result
|
|
27
|
+
else if (result !== null) {
|
|
25
28
|
documentCache.set(key, result);
|
|
26
29
|
}
|
|
27
30
|
};
|
package/package.json
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@envelop/parser-cache",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0-alpha-d0d0776.0",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"peerDependencies": {
|
|
6
|
-
"@envelop/core": "
|
|
6
|
+
"@envelop/core": "3.0.0-alpha-d0d0776.0",
|
|
7
7
|
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
|
|
8
8
|
},
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"tiny-lru": "7.0.6"
|
|
11
|
+
},
|
|
9
12
|
"repository": {
|
|
10
13
|
"type": "git",
|
|
11
|
-
"url": "https://github.com/
|
|
14
|
+
"url": "https://github.com/n1ru4l/envelop.git",
|
|
12
15
|
"directory": "packages/plugins/parser-cache"
|
|
13
16
|
},
|
|
14
17
|
"author": "Dotan Simha <dotansimha@gmail.com>",
|
|
@@ -21,12 +24,24 @@
|
|
|
21
24
|
},
|
|
22
25
|
"exports": {
|
|
23
26
|
".": {
|
|
24
|
-
"require":
|
|
25
|
-
|
|
27
|
+
"require": {
|
|
28
|
+
"default": "./index.js",
|
|
29
|
+
"types": "./index.d.ts"
|
|
30
|
+
},
|
|
31
|
+
"import": {
|
|
32
|
+
"default": "./index.mjs",
|
|
33
|
+
"types": "./index.d.ts"
|
|
34
|
+
}
|
|
26
35
|
},
|
|
27
36
|
"./*": {
|
|
28
|
-
"require":
|
|
29
|
-
|
|
37
|
+
"require": {
|
|
38
|
+
"default": "./*.js",
|
|
39
|
+
"types": "./*.d.ts"
|
|
40
|
+
},
|
|
41
|
+
"import": {
|
|
42
|
+
"default": "./*.mjs",
|
|
43
|
+
"types": "./*.d.ts"
|
|
44
|
+
}
|
|
30
45
|
},
|
|
31
46
|
"./package.json": "./package.json"
|
|
32
47
|
}
|