Expand description
Abstract Syntax Tree for GraphQL documents.
An AST Document
is more permissive but lower-level than Schema
or ExecutableDocument
.
This AST aims to faithfully represent documents that conform to the GraphQL syntactic grammar, except that ignored tokens such as whitespace are not preserved. These documents may or may not be valid.
Parsing an input that does not conform to the grammar results in parse errors together with a partial AST.
§Parsing
Start with Document::parse
, or Parser
to change the parser configuration.
§Structural sharing and mutation
Nodes inside documents are wrapped in Node
, a reference-counted smart pointer.
This allows sharing nodes between documents without cloning entire subtrees.
To modify a node, the make_mut
method provides copy-on-write semantics.
§Serialization
Document
and its node types implement Display
and ToString
by serializing to GraphQL syntax with a default configuration.
serialize
methods return a builder
that has chaining methods for setting serialization configuration,
and also implements Display
and ToString
.
§Example
use apollo_compiler::{ast, name};
let source = "{field}";
let mut doc = ast::Document::parse(source, "example.graphql").unwrap();
for def in &mut doc.definitions {
if let ast::Definition::OperationDefinition(op) = def {
// `op` has type `&mut Node<ast::OperationDefinition>`
// `Node` implements `Deref` but not `DeferMut`
// `make_mut()` clones if necessary and returns `&mut ast::OperationDefinition`
op.make_mut().directives.push(ast::Directive::new(name!(dir)));
}
}
assert_eq!(doc.serialize().no_indent().to_string(), "query @dir { field }")
Structs§
- Argument
- AST for an Argument
of a
Field
selection orDirective
application. - Directive
- AST for a Directive application.
- Directive
Definition - Type system AST for a
directive @foo
DirectiveDefinition. - Directive
List - AST for the list of Directives applied to some context.
- Document
- AST for a GraphQL Document that can contain executable definitions, type system (schema) definitions, or both.
- Enum
Type Definition - Type system AST for an
enum FooE
EnumTypeDefinition. - Enum
Type Extension - Type system AST for an
extend enum FooE
EnumTypeExtension. - Enum
Value Definition - Type system AST for an EnumValueDefinition in an enum type definition or extension.
- Field
- Executable AST for a Field selection in a selection set.
- Field
Definition - Type system AST for a FieldDefinition in an object type or interface type defintion or extension.
- Float
Overflow Error - Error type of
IntValue::try_to_f64
anFloatValue::try_to_f64
for conversions that overflowf64
and would be “rounded” to infinity. - Float
Value - An FloatValue, represented as a string in order not to lose range or precision.
- Fragment
Definition - Executable AST for a FragmentDefinition.
- Fragment
Spread - Executable AST for a FragmentSpread selection in a selection set.
- Inline
Fragment - Executable AST for an InlineFragment selection in a selection set.
- Input
Object Type Definition - Type system AST for an
input FooIn
InputObjectTypeDefinition. - Input
Object Type Extension - Type system AST for an
extend input FooIn
InputObjectTypeExtension. - Input
Value Definition - Type system AST for an InputValueDefinition, a input type field definition or an argument definition.
- IntValue
- An IntValue, represented as a string in order not to lose range or precision.
- Interface
Type Definition - Type system AST for an
interface FooI
InterfaceTypeDefinition. - Interface
Type Extension - Type system AST for an
extend interface FooI
InterfaceTypeExtension. - Object
Type Definition - Type system AST for a
type FooO
ObjectTypeDefinition. - Object
Type Extension - Type system AST for an
extend type FooO
ObjectTypeExtension. - Operation
Definition - Executable AST for an OperationDefinition.
- Scalar
Type Definition - Type system AST for a
scalar FooS
ScalarTypeDefinition. - Scalar
Type Extension - Type system AST for an
extend scalar FooS
ScalarTypeExtension. - Schema
Definition - Type system AST for a
schema
SchemaDefinition. - Schema
Extension - Type system AST for an
extend schema
SchemaExtension. - Serialize
- Builder pattern for GraphQL serialization configuration.
Implements
Display
andToString
. - Union
Type Definition - Type system AST for a
union FooU
UnionTypeDefinition. - Union
Type Extension - Type system AST for an
extend union FooU
UnionTypeExtension. - Variable
Definition - Executable AST for a VariableDefinition
in an
OperationDefinition
.
Enums§
- Argument
ByName Error - Error type of
Directive::argument_by_name
andField::argument_by_name
- Definition
- AST for a top-level Definition of any kind: executable, type system, or type system extension.
- Directive
Location - AST for a DirectiveLocation
of a
DirectiveDefinition
. - Operation
Type - AST for the OperationType
of an
OperationDefinition
orRootOperationDefinition
. - Selection
- Executable AST for a Selection in a selection set.
- Type
- Type system AST for a reference to a GraphQL Type
- Value
- Executable AST for a literal GraphQL Value.