@envelop/validation-cache 5.0.4 → 5.0.5-alpha-20221216222744-232e3b3d
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 +19 -9
- package/esm/index.js +19 -9
- package/package.json +1 -1
package/cjs/index.js
CHANGED
|
@@ -26,16 +26,26 @@ const useValidationCache = (pluginOptions = {}) => {
|
|
|
26
26
|
onParse({ params, extendContext }) {
|
|
27
27
|
extendContext({ [rawDocumentSymbol]: params.source.toString() });
|
|
28
28
|
},
|
|
29
|
-
onValidate({ params, context,
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
+
}
|
|
39
|
+
}
|
|
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);
|
|
37
46
|
resultCache.set(key, result);
|
|
38
|
-
|
|
47
|
+
return result;
|
|
48
|
+
});
|
|
39
49
|
},
|
|
40
50
|
};
|
|
41
51
|
};
|
package/esm/index.js
CHANGED
|
@@ -22,16 +22,26 @@ export const useValidationCache = (pluginOptions = {}) => {
|
|
|
22
22
|
onParse({ params, extendContext }) {
|
|
23
23
|
extendContext({ [rawDocumentSymbol]: params.source.toString() });
|
|
24
24
|
},
|
|
25
|
-
onValidate({ params, context,
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
+
}
|
|
35
|
+
}
|
|
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);
|
|
33
42
|
resultCache.set(key, result);
|
|
34
|
-
|
|
43
|
+
return result;
|
|
44
|
+
});
|
|
35
45
|
},
|
|
36
46
|
};
|
|
37
47
|
};
|