@envelop/validation-cache 1.0.1

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 ADDED
@@ -0,0 +1,37 @@
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
+ #### `max`
32
+
33
+ Set this to configure your maximum amount of items in the LRU cache. The default is `1000`.
34
+
35
+ #### `ttl`
36
+
37
+ Set this to configure the TTL (time to live) of items in the LRU cache. The default is `3600000`.
package/index.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import { Plugin } from '@envelop/types';
2
+ export declare type ValidationCacheOptions = {
3
+ max?: number;
4
+ ttl?: number;
5
+ };
6
+ export declare const useValidationCache: (pluginOptions?: ValidationCacheOptions) => Plugin;
package/index.js ADDED
@@ -0,0 +1,33 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
+
7
+ const graphql = require('graphql');
8
+ const lru = _interopDefault(require('tiny-lru'));
9
+
10
+ const DEFAULT_MAX = 1000;
11
+ const DEFAULT_TTL = 3600000;
12
+ const useValidationCache = (pluginOptions = {}) => {
13
+ const max = typeof pluginOptions.max === 'number' ? pluginOptions.max : DEFAULT_MAX;
14
+ const ttl = typeof pluginOptions.ttl === 'number' ? pluginOptions.ttl : DEFAULT_TTL;
15
+ const resultCache = lru(max, ttl);
16
+ return {
17
+ onSchemaChange() {
18
+ resultCache.clear();
19
+ },
20
+ onValidate({ params, setResult }) {
21
+ const key = graphql.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
+
33
+ exports.useValidationCache = useValidationCache;
package/index.mjs ADDED
@@ -0,0 +1,27 @@
1
+ import { print } from 'graphql';
2
+ import lru from 'tiny-lru';
3
+
4
+ const DEFAULT_MAX = 1000;
5
+ const DEFAULT_TTL = 3600000;
6
+ const useValidationCache = (pluginOptions = {}) => {
7
+ const max = typeof pluginOptions.max === 'number' ? pluginOptions.max : DEFAULT_MAX;
8
+ const ttl = typeof pluginOptions.ttl === 'number' ? pluginOptions.ttl : DEFAULT_TTL;
9
+ const resultCache = lru(max, ttl);
10
+ return {
11
+ onSchemaChange() {
12
+ resultCache.clear();
13
+ },
14
+ onValidate({ params, setResult }) {
15
+ const key = print(params.documentAST);
16
+ const cachedResult = resultCache.get(key);
17
+ if (cachedResult !== undefined) {
18
+ setResult(cachedResult);
19
+ }
20
+ return ({ result }) => {
21
+ resultCache.set(key, result);
22
+ };
23
+ },
24
+ };
25
+ };
26
+
27
+ export { useValidationCache };
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@envelop/validation-cache",
3
+ "version": "1.0.1",
4
+ "sideEffects": false,
5
+ "peerDependencies": {
6
+ "graphql": "^14.0.0 || ^15.0.0"
7
+ },
8
+ "dependencies": {
9
+ "tiny-lru": "7.0.6"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/dotansimha/envelop.git",
14
+ "directory": "packages/plugins/validation-cache"
15
+ },
16
+ "author": "Dotan Simha <dotansimha@gmail.com>",
17
+ "license": "MIT",
18
+ "main": "index.js",
19
+ "module": "index.mjs",
20
+ "typings": "index.d.ts",
21
+ "typescript": {
22
+ "definition": "index.d.ts"
23
+ },
24
+ "exports": {
25
+ ".": {
26
+ "require": "./index.js",
27
+ "import": "./index.mjs"
28
+ },
29
+ "./*": {
30
+ "require": "./*.js",
31
+ "import": "./*.mjs"
32
+ }
33
+ }
34
+ }