@envelop/validation-cache 4.4.0-alpha-db79afa.0 → 4.4.0-alpha-05dbec7.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/cjs/index.js +32 -0
- package/cjs/package.json +1 -0
- package/esm/index.js +27 -0
- package/package.json +36 -12
- package/{index.d.ts → typings/index.d.ts} +5 -3
- package/README.md +0 -33
- package/index.js +0 -34
- package/index.mjs +0 -30
package/cjs/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
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");
|
|
6
|
+
const tiny_lru_1 = tslib_1.__importDefault(require("tiny-lru"));
|
|
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' ? pluginOptions.cache : (0, tiny_lru_1.default)(DEFAULT_MAX, DEFAULT_TTL);
|
|
12
|
+
return {
|
|
13
|
+
onSchemaChange() {
|
|
14
|
+
resultCache.clear();
|
|
15
|
+
},
|
|
16
|
+
onParse({ params, extendContext }) {
|
|
17
|
+
extendContext({ [rawDocumentSymbol]: params.source.toString() });
|
|
18
|
+
},
|
|
19
|
+
onValidate({ params, context, setResult }) {
|
|
20
|
+
var _a;
|
|
21
|
+
const key = (_a = context[rawDocumentSymbol]) !== null && _a !== void 0 ? _a : (0, graphql_1.print)(params.documentAST);
|
|
22
|
+
const cachedResult = resultCache.get(key);
|
|
23
|
+
if (cachedResult !== undefined) {
|
|
24
|
+
setResult(cachedResult);
|
|
25
|
+
}
|
|
26
|
+
return ({ result }) => {
|
|
27
|
+
resultCache.set(key, result);
|
|
28
|
+
};
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
exports.useValidationCache = useValidationCache;
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { print } from 'graphql';
|
|
2
|
+
import lru from 'tiny-lru';
|
|
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' ? pluginOptions.cache : lru(DEFAULT_MAX, DEFAULT_TTL);
|
|
8
|
+
return {
|
|
9
|
+
onSchemaChange() {
|
|
10
|
+
resultCache.clear();
|
|
11
|
+
},
|
|
12
|
+
onParse({ params, extendContext }) {
|
|
13
|
+
extendContext({ [rawDocumentSymbol]: params.source.toString() });
|
|
14
|
+
},
|
|
15
|
+
onValidate({ params, context, setResult }) {
|
|
16
|
+
var _a;
|
|
17
|
+
const key = (_a = context[rawDocumentSymbol]) !== null && _a !== void 0 ? _a : 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
|
+
};
|
package/package.json
CHANGED
|
@@ -1,33 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@envelop/validation-cache",
|
|
3
|
-
"version": "4.4.0-alpha-
|
|
3
|
+
"version": "4.4.0-alpha-05dbec7.0",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"peerDependencies": {
|
|
6
|
-
"@envelop/core": "2.4.0-alpha-
|
|
6
|
+
"@envelop/core": "2.4.0-alpha-05dbec7.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>",
|
|
15
18
|
"license": "MIT",
|
|
16
|
-
"main": "index.js",
|
|
17
|
-
"module": "index.
|
|
18
|
-
"typings": "index.d.ts",
|
|
19
|
+
"main": "cjs/index.js",
|
|
20
|
+
"module": "esm/index.js",
|
|
21
|
+
"typings": "typings/index.d.ts",
|
|
19
22
|
"typescript": {
|
|
20
|
-
"definition": "index.d.ts"
|
|
23
|
+
"definition": "typings/index.d.ts"
|
|
21
24
|
},
|
|
25
|
+
"type": "module",
|
|
22
26
|
"exports": {
|
|
23
27
|
".": {
|
|
24
|
-
"require":
|
|
25
|
-
|
|
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
|
+
}
|
|
26
40
|
},
|
|
27
41
|
"./*": {
|
|
28
|
-
"require":
|
|
29
|
-
|
|
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
|
+
}
|
|
30
54
|
},
|
|
31
55
|
"./package.json": "./package.json"
|
|
32
56
|
}
|
|
33
|
-
}
|
|
57
|
+
}
|
|
@@ -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/README.md
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
## `@envelop/validation-cache`
|
|
2
|
-
|
|
3
|
-
This plugins adds simple LRU caching to your `validate`, to improve performance by caching the validation result.
|
|
4
|
-
|
|
5
|
-
This plugins improves performance of validating by ~50% (based on benchmarks).
|
|
6
|
-
|
|
7
|
-
## Getting Started
|
|
8
|
-
|
|
9
|
-
```
|
|
10
|
-
yarn add @envelop/validation-cache
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
## Usage Example
|
|
14
|
-
|
|
15
|
-
```ts
|
|
16
|
-
import { envelop } from '@envelop/core';
|
|
17
|
-
import { useValidationCache } from '@envelop/validation-cache';
|
|
18
|
-
|
|
19
|
-
const getEnveloped = envelop({
|
|
20
|
-
plugins: [
|
|
21
|
-
// ... other plugins ...
|
|
22
|
-
useValidationCache({
|
|
23
|
-
// options goes here
|
|
24
|
-
}),
|
|
25
|
-
],
|
|
26
|
-
});
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
### API Reference
|
|
30
|
-
|
|
31
|
-
#### `cache`
|
|
32
|
-
|
|
33
|
-
Set this to pass in a cache instance. By default a new LRU cache is created using default `max` and `ttl`.
|
package/index.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
const core = require('@envelop/core');
|
|
6
|
-
const graphql = require('graphql');
|
|
7
|
-
|
|
8
|
-
const rawDocumentMap = new WeakMap();
|
|
9
|
-
const useValidationCache = (pluginOptions = {}) => {
|
|
10
|
-
const resultCache = pluginOptions.cache != null ? pluginOptions.cache : core.getInMemoryLRUCache();
|
|
11
|
-
return {
|
|
12
|
-
onSchemaChange() {
|
|
13
|
-
resultCache.clear();
|
|
14
|
-
},
|
|
15
|
-
onParse({ params, context }) {
|
|
16
|
-
var _a;
|
|
17
|
-
rawDocumentMap.set(context, core.isSourceObject(params.source) ? params.source.body : (_a = params.source) === null || _a === void 0 ? void 0 : _a.toString());
|
|
18
|
-
},
|
|
19
|
-
onValidate({ params, context, setResult }) {
|
|
20
|
-
var _a;
|
|
21
|
-
const key = (_a = rawDocumentMap.get(context)) !== null && _a !== void 0 ? _a : graphql.print(params.documentAST);
|
|
22
|
-
const cachedResult = resultCache.get(key);
|
|
23
|
-
if (cachedResult !== undefined) {
|
|
24
|
-
setResult(cachedResult);
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
return ({ result }) => {
|
|
28
|
-
resultCache.set(key, result);
|
|
29
|
-
};
|
|
30
|
-
},
|
|
31
|
-
};
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
exports.useValidationCache = useValidationCache;
|
package/index.mjs
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { getInMemoryLRUCache, isSourceObject } from '@envelop/core';
|
|
2
|
-
import { print } from 'graphql';
|
|
3
|
-
|
|
4
|
-
const rawDocumentMap = new WeakMap();
|
|
5
|
-
const useValidationCache = (pluginOptions = {}) => {
|
|
6
|
-
const resultCache = pluginOptions.cache != null ? pluginOptions.cache : getInMemoryLRUCache();
|
|
7
|
-
return {
|
|
8
|
-
onSchemaChange() {
|
|
9
|
-
resultCache.clear();
|
|
10
|
-
},
|
|
11
|
-
onParse({ params, context }) {
|
|
12
|
-
var _a;
|
|
13
|
-
rawDocumentMap.set(context, isSourceObject(params.source) ? params.source.body : (_a = params.source) === null || _a === void 0 ? void 0 : _a.toString());
|
|
14
|
-
},
|
|
15
|
-
onValidate({ params, context, setResult }) {
|
|
16
|
-
var _a;
|
|
17
|
-
const key = (_a = rawDocumentMap.get(context)) !== null && _a !== void 0 ? _a : print(params.documentAST);
|
|
18
|
-
const cachedResult = resultCache.get(key);
|
|
19
|
-
if (cachedResult !== undefined) {
|
|
20
|
-
setResult(cachedResult);
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
return ({ result }) => {
|
|
24
|
-
resultCache.set(key, result);
|
|
25
|
-
};
|
|
26
|
-
},
|
|
27
|
-
};
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
export { useValidationCache };
|