@envelop/validation-cache 4.3.1 → 4.4.0-alpha-35741fe.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/index.d.ts CHANGED
@@ -1,10 +1,8 @@
1
- import { Plugin } from '@envelop/core';
1
+ import { BasicCache, Plugin } from '@envelop/core';
2
2
  import { GraphQLError } from 'graphql';
3
- export interface ValidationCache {
4
- get(key: string): readonly GraphQLError[] | undefined;
5
- set(key: string, value: readonly GraphQLError[]): void;
3
+ export declare type ValidationCache = BasicCache<readonly GraphQLError[]> & {
6
4
  clear(): void;
7
- }
5
+ };
8
6
  export declare type ValidationCacheOptions = {
9
7
  cache?: ValidationCache;
10
8
  };
package/index.js CHANGED
@@ -2,29 +2,27 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
-
5
+ const core = require('@envelop/core');
7
6
  const graphql = require('graphql');
8
- const lru = _interopDefault(require('tiny-lru'));
9
7
 
10
- const DEFAULT_MAX = 1000;
11
- const DEFAULT_TTL = 3600000;
12
- const rawDocumentSymbol = Symbol('rawDocument');
8
+ const rawDocumentMap = new WeakMap();
13
9
  const useValidationCache = (pluginOptions = {}) => {
14
- const resultCache = typeof pluginOptions.cache !== 'undefined' ? pluginOptions.cache : lru(DEFAULT_MAX, DEFAULT_TTL);
10
+ const resultCache = typeof pluginOptions.cache !== 'undefined' ? pluginOptions.cache : core.getInMemoryLRUCache();
15
11
  return {
16
12
  onSchemaChange() {
17
13
  resultCache.clear();
18
14
  },
19
- onParse({ params, extendContext }) {
20
- extendContext({ [rawDocumentSymbol]: params.source.toString() });
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());
21
18
  },
22
19
  onValidate({ params, context, setResult }) {
23
20
  var _a;
24
- const key = (_a = context[rawDocumentSymbol]) !== null && _a !== void 0 ? _a : graphql.print(params.documentAST);
21
+ const key = (_a = rawDocumentMap.get(context)) !== null && _a !== void 0 ? _a : graphql.print(params.documentAST);
25
22
  const cachedResult = resultCache.get(key);
26
23
  if (cachedResult !== undefined) {
27
24
  setResult(cachedResult);
25
+ return;
28
26
  }
29
27
  return ({ result }) => {
30
28
  resultCache.set(key, result);
package/index.mjs CHANGED
@@ -1,24 +1,24 @@
1
+ import { getInMemoryLRUCache, isSourceObject } from '@envelop/core';
1
2
  import { print } from 'graphql';
2
- import lru from 'tiny-lru';
3
3
 
4
- const DEFAULT_MAX = 1000;
5
- const DEFAULT_TTL = 3600000;
6
- const rawDocumentSymbol = Symbol('rawDocument');
4
+ const rawDocumentMap = new WeakMap();
7
5
  const useValidationCache = (pluginOptions = {}) => {
8
- const resultCache = typeof pluginOptions.cache !== 'undefined' ? pluginOptions.cache : lru(DEFAULT_MAX, DEFAULT_TTL);
6
+ const resultCache = typeof pluginOptions.cache !== 'undefined' ? pluginOptions.cache : getInMemoryLRUCache();
9
7
  return {
10
8
  onSchemaChange() {
11
9
  resultCache.clear();
12
10
  },
13
- onParse({ params, extendContext }) {
14
- extendContext({ [rawDocumentSymbol]: params.source.toString() });
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());
15
14
  },
16
15
  onValidate({ params, context, setResult }) {
17
16
  var _a;
18
- const key = (_a = context[rawDocumentSymbol]) !== null && _a !== void 0 ? _a : print(params.documentAST);
17
+ const key = (_a = rawDocumentMap.get(context)) !== null && _a !== void 0 ? _a : print(params.documentAST);
19
18
  const cachedResult = resultCache.get(key);
20
19
  if (cachedResult !== undefined) {
21
20
  setResult(cachedResult);
21
+ return;
22
22
  }
23
23
  return ({ result }) => {
24
24
  resultCache.set(key, result);
package/package.json CHANGED
@@ -1,14 +1,11 @@
1
1
  {
2
2
  "name": "@envelop/validation-cache",
3
- "version": "4.3.1",
3
+ "version": "4.4.0-alpha-35741fe.0",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
- "@envelop/core": "^2.3.1",
6
+ "@envelop/core": "2.4.0-alpha-35741fe.0",
7
7
  "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
8
8
  },
9
- "dependencies": {
10
- "tiny-lru": "7.0.6"
11
- },
12
9
  "repository": {
13
10
  "type": "git",
14
11
  "url": "https://github.com/dotansimha/envelop.git",