@envelop/validation-cache 5.0.5 → 5.1.0-alpha-20221222121455-d169c23d

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 CHANGED
@@ -3,10 +3,23 @@ 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 fast_json_stable_stringify_1 = tslib_1.__importDefault(require("fast-json-stable-stringify"));
6
7
  const lru_cache_1 = tslib_1.__importDefault(require("lru-cache"));
8
+ const js_sha1_1 = tslib_1.__importDefault(require("js-sha1"));
7
9
  const DEFAULT_MAX = 1000;
8
10
  const DEFAULT_TTL = 3600000;
9
11
  const rawDocumentSymbol = Symbol('rawDocument');
12
+ const schemaHashCache = new WeakMap();
13
+ function getSchemaHash(schema) {
14
+ let hash = schemaHashCache.get(schema);
15
+ if (hash) {
16
+ return hash;
17
+ }
18
+ const introspection = (0, graphql_1.introspectionFromSchema)(schema);
19
+ hash = (0, js_sha1_1.default)((0, fast_json_stable_stringify_1.default)(introspection.__schema));
20
+ schemaHashCache.set(schema, hash);
21
+ return hash;
22
+ }
10
23
  const useValidationCache = (pluginOptions = {}) => {
11
24
  const resultCache = typeof pluginOptions.cache !== 'undefined'
12
25
  ? pluginOptions.cache
@@ -15,14 +28,6 @@ const useValidationCache = (pluginOptions = {}) => {
15
28
  maxAge: DEFAULT_TTL,
16
29
  });
17
30
  return {
18
- onSchemaChange() {
19
- if (resultCache.reset) {
20
- resultCache.reset?.();
21
- }
22
- else if ('clear' in resultCache) {
23
- resultCache.clear?.();
24
- }
25
- },
26
31
  onParse({ params, extendContext }) {
27
32
  extendContext({ [rawDocumentSymbol]: params.source.toString() });
28
33
  },
@@ -30,6 +35,7 @@ const useValidationCache = (pluginOptions = {}) => {
30
35
  // We use setValidateFn over accessing params.rules directly, as other plugins in the chain might add more rules.
31
36
  // This would cause an issue if we are constructing the cache key here already.
32
37
  setValidationFn((...args) => {
38
+ const schemaHashKey = getSchemaHash(args[0]);
33
39
  let ruleKey = '';
34
40
  if (Array.isArray(args[2])) {
35
41
  // Note: We could also order them... but that might be too much
@@ -37,7 +43,7 @@ const useValidationCache = (pluginOptions = {}) => {
37
43
  ruleKey = ruleKey + rule.name;
38
44
  }
39
45
  }
40
- const key = ruleKey + (context[rawDocumentSymbol] ?? (0, graphql_1.print)(params.documentAST));
46
+ const key = schemaHashKey + `|` + ruleKey + `|` + (context[rawDocumentSymbol] ?? (0, graphql_1.print)(params.documentAST));
41
47
  const cachedResult = resultCache.get(key);
42
48
  if (cachedResult !== undefined) {
43
49
  return cachedResult;
package/esm/index.js CHANGED
@@ -1,8 +1,21 @@
1
- import { print } from 'graphql';
1
+ import { print, introspectionFromSchema } from 'graphql';
2
+ import jsonStableStringify from 'fast-json-stable-stringify';
2
3
  import LRU from 'lru-cache';
4
+ import sha1 from 'js-sha1';
3
5
  const DEFAULT_MAX = 1000;
4
6
  const DEFAULT_TTL = 3600000;
5
7
  const rawDocumentSymbol = Symbol('rawDocument');
8
+ const schemaHashCache = new WeakMap();
9
+ function getSchemaHash(schema) {
10
+ let hash = schemaHashCache.get(schema);
11
+ if (hash) {
12
+ return hash;
13
+ }
14
+ const introspection = introspectionFromSchema(schema);
15
+ hash = sha1(jsonStableStringify(introspection.__schema));
16
+ schemaHashCache.set(schema, hash);
17
+ return hash;
18
+ }
6
19
  export const useValidationCache = (pluginOptions = {}) => {
7
20
  const resultCache = typeof pluginOptions.cache !== 'undefined'
8
21
  ? pluginOptions.cache
@@ -11,14 +24,6 @@ export const useValidationCache = (pluginOptions = {}) => {
11
24
  maxAge: DEFAULT_TTL,
12
25
  });
13
26
  return {
14
- onSchemaChange() {
15
- if (resultCache.reset) {
16
- resultCache.reset?.();
17
- }
18
- else if ('clear' in resultCache) {
19
- resultCache.clear?.();
20
- }
21
- },
22
27
  onParse({ params, extendContext }) {
23
28
  extendContext({ [rawDocumentSymbol]: params.source.toString() });
24
29
  },
@@ -26,6 +31,7 @@ export const useValidationCache = (pluginOptions = {}) => {
26
31
  // We use setValidateFn over accessing params.rules directly, as other plugins in the chain might add more rules.
27
32
  // This would cause an issue if we are constructing the cache key here already.
28
33
  setValidationFn((...args) => {
34
+ const schemaHashKey = getSchemaHash(args[0]);
29
35
  let ruleKey = '';
30
36
  if (Array.isArray(args[2])) {
31
37
  // Note: We could also order them... but that might be too much
@@ -33,7 +39,7 @@ export const useValidationCache = (pluginOptions = {}) => {
33
39
  ruleKey = ruleKey + rule.name;
34
40
  }
35
41
  }
36
- const key = ruleKey + (context[rawDocumentSymbol] ?? print(params.documentAST));
42
+ const key = schemaHashKey + `|` + ruleKey + `|` + (context[rawDocumentSymbol] ?? print(params.documentAST));
37
43
  const cachedResult = resultCache.get(key);
38
44
  if (cachedResult !== undefined) {
39
45
  return cachedResult;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@envelop/validation-cache",
3
- "version": "5.0.5",
3
+ "version": "5.1.0-alpha-20221222121455-d169c23d",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
6
  "@envelop/core": "^3.0.4",
@@ -8,7 +8,9 @@
8
8
  },
9
9
  "dependencies": {
10
10
  "lru-cache": "^6.0.0",
11
- "tslib": "^2.4.0"
11
+ "tslib": "^2.4.0",
12
+ "fast-json-stable-stringify": "^2.1.0",
13
+ "js-sha1": "^0.6.0"
12
14
  },
13
15
  "repository": {
14
16
  "type": "git",
@@ -9,14 +9,6 @@ export interface ValidationCache {
9
9
  * Set a result to the validation cache.
10
10
  */
11
11
  set(key: string, value: readonly GraphQLError[]): void;
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;
20
12
  }
21
13
  export declare type ValidationCacheOptions = {
22
14
  cache?: ValidationCache;
@@ -9,14 +9,6 @@ export interface ValidationCache {
9
9
  * Set a result to the validation cache.
10
10
  */
11
11
  set(key: string, value: readonly GraphQLError[]): void;
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;
20
12
  }
21
13
  export declare type ValidationCacheOptions = {
22
14
  cache?: ValidationCache;