Language

GraphQL Language

The graphql.language package is responsible for parsing and operating on the GraphQL language.

AST

class graphql.language.Location

AST Location

Contains a range of UTF-8 character offsets and token references that identify the region of the source from which the AST derived.

class graphql.language.Node(**kwargs)

AST nodes

Each kind of AST node has its own class:

class graphql.language.ArgumentNode(**kwargs)
class graphql.language.BooleanValueNode(**kwargs)
class graphql.language.DefinitionNode(**kwargs)
class graphql.language.DirectiveDefinitionNode(**kwargs)
class graphql.language.DirectiveNode(**kwargs)
class graphql.language.DocumentNode(**kwargs)
class graphql.language.EnumTypeDefinitionNode(**kwargs)
class graphql.language.EnumTypeExtensionNode(**kwargs)
class graphql.language.EnumValueDefinitionNode(**kwargs)
class graphql.language.EnumValueNode(**kwargs)
class graphql.language.ExecutableDefinitionNode(**kwargs)
class graphql.language.FieldDefinitionNode(**kwargs)
class graphql.language.FieldNode(**kwargs)
class graphql.language.FloatValueNode(**kwargs)
class graphql.language.FragmentDefinitionNode(**kwargs)
class graphql.language.FragmentSpreadNode(**kwargs)
class graphql.language.InlineFragmentNode(**kwargs)
class graphql.language.InputObjectTypeDefinitionNode(**kwargs)
class graphql.language.InputObjectTypeExtensionNode(**kwargs)
class graphql.language.InputValueDefinitionNode(**kwargs)
class graphql.language.IntValueNode(**kwargs)
class graphql.language.InterfaceTypeDefinitionNode(**kwargs)
class graphql.language.InterfaceTypeExtensionNode(**kwargs)
class graphql.language.ListTypeNode(**kwargs)
class graphql.language.ListValueNode(**kwargs)
class graphql.language.NameNode(**kwargs)
class graphql.language.NamedTypeNode(**kwargs)
class graphql.language.NonNullTypeNode(**kwargs)
class graphql.language.NullValueNode(**kwargs)
class graphql.language.ObjectFieldNode(**kwargs)
class graphql.language.ObjectTypeDefinitionNode(**kwargs)
class graphql.language.ObjectTypeExtensionNode(**kwargs)
class graphql.language.ObjectValueNode(**kwargs)
class graphql.language.OperationDefinitionNode(**kwargs)
class graphql.language.OperationType

An enumeration.

class graphql.language.OperationTypeDefinitionNode(**kwargs)
class graphql.language.ScalarTypeDefinitionNode(**kwargs)
class graphql.language.ScalarTypeExtensionNode(**kwargs)
class graphql.language.SchemaDefinitionNode(**kwargs)
class graphql.language.SchemaExtensionNode(**kwargs)
class graphql.language.SelectionNode(**kwargs)
class graphql.language.SelectionSetNode(**kwargs)
class graphql.language.StringValueNode(**kwargs)
class graphql.language.TypeDefinitionNode(**kwargs)
class graphql.language.TypeExtensionNode(**kwargs)
class graphql.language.TypeNode(**kwargs)
class graphql.language.TypeSystemDefinitionNode(**kwargs)
graphql.language.TypeSystemExtensionNode
class graphql.language.UnionTypeDefinitionNode(**kwargs)
class graphql.language.UnionTypeExtensionNode(**kwargs)
class graphql.language.ValueNode(**kwargs)
class graphql.language.VariableDefinitionNode(**kwargs)
class graphql.language.VariableNode(**kwargs)

Lexer

class graphql.language.Lexer(source: graphql.language.source.Source)

GraphQL Lexer

A Lexer is a stateful stream generator in that every time it is advanced, it returns the next token in the Source. Assuming the source lexes, the final Token emitted by the lexer will be of kind EOF, after which the lexer will repeatedly return the same EOF token whenever called.

class graphql.language.TokenKind

The different kinds of tokens that the lexer emits

class graphql.language.Token(kind: graphql.language.token_kind.TokenKind, start: int, end: int, line: int, column: int, prev: Optional[graphql.language.ast.Token] = None, value: str = None)

Location

graphql.language.get_location(source: Source, position: int) → graphql.language.location.SourceLocation

Get the line and column for a character position in the source.

Takes a Source and a UTF-8 character offset, and returns the corresponding line and column as a SourceLocation.

class graphql.language.SourceLocation

Represents a location in a Source.

graphql.language.print_location(location: graphql.language.ast.Location) → str

Render a helpful description of the location in the GraphQL Source document.

Parser

graphql.language.parse(source: Union[graphql.language.source.Source, str], no_location=False, experimental_fragment_variables=False) → graphql.language.ast.DocumentNode

Given a GraphQL source, parse it into a Document.

Throws GraphQLError if a syntax error is encountered.

By default, the parser creates AST nodes that know the location in the source that they correspond to. The no_location option disables that behavior for performance or testing.

Experimental features:

If experimental_fragment_variables is set to True, the parser will understand and parse variable definitions contained in a fragment definition. They’ll be represented in the variable_definitions field of the FragmentDefinitionNode.

The syntax is identical to normal, query-defined variables. For example:

fragment A($var: Boolean = false) on T  {
  ...
}
graphql.language.parse_type(source: Union[graphql.language.source.Source, str], no_location=False, experimental_fragment_variables=False) → graphql.language.ast.TypeNode

Parse the AST for a given string containing a GraphQL Type.

Throws GraphQLError if a syntax error is encountered.

This is useful within tools that operate upon GraphQL Types directly and in isolation of complete GraphQL documents.

Consider providing the results to the utility function: type_from_ast().

graphql.language.parse_value(source: Union[graphql.language.source.Source, str], no_location=False, experimental_fragment_variables=False) → graphql.language.ast.ValueNode

Parse the AST for a given string containing a GraphQL value.

Throws GraphQLError if a syntax error is encountered.

This is useful within tools that operate upon GraphQL Values directly and in isolation of complete GraphQL documents.

Consider providing the results to the utility function: value_from_ast().

Source

class graphql.language.Source(body: str, name: str = None, location_offset: graphql.language.location.SourceLocation = None)

A representation of source input to GraphQL.

graphql.language.print_source_location(source: graphql.language.source.Source, source_location: graphql.language.location.SourceLocation) → str

Render a helpful description of the location in the GraphQL Source document.

Visitor

graphql.language.visit(root: graphql.language.ast.Node, visitor: graphql.language.visitor.Visitor, visitor_keys=None) → Any

Visit each node in an AST.

visit() will walk through an AST using a depth first traversal, calling the visitor’s enter methods at each node in the traversal, and calling the leave methods after visiting that node and all of its child nodes.

By returning different values from the enter and leave methods, the behavior of the visitor can be altered, including skipping over a sub-tree of the AST (by returning False), editing the AST by returning a value or None to remove the value, or to stop the whole traversal by returning BREAK.

When using visit() to edit an AST, the original AST will not be modified, and a new version of the AST with the changes applied will be returned from the visit function.

To customize the node attributes to be used for traversal, you can provide a dictionary visitor_keys mapping node kinds to node attributes.

class graphql.language.Visitor

Visitor that walks through an AST.

Visitors can define two generic methods “enter” and “leave”. The former will be called when a node is entered in the traversal, the latter is called after visiting the node and its child nodes. These methods have the following signature:

def enter(self, node, key, parent, path, ancestors):
    # The return value has the following meaning:
    # IDLE (None): no action
    # SKIP: skip visiting this node
    # BREAK: stop visiting altogether
    # REMOVE: delete this node
    # any other value: replace this node with the returned value
    return

def enter(self, node, key, parent, path, ancestors):
    # The return value has the following meaning:
    # IDLE (None) or SKIP: no action
    # BREAK: stop visiting altogether
    # REMOVE: delete this node
    # any other value: replace this node with the returned value
    return

The parameters have the following meaning:

Parameters:
  • node – The current node being visiting.
  • key – The index or key to this node from the parent node or Array.
  • parent – the parent immediately above this node, which may be an Array.
  • path – The key path to get to this node from the root node.
  • ancestors – All nodes and Arrays visited before reaching parent of this node. These correspond to array indices in path. Note: ancestors includes arrays which contain the parent of visited node.

You can also define node kind specific methods by suffixing them with an underscore followed by the kind of the node to be visited. For instance, to visit field nodes, you would defined the methods enter_field() and/or leave_field(), with the same signature as above. If no kind specific method has been defined for a given node, the generic method is called.

class graphql.language.ParallelVisitor(visitors: Sequence[graphql.language.visitor.Visitor])

A Visitor which delegates to many visitors to run in parallel.

Each visitor will be visited for each node before moving on.

If a prior visitor edits a node, no following visitors will see that node.

class graphql.language.TypeInfoVisitor(type_info: TypeInfo, visitor: graphql.language.visitor.Visitor)

A visitor which maintains a provided TypeInfo.

The module also exports the following special symbols which can be used as return values in the Visitor methods to signal particular actions:

graphql.language.BREAK = True

This return value signals that no further nodes shall be visited.

graphql.language.SKIP = False

This return value signals that the current node shall be skipped.

graphql.language.REMOVE = Ellipsis

This return value signals that the current node shall be deleted.

graphql.language.IDLE = None

This return value signals that no additional action shall take place.