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
Fieldselection orDirectiveapplication. - Directive
- AST for a Directive application.
- Directive
Definition - Type system AST for a
directive @fooDirectiveDefinition. - 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 FooEEnumTypeDefinition. - Enum
Type Extension - Type system AST for an
extend enum FooEEnumTypeExtension. - 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_f64anFloatValue::try_to_f64for conversions that overflowf64and 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 FooInInputObjectTypeDefinition. - Input
Object Type Extension - Type system AST for an
extend input FooInInputObjectTypeExtension. - 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 FooIInterfaceTypeDefinition. - Interface
Type Extension - Type system AST for an
extend interface FooIInterfaceTypeExtension. - Object
Type Definition - Type system AST for a
type FooOObjectTypeDefinition. - Object
Type Extension - Type system AST for an
extend type FooOObjectTypeExtension. - Operation
Definition - Executable AST for an OperationDefinition.
- Scalar
Type Definition - Type system AST for a
scalar FooSScalarTypeDefinition. - Scalar
Type Extension - Type system AST for an
extend scalar FooSScalarTypeExtension. - Schema
Definition - Type system AST for a
schemaSchemaDefinition. - Schema
Extension - Type system AST for an
extend schemaSchemaExtension. - Serialize
- Builder pattern for GraphQL serialization configuration.
Implements
DisplayandToString. - Union
Type Definition - Type system AST for a
union FooUUnionTypeDefinition. - Union
Type Extension - Type system AST for an
extend union FooUUnionTypeExtension. - Variable
Definition - Executable AST for a VariableDefinition
in an
OperationDefinition.
Enums§
- Argument
ByName Error - Error type of
Directive::argument_by_nameandField::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
OperationDefinitionorRootOperationDefinition. - 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.