@envelop/validation-cache 5.0.0-alpha-d0d0776.0 → 5.0.0-alpha-ccec0fc.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/cjs/index.js +43 -0
- package/cjs/package.json +1 -0
- package/esm/index.js +38 -0
- package/package.json +26 -17
- package/{index.d.ts → typings/index.d.ts} +15 -2
- package/index.js +0 -37
- package/index.mjs +0 -29
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
|
package/cjs/index.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
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"));
|
|
7
|
+
const DEFAULT_MAX = 1000;
|
|
8
|
+
const DEFAULT_TTL = 3600000;
|
|
9
|
+
const rawDocumentSymbol = Symbol('rawDocument');
|
|
10
|
+
const useValidationCache = (pluginOptions = {}) => {
|
|
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
|
+
});
|
|
17
|
+
return {
|
|
18
|
+
onSchemaChange() {
|
|
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
|
+
}
|
|
26
|
+
},
|
|
27
|
+
onParse({ params, extendContext }) {
|
|
28
|
+
extendContext({ [rawDocumentSymbol]: params.source.toString() });
|
|
29
|
+
},
|
|
30
|
+
onValidate({ params, context, setResult }) {
|
|
31
|
+
var _a;
|
|
32
|
+
const key = (_a = context[rawDocumentSymbol]) !== null && _a !== void 0 ? _a : (0, graphql_1.print)(params.documentAST);
|
|
33
|
+
const cachedResult = resultCache.get(key);
|
|
34
|
+
if (cachedResult !== undefined) {
|
|
35
|
+
setResult(cachedResult);
|
|
36
|
+
}
|
|
37
|
+
return ({ result }) => {
|
|
38
|
+
resultCache.set(key, result);
|
|
39
|
+
};
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
exports.useValidationCache = useValidationCache;
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { print } from '@graphql-tools/graphql';
|
|
2
|
+
import LRU from 'lru-cache';
|
|
3
|
+
const DEFAULT_MAX = 1000;
|
|
4
|
+
const DEFAULT_TTL = 3600000;
|
|
5
|
+
const rawDocumentSymbol = Symbol('rawDocument');
|
|
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
|
+
});
|
|
13
|
+
return {
|
|
14
|
+
onSchemaChange() {
|
|
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
|
+
}
|
|
22
|
+
},
|
|
23
|
+
onParse({ params, extendContext }) {
|
|
24
|
+
extendContext({ [rawDocumentSymbol]: params.source.toString() });
|
|
25
|
+
},
|
|
26
|
+
onValidate({ params, context, setResult }) {
|
|
27
|
+
var _a;
|
|
28
|
+
const key = (_a = context[rawDocumentSymbol]) !== null && _a !== void 0 ? _a : print(params.documentAST);
|
|
29
|
+
const cachedResult = resultCache.get(key);
|
|
30
|
+
if (cachedResult !== undefined) {
|
|
31
|
+
setResult(cachedResult);
|
|
32
|
+
}
|
|
33
|
+
return ({ result }) => {
|
|
34
|
+
resultCache.set(key, result);
|
|
35
|
+
};
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
};
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@envelop/validation-cache",
|
|
3
|
-
"version": "5.0.0-alpha-
|
|
3
|
+
"version": "5.0.0-alpha-ccec0fc.0",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"peerDependencies": {
|
|
6
|
-
"@
|
|
7
|
-
"
|
|
6
|
+
"@graphql-tools/graphql": "0.1.0-alpha-e7752ba5.0",
|
|
7
|
+
"@envelop/core": "3.0.0-alpha-ccec0fc.0"
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
|
-
"
|
|
10
|
+
"lru-cache": "^6.0.0"
|
|
11
11
|
},
|
|
12
12
|
"repository": {
|
|
13
13
|
"type": "git",
|
|
@@ -16,33 +16,42 @@
|
|
|
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
28
|
"require": {
|
|
28
|
-
"
|
|
29
|
-
"
|
|
29
|
+
"types": "./typings/index.d.ts",
|
|
30
|
+
"default": "./cjs/index.js"
|
|
30
31
|
},
|
|
31
32
|
"import": {
|
|
32
|
-
"
|
|
33
|
-
"
|
|
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"
|
|
34
39
|
}
|
|
35
40
|
},
|
|
36
41
|
"./*": {
|
|
37
42
|
"require": {
|
|
38
|
-
"
|
|
39
|
-
"
|
|
43
|
+
"types": "./typings/*.d.ts",
|
|
44
|
+
"default": "./cjs/*.js"
|
|
40
45
|
},
|
|
41
46
|
"import": {
|
|
42
|
-
"
|
|
43
|
-
"
|
|
47
|
+
"types": "./typings/*.d.ts",
|
|
48
|
+
"default": "./esm/*.js"
|
|
49
|
+
},
|
|
50
|
+
"default": {
|
|
51
|
+
"types": "./typings/*.d.ts",
|
|
52
|
+
"default": "./esm/*.js"
|
|
44
53
|
}
|
|
45
54
|
},
|
|
46
55
|
"./package.json": "./package.json"
|
|
47
56
|
}
|
|
48
|
-
}
|
|
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;
|
package/index.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
const graphql = require('graphql');
|
|
6
|
-
const lru = require('tiny-lru');
|
|
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;
|
|
14
|
-
const rawDocumentSymbol = Symbol('rawDocument');
|
|
15
|
-
const useValidationCache = (pluginOptions = {}) => {
|
|
16
|
-
const resultCache = typeof pluginOptions.cache !== 'undefined' ? pluginOptions.cache : lru__default["default"](DEFAULT_MAX, DEFAULT_TTL);
|
|
17
|
-
return {
|
|
18
|
-
onSchemaChange() {
|
|
19
|
-
resultCache.clear();
|
|
20
|
-
},
|
|
21
|
-
onParse({ params, extendContext }) {
|
|
22
|
-
extendContext({ [rawDocumentSymbol]: params.source.toString() });
|
|
23
|
-
},
|
|
24
|
-
onValidate({ params, context, setResult }) {
|
|
25
|
-
const key = context[rawDocumentSymbol] ?? graphql.print(params.documentAST);
|
|
26
|
-
const cachedResult = resultCache.get(key);
|
|
27
|
-
if (cachedResult !== undefined) {
|
|
28
|
-
setResult(cachedResult);
|
|
29
|
-
}
|
|
30
|
-
return ({ result }) => {
|
|
31
|
-
resultCache.set(key, result);
|
|
32
|
-
};
|
|
33
|
-
},
|
|
34
|
-
};
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
exports.useValidationCache = useValidationCache;
|
package/index.mjs
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { print } from 'graphql';
|
|
2
|
-
import lru from 'tiny-lru';
|
|
3
|
-
|
|
4
|
-
const DEFAULT_MAX = 1000;
|
|
5
|
-
const DEFAULT_TTL = 3600000;
|
|
6
|
-
const rawDocumentSymbol = Symbol('rawDocument');
|
|
7
|
-
const useValidationCache = (pluginOptions = {}) => {
|
|
8
|
-
const resultCache = typeof pluginOptions.cache !== 'undefined' ? pluginOptions.cache : lru(DEFAULT_MAX, DEFAULT_TTL);
|
|
9
|
-
return {
|
|
10
|
-
onSchemaChange() {
|
|
11
|
-
resultCache.clear();
|
|
12
|
-
},
|
|
13
|
-
onParse({ params, extendContext }) {
|
|
14
|
-
extendContext({ [rawDocumentSymbol]: params.source.toString() });
|
|
15
|
-
},
|
|
16
|
-
onValidate({ params, context, setResult }) {
|
|
17
|
-
const key = context[rawDocumentSymbol] ?? print(params.documentAST);
|
|
18
|
-
const cachedResult = resultCache.get(key);
|
|
19
|
-
if (cachedResult !== undefined) {
|
|
20
|
-
setResult(cachedResult);
|
|
21
|
-
}
|
|
22
|
-
return ({ result }) => {
|
|
23
|
-
resultCache.set(key, result);
|
|
24
|
-
};
|
|
25
|
-
},
|
|
26
|
-
};
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
export { useValidationCache };
|