@envelop/extended-validation
Advanced tools
Comparing version 1.3.5-alpha-0d78849.0 to 1.3.5-alpha-1c290fd.0
66
index.js
@@ -8,3 +8,3 @@ 'use strict'; | ||
const SYMBOL_EXTENDED_VALIDATION_RULES = Symbol('SYMBOL_EXTENDED_VALIDATION_RULES'); | ||
const symbolExtendedValidationRules = Symbol('extendedValidationContext'); | ||
const useExtendedValidation = (options) => { | ||
@@ -16,9 +16,15 @@ let schemaTypeInfo; | ||
}, | ||
onParse({ context, extendContext }) { | ||
var _a; | ||
const rules = (_a = context === null || context === void 0 ? void 0 : context[SYMBOL_EXTENDED_VALIDATION_RULES]) !== null && _a !== void 0 ? _a : []; | ||
rules.push(...options.rules); | ||
extendContext({ | ||
[SYMBOL_EXTENDED_VALIDATION_RULES]: rules, | ||
}); | ||
onContextBuilding({ context, extendContext }) { | ||
// We initialize the validationRules context in onContextBuilding as onExecute is already too late! | ||
let validationRulesContext = context[symbolExtendedValidationRules]; | ||
if (validationRulesContext === undefined) { | ||
validationRulesContext = { | ||
rules: [], | ||
didRun: false, | ||
}; | ||
extendContext({ | ||
[symbolExtendedValidationRules]: validationRulesContext, | ||
}); | ||
} | ||
validationRulesContext.rules.push(...options.rules); | ||
}, | ||
@@ -30,20 +36,30 @@ onExecute({ args, setResultAndStopExecution }) { | ||
// we may use execution context in the validation rules. | ||
const rules = args.contextValue[SYMBOL_EXTENDED_VALIDATION_RULES]; | ||
const errors = []; | ||
// We replicate the default validation step manually before execution starts. | ||
const typeInfo = schemaTypeInfo || new graphql.TypeInfo(args.schema); | ||
const validationContext = new graphql.ValidationContext(args.schema, args.document, typeInfo, e => { | ||
errors.push(e); | ||
}); | ||
const visitor = graphql.visitInParallel(rules.map(rule => rule(validationContext, args))); | ||
graphql.visit(args.document, graphql.visitWithTypeInfo(typeInfo, visitor)); | ||
if (errors.length > 0) { | ||
let result = { | ||
data: null, | ||
errors, | ||
}; | ||
if (options.onValidationFailed) { | ||
options.onValidationFailed({ args, result, setResult: newResult => (result = newResult) }); | ||
const validationRulesContext = args.contextValue[symbolExtendedValidationRules]; | ||
if (validationRulesContext === undefined) { | ||
throw new Error('Plugin has not been properly set up. ' + | ||
"The 'contextFactory' function is not invoked and the result has not been passed to 'execute'."); | ||
} | ||
// we only want to run the extended execution once. | ||
if (validationRulesContext.didRun === false) { | ||
validationRulesContext.didRun = true; | ||
if (validationRulesContext.rules.length !== 0) { | ||
const errors = []; | ||
// We replicate the default validation step manually before execution starts. | ||
const typeInfo = schemaTypeInfo || new graphql.TypeInfo(args.schema); | ||
const validationContext = new graphql.ValidationContext(args.schema, args.document, typeInfo, e => { | ||
errors.push(e); | ||
}); | ||
const visitor = graphql.visitInParallel(validationRulesContext.rules.map(rule => rule(validationContext, args))); | ||
graphql.visit(args.document, graphql.visitWithTypeInfo(typeInfo, visitor)); | ||
if (errors.length > 0) { | ||
let result = { | ||
data: null, | ||
errors, | ||
}; | ||
if (options.onValidationFailed) { | ||
options.onValidationFailed({ args, result, setResult: newResult => (result = newResult) }); | ||
} | ||
setResultAndStopExecution(result); | ||
} | ||
} | ||
return setResultAndStopExecution(result); | ||
} | ||
@@ -50,0 +66,0 @@ }, |
{ | ||
"name": "@envelop/extended-validation", | ||
"version": "1.3.5-alpha-0d78849.0", | ||
"version": "1.3.5-alpha-1c290fd.0", | ||
"sideEffects": false, | ||
"peerDependencies": { | ||
"@envelop/core": "2.0.1-alpha-0d78849.0", | ||
"@envelop/core": "^2.0.0", | ||
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0" | ||
@@ -8,0 +8,0 @@ }, |
@@ -5,3 +5,3 @@ import { Plugin } from '@envelop/core'; | ||
export declare const useExtendedValidation: (options: { | ||
rules: ExtendedValidationRule[]; | ||
rules: Array<ExtendedValidationRule>; | ||
/** | ||
@@ -8,0 +8,0 @@ * Callback that is invoked if the extended validation yields any errors. |
Sorry, the diff of this file is not supported yet
19692
308