@envelop/validation-cache 5.0.0-alpha-36a1267.0 → 5.0.0-alpha-5b6bfa8.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/README.md +5 -5
- package/{index.js → cjs/index.js} +20 -13
- package/cjs/package.json +1 -0
- package/{index.mjs → esm/index.js} +16 -8
- package/package.json +35 -14
- package/{index.d.ts → typings/index.d.ts} +15 -2
package/README.md
CHANGED
|
@@ -13,17 +13,17 @@ yarn add @envelop/validation-cache
|
|
|
13
13
|
## Usage Example
|
|
14
14
|
|
|
15
15
|
```ts
|
|
16
|
-
import { envelop } from '@envelop/core'
|
|
17
|
-
import { useValidationCache } from '@envelop/validation-cache'
|
|
16
|
+
import { envelop } from '@envelop/core'
|
|
17
|
+
import { useValidationCache } from '@envelop/validation-cache'
|
|
18
18
|
|
|
19
19
|
const getEnveloped = envelop({
|
|
20
20
|
plugins: [
|
|
21
21
|
// ... other plugins ...
|
|
22
22
|
useValidationCache({
|
|
23
23
|
// options goes here
|
|
24
|
-
})
|
|
25
|
-
]
|
|
26
|
-
})
|
|
24
|
+
})
|
|
25
|
+
]
|
|
26
|
+
})
|
|
27
27
|
```
|
|
28
28
|
|
|
29
29
|
### API Reference
|
|
@@ -1,27 +1,35 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const graphql = require('graphql');
|
|
8
|
-
const lru = _interopDefault(require('tiny-lru'));
|
|
9
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useValidationCache = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const graphql_1 = require("@graphql-tools/graphql");
|
|
6
|
+
const lru_cache_1 = tslib_1.__importDefault(require("lru-cache"));
|
|
10
7
|
const DEFAULT_MAX = 1000;
|
|
11
8
|
const DEFAULT_TTL = 3600000;
|
|
12
9
|
const rawDocumentSymbol = Symbol('rawDocument');
|
|
13
10
|
const useValidationCache = (pluginOptions = {}) => {
|
|
14
|
-
const resultCache = typeof pluginOptions.cache !== 'undefined'
|
|
11
|
+
const resultCache = typeof pluginOptions.cache !== 'undefined'
|
|
12
|
+
? pluginOptions.cache
|
|
13
|
+
: new lru_cache_1.default({
|
|
14
|
+
max: DEFAULT_MAX,
|
|
15
|
+
maxAge: DEFAULT_TTL,
|
|
16
|
+
});
|
|
15
17
|
return {
|
|
16
18
|
onSchemaChange() {
|
|
17
|
-
|
|
19
|
+
var _a, _b;
|
|
20
|
+
if (resultCache.reset) {
|
|
21
|
+
(_a = resultCache.reset) === null || _a === void 0 ? void 0 : _a.call(resultCache);
|
|
22
|
+
}
|
|
23
|
+
else if ('clear' in resultCache) {
|
|
24
|
+
(_b = resultCache.clear) === null || _b === void 0 ? void 0 : _b.call(resultCache);
|
|
25
|
+
}
|
|
18
26
|
},
|
|
19
27
|
onParse({ params, extendContext }) {
|
|
20
28
|
extendContext({ [rawDocumentSymbol]: params.source.toString() });
|
|
21
29
|
},
|
|
22
30
|
onValidate({ params, context, setResult }) {
|
|
23
31
|
var _a;
|
|
24
|
-
const key = (_a = context[rawDocumentSymbol]) !== null && _a !== void 0 ? _a :
|
|
32
|
+
const key = (_a = context[rawDocumentSymbol]) !== null && _a !== void 0 ? _a : (0, graphql_1.print)(params.documentAST);
|
|
25
33
|
const cachedResult = resultCache.get(key);
|
|
26
34
|
if (cachedResult !== undefined) {
|
|
27
35
|
setResult(cachedResult);
|
|
@@ -32,5 +40,4 @@ const useValidationCache = (pluginOptions = {}) => {
|
|
|
32
40
|
},
|
|
33
41
|
};
|
|
34
42
|
};
|
|
35
|
-
|
|
36
43
|
exports.useValidationCache = useValidationCache;
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
|
@@ -1,14 +1,24 @@
|
|
|
1
|
-
import { print } from 'graphql';
|
|
2
|
-
import
|
|
3
|
-
|
|
1
|
+
import { print } from '@graphql-tools/graphql';
|
|
2
|
+
import LRU from 'lru-cache';
|
|
4
3
|
const DEFAULT_MAX = 1000;
|
|
5
4
|
const DEFAULT_TTL = 3600000;
|
|
6
5
|
const rawDocumentSymbol = Symbol('rawDocument');
|
|
7
|
-
const useValidationCache = (pluginOptions = {}) => {
|
|
8
|
-
const resultCache = typeof pluginOptions.cache !== 'undefined'
|
|
6
|
+
export const useValidationCache = (pluginOptions = {}) => {
|
|
7
|
+
const resultCache = typeof pluginOptions.cache !== 'undefined'
|
|
8
|
+
? pluginOptions.cache
|
|
9
|
+
: new LRU({
|
|
10
|
+
max: DEFAULT_MAX,
|
|
11
|
+
maxAge: DEFAULT_TTL,
|
|
12
|
+
});
|
|
9
13
|
return {
|
|
10
14
|
onSchemaChange() {
|
|
11
|
-
|
|
15
|
+
var _a, _b;
|
|
16
|
+
if (resultCache.reset) {
|
|
17
|
+
(_a = resultCache.reset) === null || _a === void 0 ? void 0 : _a.call(resultCache);
|
|
18
|
+
}
|
|
19
|
+
else if ('clear' in resultCache) {
|
|
20
|
+
(_b = resultCache.clear) === null || _b === void 0 ? void 0 : _b.call(resultCache);
|
|
21
|
+
}
|
|
12
22
|
},
|
|
13
23
|
onParse({ params, extendContext }) {
|
|
14
24
|
extendContext({ [rawDocumentSymbol]: params.source.toString() });
|
|
@@ -26,5 +36,3 @@ const useValidationCache = (pluginOptions = {}) => {
|
|
|
26
36
|
},
|
|
27
37
|
};
|
|
28
38
|
};
|
|
29
|
-
|
|
30
|
-
export { useValidationCache };
|
package/package.json
CHANGED
|
@@ -1,36 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@envelop/validation-cache",
|
|
3
|
-
"version": "5.0.0-alpha-
|
|
3
|
+
"version": "5.0.0-alpha-5b6bfa8.0",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"peerDependencies": {
|
|
6
|
-
"@
|
|
7
|
-
"
|
|
6
|
+
"@graphql-tools/graphql": "0.1.0-alpha-33fb2950.0",
|
|
7
|
+
"@envelop/core": "3.0.0-alpha-5b6bfa8.0"
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
|
-
"
|
|
10
|
+
"lru-cache": "^6.0.0"
|
|
11
11
|
},
|
|
12
12
|
"repository": {
|
|
13
13
|
"type": "git",
|
|
14
|
-
"url": "https://github.com/
|
|
14
|
+
"url": "https://github.com/n1ru4l/envelop.git",
|
|
15
15
|
"directory": "packages/plugins/validation-cache"
|
|
16
16
|
},
|
|
17
17
|
"author": "Dotan Simha <dotansimha@gmail.com>",
|
|
18
18
|
"license": "MIT",
|
|
19
|
-
"main": "index.js",
|
|
20
|
-
"module": "index.
|
|
21
|
-
"typings": "index.d.ts",
|
|
19
|
+
"main": "cjs/index.js",
|
|
20
|
+
"module": "esm/index.js",
|
|
21
|
+
"typings": "typings/index.d.ts",
|
|
22
22
|
"typescript": {
|
|
23
|
-
"definition": "index.d.ts"
|
|
23
|
+
"definition": "typings/index.d.ts"
|
|
24
24
|
},
|
|
25
|
+
"type": "module",
|
|
25
26
|
"exports": {
|
|
26
27
|
".": {
|
|
27
|
-
"require":
|
|
28
|
-
|
|
28
|
+
"require": {
|
|
29
|
+
"types": "./typings/index.d.ts",
|
|
30
|
+
"default": "./cjs/index.js"
|
|
31
|
+
},
|
|
32
|
+
"import": {
|
|
33
|
+
"types": "./typings/index.d.ts",
|
|
34
|
+
"default": "./esm/index.js"
|
|
35
|
+
},
|
|
36
|
+
"default": {
|
|
37
|
+
"types": "./typings/index.d.ts",
|
|
38
|
+
"default": "./esm/index.js"
|
|
39
|
+
}
|
|
29
40
|
},
|
|
30
41
|
"./*": {
|
|
31
|
-
"require":
|
|
32
|
-
|
|
42
|
+
"require": {
|
|
43
|
+
"types": "./typings/*.d.ts",
|
|
44
|
+
"default": "./cjs/*.js"
|
|
45
|
+
},
|
|
46
|
+
"import": {
|
|
47
|
+
"types": "./typings/*.d.ts",
|
|
48
|
+
"default": "./esm/*.js"
|
|
49
|
+
},
|
|
50
|
+
"default": {
|
|
51
|
+
"types": "./typings/*.d.ts",
|
|
52
|
+
"default": "./esm/*.js"
|
|
53
|
+
}
|
|
33
54
|
},
|
|
34
55
|
"./package.json": "./package.json"
|
|
35
56
|
}
|
|
36
|
-
}
|
|
57
|
+
}
|
|
@@ -1,9 +1,22 @@
|
|
|
1
1
|
import { Plugin } from '@envelop/core';
|
|
2
|
-
import { GraphQLError } from 'graphql';
|
|
2
|
+
import { GraphQLError } from '@graphql-tools/graphql';
|
|
3
3
|
export interface ValidationCache {
|
|
4
|
+
/**
|
|
5
|
+
* Get a result from the validation cache.
|
|
6
|
+
*/
|
|
4
7
|
get(key: string): readonly GraphQLError[] | undefined;
|
|
8
|
+
/**
|
|
9
|
+
* Set a result to the validation cache.
|
|
10
|
+
*/
|
|
5
11
|
set(key: string, value: readonly GraphQLError[]): void;
|
|
6
|
-
|
|
12
|
+
/**
|
|
13
|
+
* @deprecated Provide a `reset` implementation instead.
|
|
14
|
+
*/
|
|
15
|
+
clear?(): void;
|
|
16
|
+
/**
|
|
17
|
+
* Reset the cache by clearing all entries.
|
|
18
|
+
*/
|
|
19
|
+
reset?(): void;
|
|
7
20
|
}
|
|
8
21
|
export declare type ValidationCacheOptions = {
|
|
9
22
|
cache?: ValidationCache;
|