@envelop/validation-cache
Advanced tools
Comparing version 4.4.0-alpha-3936561.0 to 4.4.0-alpha-aca44e1.0
@@ -1,8 +0,6 @@ | ||
import { Plugin } from '@envelop/core'; | ||
import { BasicCache, Plugin } from '@envelop/core'; | ||
import { GraphQLError } from 'graphql'; | ||
export interface ValidationCache { | ||
get(key: string): readonly GraphQLError[] | undefined; | ||
set(key: string, value: readonly GraphQLError[]): void; | ||
export declare type ValidationCache = BasicCache<readonly GraphQLError[]> & { | ||
clear(): void; | ||
} | ||
}; | ||
export declare type ValidationCacheOptions = { | ||
@@ -9,0 +7,0 @@ cache?: ValidationCache; |
12
index.js
@@ -8,5 +8,5 @@ 'use strict'; | ||
const rawDocumentSymbol = Symbol('rawDocument'); | ||
const rawDocumentMap = new WeakMap(); | ||
const useValidationCache = (pluginOptions = {}) => { | ||
const resultCache = typeof pluginOptions.cache !== 'undefined' ? pluginOptions.cache : core.getInMemoryLRUCache(); | ||
const resultCache = pluginOptions.cache != null ? pluginOptions.cache : core.getInMemoryLRUCache(); | ||
return { | ||
@@ -16,11 +16,13 @@ onSchemaChange() { | ||
}, | ||
onParse({ params, extendContext }) { | ||
extendContext({ [rawDocumentSymbol]: params.source.toString() }); | ||
onParse({ params, context }) { | ||
var _a; | ||
rawDocumentMap.set(context, core.isSourceObject(params.source) ? params.source.body : (_a = params.source) === null || _a === void 0 ? void 0 : _a.toString()); | ||
}, | ||
onValidate({ params, context, setResult }) { | ||
var _a; | ||
const key = (_a = context[rawDocumentSymbol]) !== null && _a !== void 0 ? _a : graphql.print(params.documentAST); | ||
const key = (_a = rawDocumentMap.get(context)) !== null && _a !== void 0 ? _a : graphql.print(params.documentAST); | ||
const cachedResult = resultCache.get(key); | ||
if (cachedResult !== undefined) { | ||
setResult(cachedResult); | ||
return; | ||
} | ||
@@ -27,0 +29,0 @@ return ({ result }) => { |
{ | ||
"name": "@envelop/validation-cache", | ||
"version": "4.4.0-alpha-3936561.0", | ||
"version": "4.4.0-alpha-aca44e1.0", | ||
"sideEffects": false, | ||
"peerDependencies": { | ||
"@envelop/core": "2.4.0-alpha-3936561.0", | ||
"@envelop/core": "2.4.0-alpha-aca44e1.0", | ||
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0" | ||
@@ -8,0 +8,0 @@ }, |
Sorry, the diff of this file is not supported yet
4220
67