Validation

GraphQL Validation

The graphql.validation package fulfills the Validation phase of fulfilling a GraphQL result.

graphql.validation.validate(schema: graphql.type.schema.GraphQLSchema, document_ast: graphql.language.ast.DocumentNode, rules: Sequence[Type[graphql.validation.rules.ASTValidationRule]] = None, type_info: graphql.utilities.type_info.TypeInfo = None, max_errors: int = None) → List[graphql.error.graphql_error.GraphQLError]

Implements the “Validation” section of the spec.

Validation runs synchronously, returning a list of encountered errors, or an empty list if no errors were encountered and the document is valid.

A list of specific validation rules may be provided. If not provided, the default list of rules defined by the GraphQL specification will be used.

Each validation rule is a ValidationRule object which is a visitor object that holds a ValidationContext (see the language/visitor API). Visitor methods are expected to return GraphQLErrors, or lists of GraphQLErrors when invalid.

Optionally a custom TypeInfo instance may be provided. If not provided, one will be created from the provided schema.

class graphql.validation.ASTValidationContext(ast: graphql.language.ast.DocumentNode, on_error: Callable[[graphql.error.graphql_error.GraphQLError], None] = None)

Utility class providing a context for validation of an AST.

An instance of this class is passed as the context attribute to all Validators, allowing access to commonly useful contextual information from within a validation rule.

class graphql.validation.ASTValidationRule(context: graphql.validation.validation_context.ASTValidationContext)

Visitor for validation of an AST.

class graphql.validation.SDLValidationContext(ast: graphql.language.ast.DocumentNode, schema: graphql.type.schema.GraphQLSchema = None, on_error: Callable[[graphql.error.graphql_error.GraphQLError], None] = None)

Utility class providing a context for validation of an SDL AST.

An instance of this class is passed as the context attribute to all Validators, allowing access to commonly useful contextual information from within a validation rule.

class graphql.validation.SDLValidationRule(context: graphql.validation.validation_context.SDLValidationContext)

Visitor for validation of an SDL AST.

class graphql.validation.ValidationContext(schema: graphql.type.schema.GraphQLSchema, ast: graphql.language.ast.DocumentNode, type_info: graphql.utilities.type_info.TypeInfo, on_error: Callable[[graphql.error.graphql_error.GraphQLError], None] = None)

Utility class providing a context for validation using a GraphQL schema.

An instance of this class is passed as the context attribute to all Validators, allowing access to commonly useful contextual information from within a validation rule.

class graphql.validation.ValidationRule(context: graphql.validation.validation_context.ValidationContext)

Visitor for validation using a GraphQL schema.

Rules

This list includes all validation rules defined by the GraphQL spec. The order of the rules in this list has been adjusted to lead to the most clear output when encountering multiple validation errors:

graphql.validation.specified_rules = FrozenList([...])

This list includes all validation rules defined by the GraphQL spec.

The order of the rules in this list has been adjusted to lead to the most clear output when encountering multiple validation errors.

Spec Section: “Executable Definitions”

class graphql.validation.ExecutableDefinitionsRule(context: graphql.validation.validation_context.ASTValidationContext)

Executable definitions

A GraphQL document is only valid for execution if all definitions are either operation or fragment definitions.

Spec Section: “Field Selections on Objects, Interfaces, and Unions Types”

class graphql.validation.FieldsOnCorrectTypeRule(context: graphql.validation.validation_context.ValidationContext)

Fields on correct type

A GraphQL document is only valid if all fields selected are defined by the parent type, or are an allowed meta field such as __typename.

Spec Section: “Fragments on Composite Types”

class graphql.validation.FragmentsOnCompositeTypesRule(context: graphql.validation.validation_context.ValidationContext)

Fragments on composite type

Fragments use a type condition to determine if they apply, since fragments can only be spread into a composite type (object, interface, or union), the type condition must also be a composite type.

Spec Section: “Argument Names”

class graphql.validation.KnownArgumentNamesRule(context: graphql.validation.validation_context.ValidationContext)

Known argument names

A GraphQL field is only valid if all supplied arguments are defined by that field.

Spec Section: “Directives Are Defined”

class graphql.validation.KnownDirectivesRule(context: Union[graphql.validation.validation_context.ValidationContext, graphql.validation.validation_context.SDLValidationContext])

Known directives

A GraphQL document is only valid if all @directives are known by the schema and legally positioned.

Spec Section: “Fragment spread target defined”

class graphql.validation.KnownFragmentNamesRule(context: graphql.validation.validation_context.ValidationContext)

Known fragment names

A GraphQL document is only valid if all …Fragment fragment spreads refer to fragments defined in the same document.

Spec Section: “Fragment Spread Type Existence”

class graphql.validation.KnownTypeNamesRule(context: Union[graphql.validation.validation_context.ValidationContext, graphql.validation.validation_context.SDLValidationContext])

Known type names

A GraphQL document is only valid if referenced types (specifically variable definitions and fragment conditions) are defined by the type schema.

Spec Section: “Lone Anonymous Operation”

class graphql.validation.LoneAnonymousOperationRule(context: graphql.validation.validation_context.ASTValidationContext)

Lone anonymous operation

A GraphQL document is only valid if when it contains an anonymous operation (the query short-hand) that it contains only that one operation definition.

Spec Section: “Fragments must not form cycles”

class graphql.validation.NoFragmentCyclesRule(context: graphql.validation.validation_context.ASTValidationContext)

No fragment cycles

Spec Section: “All Variable Used Defined”

class graphql.validation.NoUndefinedVariablesRule(context: graphql.validation.validation_context.ValidationContext)

No undefined variables

A GraphQL operation is only valid if all variables encountered, both directly and via fragment spreads, are defined by that operation.

Spec Section: “Fragments must be used”

class graphql.validation.NoUnusedFragmentsRule(context: graphql.validation.validation_context.ASTValidationContext)

No unused fragments

A GraphQL document is only valid if all fragment definitions are spread within operations, or spread within other fragments spread within operations.

Spec Section: “All Variables Used”

class graphql.validation.NoUnusedVariablesRule(context: graphql.validation.validation_context.ValidationContext)

No unused variables

A GraphQL operation is only valid if all variables defined by an operation are used, either directly or within a spread fragment.

Spec Section: “Field Selection Merging”

class graphql.validation.OverlappingFieldsCanBeMergedRule(context: graphql.validation.validation_context.ValidationContext)

Overlapping fields can be merged

A selection set is only valid if all fields (including spreading any fragments) either correspond to distinct response names or can be merged without ambiguity.

Spec Section: “Fragment spread is possible”

class graphql.validation.PossibleFragmentSpreadsRule(context: graphql.validation.validation_context.ValidationContext)

Possible fragment spread

A fragment spread is only valid if the type condition could ever possibly be true: if there is a non-empty intersection of the possible parent types, and possible types which pass the type condition.

Spec Section: “Argument Optionality”

class graphql.validation.ProvidedRequiredArgumentsRule(context: graphql.validation.validation_context.ValidationContext)

Provided required arguments

A field or directive is only valid if all required (non-null without a default value) field arguments have been provided.

Spec Section: “Leaf Field Selections”

class graphql.validation.ScalarLeafsRule(context: graphql.validation.validation_context.ValidationContext)

Scalar leafs

A GraphQL document is valid only if all leaf fields (fields without sub selections) are of scalar or enum types.

Spec Section: “Subscriptions with Single Root Field”

class graphql.validation.SingleFieldSubscriptionsRule(context: graphql.validation.validation_context.ASTValidationContext)

Subscriptions must only include one field.

A GraphQL subscription is valid only if it contains a single root.

Spec Section: “Argument Uniqueness”

class graphql.validation.UniqueArgumentNamesRule(context: graphql.validation.validation_context.ASTValidationContext)

Unique argument names

A GraphQL field or directive is only valid if all supplied arguments are uniquely named.

Spec Section: “Directives Are Unique Per Location”

class graphql.validation.UniqueDirectivesPerLocationRule(context: Union[graphql.validation.validation_context.ValidationContext, graphql.validation.validation_context.SDLValidationContext])

Unique directive names per location

A GraphQL document is only valid if all non-repeatable directives at a given location are uniquely named.

Spec Section: “Fragment Name Uniqueness”

class graphql.validation.UniqueFragmentNamesRule(context: graphql.validation.validation_context.ASTValidationContext)

Unique fragment names

A GraphQL document is only valid if all defined fragments have unique names.

Spec Section: “Input Object Field Uniqueness”

class graphql.validation.UniqueInputFieldNamesRule(context: graphql.validation.validation_context.ASTValidationContext)

Unique input field names

A GraphQL input object value is only valid if all supplied fields are uniquely named.

Spec Section: “Operation Name Uniqueness”

class graphql.validation.UniqueOperationNamesRule(context: graphql.validation.validation_context.ASTValidationContext)

Unique operation names

A GraphQL document is only valid if all defined operations have unique names.

Spec Section: “Variable Uniqueness”

class graphql.validation.UniqueVariableNamesRule(context: graphql.validation.validation_context.ASTValidationContext)

Unique variable names

A GraphQL operation is only valid if all its variables are uniquely named.

Spec Section: “Value Type Correctness”

class graphql.validation.ValuesOfCorrectTypeRule(context: graphql.validation.validation_context.ValidationContext)

Value literals of correct type

A GraphQL document is only valid if all value literals are of the type expected at their position.

Spec Section: “Variables are Input Types”

class graphql.validation.VariablesAreInputTypesRule(context: graphql.validation.validation_context.ValidationContext)

Variables are input types

A GraphQL operation is only valid if all the variables it defines are of input types (scalar, enum, or input object).

Spec Section: “All Variable Usages Are Allowed”

class graphql.validation.VariablesInAllowedPositionRule(context: graphql.validation.validation_context.ValidationContext)

Variables passed to field arguments conform to type