@envelop/extended-validation
Advanced tools
Comparing version 2.0.0-alpha-4ba70ab.0 to 2.0.0-alpha-d0d0776.0
@@ -1,2 +0,2 @@ | ||
import { ASTVisitor, DirectiveNode, ExecutionArgs, GraphQLNamedType, GraphQLType, ValidationContext } from 'graphql'; | ||
import { ASTVisitor, DirectiveNode, ExecutionArgs, ValidationContext } from 'graphql'; | ||
export declare type ExtendedValidationRule = (context: ValidationContext, executionArgs: ExecutionArgs) => ASTVisitor; | ||
@@ -6,2 +6,1 @@ export declare function getDirectiveFromAstNode(astNode: { | ||
}, names: string | string[]): null | DirectiveNode; | ||
export declare function unwrapType(type: GraphQLType): GraphQLNamedType; |
39
index.js
@@ -38,3 +38,2 @@ 'use strict'; | ||
return function handler({ args, setResultAndStopExecution, }) { | ||
var _a; | ||
// We hook into onExecute/onSubscribe even though this is a validation pattern. The reasoning behind | ||
@@ -55,3 +54,3 @@ // it is that hooking right after validation and before execution has started is the | ||
// We replicate the default validation step manually before execution starts. | ||
const typeInfo = (_a = getTypeInfo()) !== null && _a !== void 0 ? _a : new graphql.TypeInfo(args.schema); | ||
const typeInfo = getTypeInfo() ?? new graphql.TypeInfo(args.schema); | ||
const validationContext = new graphql.ValidationContext(args.schema, args.document, typeInfo, e => { | ||
@@ -83,8 +82,2 @@ errors.push(e); | ||
} | ||
function unwrapType(type) { | ||
if (graphql.isNonNullType(type) || graphql.isListType(type)) { | ||
return unwrapType(type.ofType); | ||
} | ||
return type; | ||
} | ||
@@ -97,4 +90,3 @@ const ONE_OF_DIRECTIVE_SDL = /* GraphQL */ ` | ||
Field: node => { | ||
var _a, _b; | ||
if ((_a = node.arguments) === null || _a === void 0 ? void 0 : _a.length) { | ||
if (node.arguments?.length) { | ||
const fieldType = validationContext.getFieldDef(); | ||
@@ -105,8 +97,6 @@ if (!fieldType) { | ||
const values = utils.getArgumentValues(fieldType, node, executionArgs.variableValues || undefined); | ||
if (fieldType) { | ||
const isOneOfFieldType = ((_b = fieldType.extensions) === null || _b === void 0 ? void 0 : _b.oneOf) || (fieldType.astNode && getDirectiveFromAstNode(fieldType.astNode, 'oneOf')); | ||
if (isOneOfFieldType) { | ||
if (Object.keys(values).length !== 1) { | ||
validationContext.reportError(new graphql.GraphQLError(`Exactly one key must be specified for input for field "${fieldType.type.toString()}.${node.name.value}"`, [node])); | ||
} | ||
const isOneOfFieldType = fieldType.extensions?.oneOf || (fieldType.astNode && getDirectiveFromAstNode(fieldType.astNode, 'oneOf')); | ||
if (isOneOfFieldType) { | ||
if (Object.keys(values).length !== 1) { | ||
validationContext.reportError(new graphql.GraphQLError(`Exactly one key must be specified for input for field "${fieldType.type.toString()}.${node.name.value}"`, [node])); | ||
} | ||
@@ -124,4 +114,9 @@ } | ||
}; | ||
function getNonNullType(ttype) { | ||
if (ttype instanceof graphql.GraphQLNonNull) { | ||
return ttype.ofType; | ||
} | ||
return ttype; | ||
} | ||
function traverseVariables(validationContext, arg, graphqlType, currentValue) { | ||
var _a; | ||
// if the current value is empty we don't need to traverse deeper | ||
@@ -132,3 +127,4 @@ // if it shouldn't be empty, the "original" validation phase should complain. | ||
} | ||
if (graphql.isListType(graphqlType)) { | ||
const unwrappedType = getNonNullType(graphqlType); | ||
if (graphql.isListType(unwrappedType)) { | ||
if (!Array.isArray(currentValue)) { | ||
@@ -139,3 +135,3 @@ // because of graphql type coercion a single object should be treated as an array of one object | ||
currentValue.forEach(value => { | ||
traverseVariables(validationContext, arg, graphqlType.ofType, value); | ||
traverseVariables(validationContext, arg, unwrappedType.ofType, value); | ||
}); | ||
@@ -148,4 +144,4 @@ return; | ||
} | ||
const inputType = unwrapType(graphqlType); | ||
const isOneOfInputType = ((_a = inputType.extensions) === null || _a === void 0 ? void 0 : _a.oneOf) || (inputType.astNode && getDirectiveFromAstNode(inputType.astNode, 'oneOf')); | ||
const inputType = graphql.getNamedType(graphqlType); | ||
const isOneOfInputType = inputType.extensions?.oneOf || (inputType.astNode && getDirectiveFromAstNode(inputType.astNode, 'oneOf')); | ||
if (isOneOfInputType) { | ||
@@ -166,3 +162,2 @@ if (Object.keys(currentValue).length !== 1) { | ||
exports.getDirectiveFromAstNode = getDirectiveFromAstNode; | ||
exports.unwrapType = unwrapType; | ||
exports.useExtendedValidation = useExtendedValidation; |
{ | ||
"name": "@envelop/extended-validation", | ||
"version": "2.0.0-alpha-4ba70ab.0", | ||
"version": "2.0.0-alpha-d0d0776.0", | ||
"sideEffects": false, | ||
"peerDependencies": { | ||
"@envelop/core": "2.1.0-alpha-4ba70ab.0", | ||
"@envelop/core": "3.0.0-alpha-d0d0776.0", | ||
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0" | ||
@@ -14,3 +14,3 @@ }, | ||
"type": "git", | ||
"url": "https://github.com/dotansimha/envelop.git", | ||
"url": "https://github.com/n1ru4l/envelop.git", | ||
"directory": "packages/plugins/extended-validation" | ||
@@ -28,8 +28,20 @@ }, | ||
".": { | ||
"require": "./index.js", | ||
"import": "./index.mjs" | ||
"require": { | ||
"default": "./index.js", | ||
"types": "./index.d.ts" | ||
}, | ||
"import": { | ||
"default": "./index.mjs", | ||
"types": "./index.d.ts" | ||
} | ||
}, | ||
"./*": { | ||
"require": "./*.js", | ||
"import": "./*.mjs" | ||
"require": { | ||
"default": "./*.js", | ||
"types": "./*.d.ts" | ||
}, | ||
"import": { | ||
"default": "./*.mjs", | ||
"types": "./*.d.ts" | ||
} | ||
}, | ||
@@ -36,0 +48,0 @@ "./package.json": "./package.json" |
Sorry, the diff of this file is not supported yet
19800
316