py_gql.validation#

Validation of GraphQL (query) documents.

Note

This module is only concerned with validating query documents, not SDL documents which are validated when using py_gql.build_schema or py_gql.schema.Schema.validate().

class py_gql.validation.ValidationResult(errors)[source]#

Encode validation result by wrapping a collection of ValidationError. Instances are iterable and falsy when they contain at least one error.

py_gql.validation.validate_ast(schema, ast_root, validators=None)[source]#

Check that an ast is a valid GraphQL query document by running the parse tree through a list of ValidationVisitor given a Schema instance.

Warning

This assumes the ast is a valid document generated by py_gql.lang.parse() and will most likely break unexpectedly if that’s not the case.

Parameters
Return type

ValidationResult

Returns

Validation result wrapping any validatione error that occured.

class py_gql.validation.ValidationVisitor(schema, type_info)[source]#

Visitor class used for validating GraphQL documents.

Subclass this to implement custom validators. Use add_error() to register errors and py_gql.lang.visitor.SkipNode to prevent validating child nodes when parent node is invalid.

Parameters
  • schema (Schema) – Schema to validate against (for known types, directives, etc.).

  • type_info (TypeInfoVisitor) – Type information collector provided by validate().

schema#

Schema to validate against (for known types, directives, etc.).

Type

py_gql.schema.Schema

type_info#

Type information collector provided by validate().

Type

TypeInfoVisitor

errors#

Collected errors.

Type

List[ValidationError]

add_error(message, nodes=None)[source]#

Register an error

Parameters
  • message (str) – Error description

  • nodes (Optional[List[py_gql.lang.ast.Node]]) – Nodes where the error comes from

Return type

None

enter(node)[source]#

Implement this for the main visiting behaviour (i.e. before a node’s children have been visited).

Return None to delete the node from it’s parent context or raise SkipNode to prevent any further processing (children do not get visited and leave doesn’t get called for that node).

Return type

~N

class py_gql.validation.VariablesCollector(schema, type_info)[source]#

Custom validation visitor which tracks all variable definitions and usage across the document.

validation.SPECIFIED_RULES#

This is the list of ValidationVisitor from py_gql.validation.rules encoding all the validation rules defined in this section of the Spec.