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()
.
-
py_gql.validation.
validate_ast
(schema, document, *, validators=None, variables=None)[source]# Check that an ast is a valid GraphQL query document.
This runs the parse tree through a list of validation callables given a
Schema
instance.Warning
This assumes the ast is a document generated by
py_gql.lang.parse()
(as opposed to manually constructed) and will most likely break unexpectedly if that’s not the case.Parameters: - schema (
Schema
) – Schema to validate against (for known types, directives, etc.). - document (
Document
) – The parse tree root. - validators (
Optional
[Sequence
[Callable
[[Schema
,Document
,Optional
[Dict
[str
,Any
]]],Iterable
[ValidationError
]]]]) – List of validator callables to use. Defaults to the rules defined in the specification. - variables (
Optional
[Dict
[str
,Any
]]) – Raw, JSON decoded variables parsed from the request.
Return type: ValidationResult
Returns: Validation result wrapping any validation error that occurred.
- schema (
-
py_gql.validation.
default_validator
(schema, document, variables=None, *, validators=(<class 'py_gql.validation.rules.ExecutableDefinitionsChecker'>, <class 'py_gql.validation.rules.UniqueOperationNameChecker'>, <class 'py_gql.validation.rules.LoneAnonymousOperationChecker'>, <class 'py_gql.validation.rules.SingleFieldSubscriptionsChecker'>, <class 'py_gql.validation.rules.KnownTypeNamesChecker'>, <class 'py_gql.validation.rules.FragmentsOnCompositeTypesChecker'>, <class 'py_gql.validation.rules.VariablesAreInputTypesChecker'>, <class 'py_gql.validation.rules.ScalarLeafsChecker'>, <class 'py_gql.validation.rules.FieldsOnCorrectTypeChecker'>, <class 'py_gql.validation.rules.UniqueFragmentNamesChecker'>, <class 'py_gql.validation.rules.KnownFragmentNamesChecker'>, <class 'py_gql.validation.rules.NoUnusedFragmentsChecker'>, <class 'py_gql.validation.rules.PossibleFragmentSpreadsChecker'>, <class 'py_gql.validation.rules.NoFragmentCyclesChecker'>, <class 'py_gql.validation.rules.UniqueVariableNamesChecker'>, <class 'py_gql.validation.rules.NoUndefinedVariablesChecker'>, <class 'py_gql.validation.rules.NoUnusedVariablesChecker'>, <class 'py_gql.validation.rules.KnownDirectivesChecker'>, <class 'py_gql.validation.rules.UniqueDirectivesPerLocationChecker'>, <class 'py_gql.validation.rules.KnownArgumentNamesChecker'>, <class 'py_gql.validation.rules.UniqueArgumentNamesChecker'>, <class 'py_gql.validation.rules.values_of_correct_type.ValuesOfCorrectTypeChecker'>, <class 'py_gql.validation.rules.ProvidedRequiredArgumentsChecker'>, <class 'py_gql.validation.rules.VariablesInAllowedPositionChecker'>, <class 'py_gql.validation.rules.overlapping_fields_can_be_merged.OverlappingFieldsCanBeMergedChecker'>, <class 'py_gql.validation.rules.UniqueInputFieldNamesChecker'>))[source]# Validate a GraphQL document using a collection of
ValidationVisitor
.This is the default validator implementation and uses a chain of
ValidationVisitor
classes, collecting all errors by passing the document through all visitors in order.The ordering allows visitors to rely on previous visitors having filtered out invalid nodes.
In order to use this with
validate_ast()
and custom visitor classes a lambda or partial should be created.Return type: Iterable
[ValidationError
]
-
class
py_gql.validation.
ValidationResult
(errors=None)[source]# Wrap a collection of
ValidationError
.Instances are iterable and falsy when they contain at least one validation error.
-
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 andpy_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 byvalidate()
.
-
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 originated from.
Return type: None
-
enter
(node)[source]# Process an AST node.
Implement this for the main visiting behavior (i.e. before a node’s children have been visited).
Return
None
to delete the node from it’s parent context or raiseSkipNode
to prevent any further processing (children do not get visited and leave doesn’t get called for that node).Return type: ~N
- schema (
-
validation.
SPECIFIED_RULES
# This is the list of
ValidationVisitor
frompy_gql.validation.rules
encoding all the validation rules defined in this section of the Spec.