v16 APIgraphql/validationValidation Context

graphql/validation/validation-context

Classes

ValidationContext

Validation context passed to query validation rules.

Constructor

Creates a ValidationContext instance.

Signature:

new ValidationContext(schema: GraphQLSchema, ast: DocumentNode, typeInfo: TypeInfo, onError: (error: GraphQLError): void)
Arguments
NameTypeDescription
schemaGraphQLSchemaThe GraphQL schema to use.
astDocumentNodeThe AST value node to inspect or convert.
typeInfoTypeInfoThe type info.
onError(error: GraphQLError): voidThe on error.
Returns
TypeDescription
ValidationContext

getSchema()

Returns the schema being used by this validation context.

Signature:

getSchema(): GraphQLSchema
Returns
TypeDescription
GraphQLSchemaThe schema being validated against.
Example
// Given a ValidationContext instance named context:
const result = context.getSchema();
 
// result contains the getSchema return value

getVariableUsages()

Returns variable usages found directly within this node.

Signature:

getVariableUsages(node: OperationDefinitionNode | FragmentDefinitionNode): readonly { node: VariableNode; type: null | undefined | GraphQLInputType; defaultValue: null | undefined | unknown; parentType: null | undefined | GraphQLInputType }[]
Arguments
NameTypeDescription
nodeOperationDefinitionNode | FragmentDefinitionNodeThe AST node to inspect or visit.
Returns
TypeDescription
readonly { node: VariableNode; type: null | undefined | GraphQLInputType; defaultValue: null | undefined | unknown; parentType: null | undefined | GraphQLInputType }[]Variable usages found directly within this node.
Example
// Given a ValidationContext instance named context:
const result = context.getVariableUsages(node);
 
// result contains the getVariableUsages return value

getRecursiveVariableUsages()

Returns variable usages for an operation, including variables used by referenced fragments.

Signature:

getRecursiveVariableUsages(operation: OperationDefinitionNode): readonly { node: VariableNode; type: null | undefined | GraphQLInputType; defaultValue: null | undefined | unknown; parentType: null | undefined | GraphQLInputType }[]
Arguments
NameTypeDescription
operationOperationDefinitionNodeOperation definition to inspect.
Returns
TypeDescription
readonly { node: VariableNode; type: null | undefined | GraphQLInputType; defaultValue: null | undefined | unknown; parentType: null | undefined | GraphQLInputType }[]Variable usages reachable from the operation.
Example
// Given a ValidationContext instance named context:
const result = context.getRecursiveVariableUsages(operation);
 
// result contains the getRecursiveVariableUsages return value

getType()

Returns the current output type at this point in traversal.

Signature:

getType(): null | undefined | GraphQLOutputType
Returns
TypeDescription
null | undefined | GraphQLOutputTypeThe current output type, if known.
Example
// Given a ValidationContext instance named context:
const result = context.getType();
 
// result contains the getType return value

getParentType()

Returns the current parent composite type.

Signature:

getParentType(): null | undefined | GraphQLCompositeType
Returns
TypeDescription
null | undefined | GraphQLCompositeTypeThe current parent composite type, if known.
Example
// Given a ValidationContext instance named context:
const result = context.getParentType();
 
// result contains the getParentType return value

getInputType()

Returns the current input type at this point in traversal.

Signature:

getInputType(): null | undefined | GraphQLInputType
Returns
TypeDescription
null | undefined | GraphQLInputTypeThe current input type, if known.
Example
// Given a ValidationContext instance named context:
const result = context.getInputType();
 
// result contains the getInputType return value

getParentInputType()

Returns the parent input type for the current input position.

Signature:

getParentInputType(): null | undefined | GraphQLInputType
Returns
TypeDescription
null | undefined | GraphQLInputTypeThe parent input type, if known.
Example
// Given a ValidationContext instance named context:
const result = context.getParentInputType();
 
// result contains the getParentInputType return value

getFieldDef()

Returns the current field definition.

Signature:

getFieldDef(): null | undefined | GraphQLField<unknown, unknown>
Returns
TypeDescription
null | undefined | GraphQLField<unknown, unknown>The current field definition, if known.
Example
// Given a ValidationContext instance named context:
const result = context.getFieldDef();
 
// result contains the getFieldDef return value

getDirective()

Returns the current directive definition.

Signature:

getDirective(): null | undefined | GraphQLDirective
Returns
TypeDescription
null | undefined | GraphQLDirectiveThe current directive definition, if known.
Example
// Given a ValidationContext instance named context:
const result = context.getDirective();
 
// result contains the getDirective return value

getArgument()

Returns the current argument definition.

Signature:

getArgument(): null | undefined | GraphQLArgument
Returns
TypeDescription
null | undefined | GraphQLArgumentThe current argument definition, if known.
Example
// Given a ValidationContext instance named context:
const result = context.getArgument();
 
// result contains the getArgument return value

getEnumValue()

Returns the current enum value definition.

Signature:

getEnumValue(): null | undefined | GraphQLEnumValue
Returns
TypeDescription
null | undefined | GraphQLEnumValueThe current enum value definition, if known.
Example
// Given a ValidationContext instance named context:
const result = context.getEnumValue();
 
// result contains the getEnumValue return value

Types

ValidationRule

Type alias. A function that creates an AST visitor for validating a GraphQL document.

type ValidationRule = (context: ValidationContext): ASTVisitor;