@envelop/validation-cache 5.0.5-alpha-20221216195722-efa72eac → 5.0.5

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.
Files changed (3) hide show
  1. package/cjs/index.js +18 -15
  2. package/esm/index.js +18 -15
  3. package/package.json +1 -1
package/cjs/index.js CHANGED
@@ -26,23 +26,26 @@ const useValidationCache = (pluginOptions = {}) => {
26
26
  onParse({ params, extendContext }) {
27
27
  extendContext({ [rawDocumentSymbol]: params.source.toString() });
28
28
  },
29
- onValidate({ params, context, setResult }) {
30
- let ruleKey = '';
31
- if (Array.isArray(params.rules)) {
32
- // Note: We could also order them... but that might be too much
33
- for (const rule of params.rules) {
34
- ruleKey = ruleKey + rule.name;
29
+ onValidate({ params, context, setValidationFn, validateFn }) {
30
+ // We use setValidateFn over accessing params.rules directly, as other plugins in the chain might add more rules.
31
+ // This would cause an issue if we are constructing the cache key here already.
32
+ setValidationFn((...args) => {
33
+ let ruleKey = '';
34
+ if (Array.isArray(args[2])) {
35
+ // Note: We could also order them... but that might be too much
36
+ for (const rule of args[2]) {
37
+ ruleKey = ruleKey + rule.name;
38
+ }
35
39
  }
36
- }
37
- const key = ruleKey + (context[rawDocumentSymbol] ?? (0, graphql_1.print)(params.documentAST));
38
- const cachedResult = resultCache.get(key);
39
- if (cachedResult !== undefined) {
40
- setResult(cachedResult);
41
- }
42
- return ({ result }) => {
43
- // @ts-expect-error TODO: not sure how we will make it dev friendly
40
+ const key = ruleKey + (context[rawDocumentSymbol] ?? (0, graphql_1.print)(params.documentAST));
41
+ const cachedResult = resultCache.get(key);
42
+ if (cachedResult !== undefined) {
43
+ return cachedResult;
44
+ }
45
+ const result = validateFn(...args);
44
46
  resultCache.set(key, result);
45
- };
47
+ return result;
48
+ });
46
49
  },
47
50
  };
48
51
  };
package/esm/index.js CHANGED
@@ -22,23 +22,26 @@ export const useValidationCache = (pluginOptions = {}) => {
22
22
  onParse({ params, extendContext }) {
23
23
  extendContext({ [rawDocumentSymbol]: params.source.toString() });
24
24
  },
25
- onValidate({ params, context, setResult }) {
26
- let ruleKey = '';
27
- if (Array.isArray(params.rules)) {
28
- // Note: We could also order them... but that might be too much
29
- for (const rule of params.rules) {
30
- ruleKey = ruleKey + rule.name;
25
+ onValidate({ params, context, setValidationFn, validateFn }) {
26
+ // We use setValidateFn over accessing params.rules directly, as other plugins in the chain might add more rules.
27
+ // This would cause an issue if we are constructing the cache key here already.
28
+ setValidationFn((...args) => {
29
+ let ruleKey = '';
30
+ if (Array.isArray(args[2])) {
31
+ // Note: We could also order them... but that might be too much
32
+ for (const rule of args[2]) {
33
+ ruleKey = ruleKey + rule.name;
34
+ }
31
35
  }
32
- }
33
- const key = ruleKey + (context[rawDocumentSymbol] ?? print(params.documentAST));
34
- const cachedResult = resultCache.get(key);
35
- if (cachedResult !== undefined) {
36
- setResult(cachedResult);
37
- }
38
- return ({ result }) => {
39
- // @ts-expect-error TODO: not sure how we will make it dev friendly
36
+ const key = ruleKey + (context[rawDocumentSymbol] ?? print(params.documentAST));
37
+ const cachedResult = resultCache.get(key);
38
+ if (cachedResult !== undefined) {
39
+ return cachedResult;
40
+ }
41
+ const result = validateFn(...args);
40
42
  resultCache.set(key, result);
41
- };
43
+ return result;
44
+ });
42
45
  },
43
46
  };
44
47
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@envelop/validation-cache",
3
- "version": "5.0.5-alpha-20221216195722-efa72eac",
3
+ "version": "5.0.5",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
6
  "@envelop/core": "^3.0.4",