@envelop/validation-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 +5 -3
- package/index.js +12 -9
- package/index.mjs +8 -9
- package/package.json +22 -7
package/index.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Plugin } from '@envelop/core';
|
|
2
2
|
import { GraphQLError } from 'graphql';
|
|
3
|
-
export
|
|
3
|
+
export interface ValidationCache {
|
|
4
|
+
get(key: string): readonly GraphQLError[] | undefined;
|
|
5
|
+
set(key: string, value: readonly GraphQLError[]): void;
|
|
4
6
|
clear(): void;
|
|
5
|
-
}
|
|
7
|
+
}
|
|
6
8
|
export declare type ValidationCacheOptions = {
|
|
7
9
|
cache?: ValidationCache;
|
|
8
10
|
};
|
package/index.js
CHANGED
|
@@ -2,27 +2,30 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
const core = require('@envelop/core');
|
|
6
5
|
const graphql = require('graphql');
|
|
6
|
+
const lru = require('tiny-lru');
|
|
7
7
|
|
|
8
|
-
|
|
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;
|
|
14
|
+
const rawDocumentSymbol = Symbol('rawDocument');
|
|
9
15
|
const useValidationCache = (pluginOptions = {}) => {
|
|
10
|
-
const resultCache = pluginOptions.cache
|
|
16
|
+
const resultCache = typeof pluginOptions.cache !== 'undefined' ? pluginOptions.cache : lru__default["default"](DEFAULT_MAX, DEFAULT_TTL);
|
|
11
17
|
return {
|
|
12
18
|
onSchemaChange() {
|
|
13
19
|
resultCache.clear();
|
|
14
20
|
},
|
|
15
|
-
onParse({ params,
|
|
16
|
-
|
|
17
|
-
rawDocumentMap.set(context, core.isSourceObject(params.source) ? params.source.body : (_a = params.source) === null || _a === void 0 ? void 0 : _a.toString());
|
|
21
|
+
onParse({ params, extendContext }) {
|
|
22
|
+
extendContext({ [rawDocumentSymbol]: params.source.toString() });
|
|
18
23
|
},
|
|
19
24
|
onValidate({ params, context, setResult }) {
|
|
20
|
-
|
|
21
|
-
const key = (_a = rawDocumentMap.get(context)) !== null && _a !== void 0 ? _a : graphql.print(params.documentAST);
|
|
25
|
+
const key = context[rawDocumentSymbol] ?? graphql.print(params.documentAST);
|
|
22
26
|
const cachedResult = resultCache.get(key);
|
|
23
27
|
if (cachedResult !== undefined) {
|
|
24
28
|
setResult(cachedResult);
|
|
25
|
-
return;
|
|
26
29
|
}
|
|
27
30
|
return ({ result }) => {
|
|
28
31
|
resultCache.set(key, result);
|
package/index.mjs
CHANGED
|
@@ -1,24 +1,23 @@
|
|
|
1
|
-
import { getInMemoryLRUCache, isSourceObject } from '@envelop/core';
|
|
2
1
|
import { print } from 'graphql';
|
|
2
|
+
import lru from 'tiny-lru';
|
|
3
3
|
|
|
4
|
-
const
|
|
4
|
+
const DEFAULT_MAX = 1000;
|
|
5
|
+
const DEFAULT_TTL = 3600000;
|
|
6
|
+
const rawDocumentSymbol = Symbol('rawDocument');
|
|
5
7
|
const useValidationCache = (pluginOptions = {}) => {
|
|
6
|
-
const resultCache = pluginOptions.cache
|
|
8
|
+
const resultCache = typeof pluginOptions.cache !== 'undefined' ? pluginOptions.cache : lru(DEFAULT_MAX, DEFAULT_TTL);
|
|
7
9
|
return {
|
|
8
10
|
onSchemaChange() {
|
|
9
11
|
resultCache.clear();
|
|
10
12
|
},
|
|
11
|
-
onParse({ params,
|
|
12
|
-
|
|
13
|
-
rawDocumentMap.set(context, isSourceObject(params.source) ? params.source.body : (_a = params.source) === null || _a === void 0 ? void 0 : _a.toString());
|
|
13
|
+
onParse({ params, extendContext }) {
|
|
14
|
+
extendContext({ [rawDocumentSymbol]: params.source.toString() });
|
|
14
15
|
},
|
|
15
16
|
onValidate({ params, context, setResult }) {
|
|
16
|
-
|
|
17
|
-
const key = (_a = rawDocumentMap.get(context)) !== null && _a !== void 0 ? _a : print(params.documentAST);
|
|
17
|
+
const key = context[rawDocumentSymbol] ?? print(params.documentAST);
|
|
18
18
|
const cachedResult = resultCache.get(key);
|
|
19
19
|
if (cachedResult !== undefined) {
|
|
20
20
|
setResult(cachedResult);
|
|
21
|
-
return;
|
|
22
21
|
}
|
|
23
22
|
return ({ result }) => {
|
|
24
23
|
resultCache.set(key, result);
|
package/package.json
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@envelop/validation-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/validation-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
|
}
|