@envelop/validation-cache 5.1.3 → 5.1.4-alpha-20230505095813-2dc6c6f3

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Dotan Simha
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  ## `@envelop/validation-cache`
2
2
 
3
- This plugins adds simple LRU caching to your `validate`, to improve performance by caching the validation result.
3
+ This plugins adds simple LRU caching to your `validate`, to improve performance by caching the
4
+ validation result.
4
5
 
5
6
  This plugins improves performance of validating by ~50% (based on benchmarks).
6
7
 
@@ -13,7 +14,7 @@ yarn add @envelop/validation-cache
13
14
  ## Usage Example
14
15
 
15
16
  ```ts
16
- import { parse, validate, specifiedRules, execute, subscribe } from 'graphql'
17
+ import { execute, parse, specifiedRules, subscribe, validate } from 'graphql'
17
18
  import { envelop, useEngine } from '@envelop/core'
18
19
  import { useValidationCache } from '@envelop/validation-cache'
19
20
 
@@ -32,4 +33,5 @@ const getEnveloped = envelop({
32
33
 
33
34
  #### `cache`
34
35
 
35
- Set this to pass in a cache instance. By default a new LRU cache is created using default `max` and `ttl`.
36
+ Set this to pass in a cache instance. By default a new LRU cache is created using default `max` and
37
+ `ttl`.
package/cjs/index.js CHANGED
@@ -3,11 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useValidationCache = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const graphql_1 = require("graphql");
6
- const lru_cache_1 = tslib_1.__importDefault(require("lru-cache"));
7
6
  const hash_it_1 = tslib_1.__importDefault(require("hash-it"));
7
+ const lru_cache_1 = tslib_1.__importDefault(require("lru-cache"));
8
+ const core_1 = require("@envelop/core");
8
9
  const DEFAULT_MAX = 1000;
9
10
  const DEFAULT_TTL = 3600000;
10
- const rawDocumentSymbol = Symbol('rawDocument');
11
11
  const schemaHashCache = new WeakMap();
12
12
  function getSchemaHash(schema) {
13
13
  let hash = schemaHashCache.get(schema);
@@ -27,10 +27,7 @@ const useValidationCache = (pluginOptions = {}) => {
27
27
  maxAge: DEFAULT_TTL,
28
28
  });
29
29
  return {
30
- onParse({ params, extendContext }) {
31
- extendContext({ [rawDocumentSymbol]: params.source.toString() });
32
- },
33
- onValidate({ params, context, setValidationFn, validateFn }) {
30
+ onValidate({ params, setValidationFn, validateFn }) {
34
31
  // We use setValidateFn over accessing params.rules directly, as other plugins in the chain might add more rules.
35
32
  // This would cause an issue if we are constructing the cache key here already.
36
33
  setValidationFn((...args) => {
@@ -42,7 +39,7 @@ const useValidationCache = (pluginOptions = {}) => {
42
39
  ruleKey = ruleKey + rule.name;
43
40
  }
44
41
  }
45
- const key = schemaHashKey + `|` + ruleKey + `|` + (context[rawDocumentSymbol] ?? (0, graphql_1.print)(params.documentAST));
42
+ const key = schemaHashKey + `|` + ruleKey + `|` + (0, core_1.getDocumentString)(params.documentAST, graphql_1.print);
46
43
  const cachedResult = resultCache.get(key);
47
44
  if (cachedResult !== undefined) {
48
45
  return cachedResult;
package/esm/index.js CHANGED
@@ -1,9 +1,9 @@
1
- import { print, introspectionFromSchema } from 'graphql';
2
- import LRU from 'lru-cache';
1
+ import { introspectionFromSchema, print } from 'graphql';
3
2
  import hashIt from 'hash-it';
3
+ import LRU from 'lru-cache';
4
+ import { getDocumentString } from '@envelop/core';
4
5
  const DEFAULT_MAX = 1000;
5
6
  const DEFAULT_TTL = 3600000;
6
- const rawDocumentSymbol = Symbol('rawDocument');
7
7
  const schemaHashCache = new WeakMap();
8
8
  function getSchemaHash(schema) {
9
9
  let hash = schemaHashCache.get(schema);
@@ -23,10 +23,7 @@ export const useValidationCache = (pluginOptions = {}) => {
23
23
  maxAge: DEFAULT_TTL,
24
24
  });
25
25
  return {
26
- onParse({ params, extendContext }) {
27
- extendContext({ [rawDocumentSymbol]: params.source.toString() });
28
- },
29
- onValidate({ params, context, setValidationFn, validateFn }) {
26
+ onValidate({ params, setValidationFn, validateFn }) {
30
27
  // We use setValidateFn over accessing params.rules directly, as other plugins in the chain might add more rules.
31
28
  // This would cause an issue if we are constructing the cache key here already.
32
29
  setValidationFn((...args) => {
@@ -38,7 +35,7 @@ export const useValidationCache = (pluginOptions = {}) => {
38
35
  ruleKey = ruleKey + rule.name;
39
36
  }
40
37
  }
41
- const key = schemaHashKey + `|` + ruleKey + `|` + (context[rawDocumentSymbol] ?? print(params.documentAST));
38
+ const key = schemaHashKey + `|` + ruleKey + `|` + getDocumentString(params.documentAST, print);
42
39
  const cachedResult = resultCache.get(key);
43
40
  if (cachedResult !== undefined) {
44
41
  return cachedResult;
package/package.json CHANGED
@@ -1,15 +1,15 @@
1
1
  {
2
2
  "name": "@envelop/validation-cache",
3
- "version": "5.1.3",
3
+ "version": "5.1.4-alpha-20230505095813-2dc6c6f3",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
- "@envelop/core": "^3.0.6",
6
+ "@envelop/core": "3.0.7-alpha-20230505095813-2dc6c6f3",
7
7
  "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
8
8
  },
9
9
  "dependencies": {
10
+ "hash-it": "^6.0.0",
10
11
  "lru-cache": "^6.0.0",
11
- "tslib": "^2.5.0",
12
- "hash-it": "^6.0.0"
12
+ "tslib": "^2.5.0"
13
13
  },
14
14
  "repository": {
15
15
  "type": "git",
@@ -1,5 +1,5 @@
1
- import { Plugin } from '@envelop/core';
2
1
  import { GraphQLError } from 'graphql';
2
+ import { type Plugin } from '@envelop/core';
3
3
  export interface ValidationCache {
4
4
  /**
5
5
  * Get a result from the validation cache.
@@ -1,5 +1,5 @@
1
- import { Plugin } from '@envelop/core';
2
1
  import { GraphQLError } from 'graphql';
2
+ import { type Plugin } from '@envelop/core';
3
3
  export interface ValidationCache {
4
4
  /**
5
5
  * Get a result from the validation cache.