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 encoding AST traversal and transforms behaviors.

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 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]#

Cleanup after processing an AST node and its children.

Implement this if you need behavior 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 behavior 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 traversal 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 specialized 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 behavior is noop for all node types.

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 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]#

Cleanup after processing an AST node and its children.

Implement this if you need behavior 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.ChainedVisitor(*visitors)[source]#

Bases: py_gql.lang.visitor.ASTVisitor

Run multiple visitor instances in sequence.

  • All visitors are run in the order they are defined, with enter being called in order and leave in reverse order.
  • 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[ASTVisitor]
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 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]#

Cleanup after processing an AST node and its children.

Implement this if you need behavior 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