@envelop/extended-validation
Advanced tools
Comparing version 4.0.0 to 4.1.0-alpha-20240806125641-9b9885ba
@@ -5,2 +5,3 @@ "use strict"; | ||
const graphql_1 = require("graphql"); | ||
const core_1 = require("@envelop/core"); | ||
const symbolExtendedValidationRules = Symbol('extendedValidationContext'); | ||
@@ -31,8 +32,8 @@ const useExtendedValidation = (options) => { | ||
}, | ||
onSubscribe: buildHandler('subscribe', getTypeInfo, options.onValidationFailed), | ||
onExecute: buildHandler('execute', getTypeInfo, options.onValidationFailed), | ||
onSubscribe: buildHandler('subscribe', getTypeInfo, options.onValidationFailed, options.rejectOnErrors !== false), | ||
onExecute: buildHandler('execute', getTypeInfo, options.onValidationFailed, options.rejectOnErrors !== false), | ||
}; | ||
}; | ||
exports.useExtendedValidation = useExtendedValidation; | ||
function buildHandler(name, getTypeInfo, onValidationFailed) { | ||
function buildHandler(name, getTypeInfo, onValidationFailed, rejectOnErrors = true) { | ||
return function handler({ args, setResultAndStopExecution, }) { | ||
@@ -59,12 +60,45 @@ // We hook into onExecute/onSubscribe even though this is a validation pattern. The reasoning behind | ||
const visitor = (0, graphql_1.visitInParallel)(validationRulesContext.rules.map(rule => rule(validationContext, args))); | ||
(0, graphql_1.visit)(args.document, (0, graphql_1.visitWithTypeInfo)(typeInfo, visitor)); | ||
args.document = (0, graphql_1.visit)(args.document, (0, graphql_1.visitWithTypeInfo)(typeInfo, visitor)); | ||
if (errors.length > 0) { | ||
let result = { | ||
data: null, | ||
errors, | ||
}; | ||
if (onValidationFailed) { | ||
onValidationFailed({ args, result, setResult: newResult => (result = newResult) }); | ||
if (rejectOnErrors) { | ||
let result = { | ||
data: null, | ||
errors, | ||
}; | ||
if (onValidationFailed) { | ||
onValidationFailed({ args, result, setResult: newResult => (result = newResult) }); | ||
} | ||
setResultAndStopExecution(result); | ||
} | ||
setResultAndStopExecution(result); | ||
else { | ||
// eslint-disable-next-line no-inner-declarations | ||
function onResult({ result, setResult, }) { | ||
if ((0, core_1.isAsyncIterable)(result)) { | ||
// rejectOnErrors is false doesn't work with async iterables | ||
setResult({ | ||
data: null, | ||
errors, | ||
}); | ||
return; | ||
} | ||
const newResult = { | ||
...result, | ||
errors: [...(result.errors || []), ...errors], | ||
}; | ||
errors.forEach(e => { | ||
if (e.path?.length) { | ||
let currentData = (newResult.data ||= {}); | ||
for (const pathItem of e.path.slice(0, -1)) { | ||
currentData = currentData[pathItem] ||= {}; | ||
} | ||
currentData[e.path[e.path.length - 1]] = null; | ||
} | ||
}); | ||
setResult(newResult); | ||
} | ||
return { | ||
onSubscribeResult: onResult, | ||
onExecuteDone: onResult, | ||
}; | ||
} | ||
} | ||
@@ -71,0 +105,0 @@ } |
import { TypeInfo, ValidationContext, visit, visitInParallel, visitWithTypeInfo, } from 'graphql'; | ||
import { isAsyncIterable, } from '@envelop/core'; | ||
const symbolExtendedValidationRules = Symbol('extendedValidationContext'); | ||
@@ -27,7 +28,7 @@ export const useExtendedValidation = (options) => { | ||
}, | ||
onSubscribe: buildHandler('subscribe', getTypeInfo, options.onValidationFailed), | ||
onExecute: buildHandler('execute', getTypeInfo, options.onValidationFailed), | ||
onSubscribe: buildHandler('subscribe', getTypeInfo, options.onValidationFailed, options.rejectOnErrors !== false), | ||
onExecute: buildHandler('execute', getTypeInfo, options.onValidationFailed, options.rejectOnErrors !== false), | ||
}; | ||
}; | ||
function buildHandler(name, getTypeInfo, onValidationFailed) { | ||
function buildHandler(name, getTypeInfo, onValidationFailed, rejectOnErrors = true) { | ||
return function handler({ args, setResultAndStopExecution, }) { | ||
@@ -54,12 +55,45 @@ // We hook into onExecute/onSubscribe even though this is a validation pattern. The reasoning behind | ||
const visitor = visitInParallel(validationRulesContext.rules.map(rule => rule(validationContext, args))); | ||
visit(args.document, visitWithTypeInfo(typeInfo, visitor)); | ||
args.document = visit(args.document, visitWithTypeInfo(typeInfo, visitor)); | ||
if (errors.length > 0) { | ||
let result = { | ||
data: null, | ||
errors, | ||
}; | ||
if (onValidationFailed) { | ||
onValidationFailed({ args, result, setResult: newResult => (result = newResult) }); | ||
if (rejectOnErrors) { | ||
let result = { | ||
data: null, | ||
errors, | ||
}; | ||
if (onValidationFailed) { | ||
onValidationFailed({ args, result, setResult: newResult => (result = newResult) }); | ||
} | ||
setResultAndStopExecution(result); | ||
} | ||
setResultAndStopExecution(result); | ||
else { | ||
// eslint-disable-next-line no-inner-declarations | ||
function onResult({ result, setResult, }) { | ||
if (isAsyncIterable(result)) { | ||
// rejectOnErrors is false doesn't work with async iterables | ||
setResult({ | ||
data: null, | ||
errors, | ||
}); | ||
return; | ||
} | ||
const newResult = { | ||
...result, | ||
errors: [...(result.errors || []), ...errors], | ||
}; | ||
errors.forEach(e => { | ||
if (e.path?.length) { | ||
let currentData = (newResult.data ||= {}); | ||
for (const pathItem of e.path.slice(0, -1)) { | ||
currentData = currentData[pathItem] ||= {}; | ||
} | ||
currentData[e.path[e.path.length - 1]] = null; | ||
} | ||
}); | ||
setResult(newResult); | ||
} | ||
return { | ||
onSubscribeResult: onResult, | ||
onExecuteDone: onResult, | ||
}; | ||
} | ||
} | ||
@@ -66,0 +100,0 @@ } |
{ | ||
"name": "@envelop/extended-validation", | ||
"version": "4.0.0", | ||
"version": "4.1.0-alpha-20240806125641-9b9885ba", | ||
"sideEffects": false, | ||
@@ -5,0 +5,0 @@ "peerDependencies": { |
@@ -20,2 +20,8 @@ import { ExecutionArgs, ExecutionResult } from 'graphql'; | ||
onValidationFailed?: OnValidationFailedCallback; | ||
/** | ||
* Reject the execution if the validation fails. | ||
* | ||
* @default true | ||
*/ | ||
rejectOnErrors?: boolean; | ||
}) => Plugin<PluginContext & { | ||
@@ -22,0 +28,0 @@ [symbolExtendedValidationRules]?: ExtendedValidationContext | undefined; |
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
28225
417
2