py_gql.lang.visitor#

Visitors provide abstractions for traversing and transforming a GraphQL AST.

exception py_gql.lang.visitor.SkipNode(message='')[source]#

Bases: py_gql.exc.GraphQLError

Raise this to short-circuit traversal and ignore the node and all its children.

class py_gql.lang.visitor.ASTVisitor[source]#

Bases: object

Base visitor class used to build complex AST tarversal and trnsform behaviours.

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

Optional[~N]

leave(node)[source]#

Implement this if you need behaviour to run after a node’s children have been visited.

This is called with the corresponding value returned by enter(). In case you modify the node, this will be called on the modified node. This doesn’t run if enter() returned None or raised SkipNode.

Return type

None

visit(node)[source]#

Apply visitor’s behaviour to a given node.

Warning

Transformations are applied inline. If you rely on node identity in your tooling, you should use copy.deepcopy or analogous before calling this.

Warning

In general you should not override this method as this is where tarversal of a node’s children and orchestration around enter() and leave() is encoded.

Return type

Optional[~N]

class py_gql.lang.visitor.DispatchingVisitor[source]#

Bases: py_gql.lang.visitor.ASTVisitor

Base class for specialised visitors.

You should subclass this and implement methods named enter_* and leave_* where * represents the node class to be handled. For instance to process py_gql.lang.ast.FloatValue nodes, implement enter_float_value.

Default behaviour is noop for all node types.

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

Optional[~N]

leave(node)[source]#

Implement this if you need behaviour to run after a node’s children have been visited.

This is called with the corresponding value returned by enter(). In case you modify the node, this will be called on the modified node. This doesn’t run if enter() returned None or raised SkipNode.

Return type

None

class py_gql.lang.visitor.ParallelVisitor(*visitors)[source]#

Bases: py_gql.lang.visitor.ASTVisitor

Abstraction to run multiple visitor instances as one.

  • All visitors are run in the order they are defined.

  • raising SkipNode in one of them will prevent any later visitor to run.

Parameters

*visitors – List of visitors to run.

visitors#

Children visitors.

Type

List[Visitor]

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

leave(node)[source]#

Implement this if you need behaviour to run after a node’s children have been visited.

This is called with the corresponding value returned by enter(). In case you modify the node, this will be called on the modified node. This doesn’t run if enter() returned None or raised SkipNode.

Return type

None

py_gql.lang.visitor.Visitor#

alias of py_gql.lang.visitor.ASTVisitor