Module ast

Source
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 or Directive application.
Directive
AST for a Directive application.
DirectiveDefinition
Type system AST for a directive @foo DirectiveDefinition.
DirectiveList
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.
EnumTypeDefinition
Type system AST for an enum FooE EnumTypeDefinition.
EnumTypeExtension
Type system AST for an extend enum FooE EnumTypeExtension.
EnumValueDefinition
Type system AST for an EnumValueDefinition in an enum type definition or extension.
Field
Executable AST for a Field selection in a selection set.
FieldDefinition
Type system AST for a FieldDefinition in an object type or interface type defintion or extension.
FloatOverflowError
Error type of IntValue::try_to_f64 an FloatValue::try_to_f64 for conversions that overflow f64 and would be “rounded” to infinity.
FloatValue
An FloatValue, represented as a string in order not to lose range or precision.
FragmentDefinition
Executable AST for a FragmentDefinition.
FragmentSpread
Executable AST for a FragmentSpread selection in a selection set.
InlineFragment
Executable AST for an InlineFragment selection in a selection set.
InputObjectTypeDefinition
Type system AST for an input FooIn InputObjectTypeDefinition.
InputObjectTypeExtension
Type system AST for an extend input FooIn InputObjectTypeExtension.
InputValueDefinition
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.
InterfaceTypeDefinition
Type system AST for an interface FooI InterfaceTypeDefinition.
InterfaceTypeExtension
Type system AST for an extend interface FooI InterfaceTypeExtension.
ObjectTypeDefinition
Type system AST for a type FooO ObjectTypeDefinition.
ObjectTypeExtension
Type system AST for an extend type FooO ObjectTypeExtension.
OperationDefinition
Executable AST for an OperationDefinition.
ScalarTypeDefinition
Type system AST for a scalar FooS ScalarTypeDefinition.
ScalarTypeExtension
Type system AST for an extend scalar FooS ScalarTypeExtension.
SchemaDefinition
Type system AST for a schema SchemaDefinition.
SchemaExtension
Type system AST for an extend schema SchemaExtension.
Serialize
Builder pattern for GraphQL serialization configuration. Implements Display and ToString.
UnionTypeDefinition
Type system AST for a union FooU UnionTypeDefinition.
UnionTypeExtension
Type system AST for an extend union FooU UnionTypeExtension.
VariableDefinition
Executable AST for a VariableDefinition in an OperationDefinition.

Enums§

ArgumentByNameError
Error type of Directive::argument_by_name and Field::argument_by_name
Definition
AST for a top-level Definition of any kind: executable, type system, or type system extension.
DirectiveLocation
AST for a DirectiveLocation of a DirectiveDefinition.
OperationType
AST for the OperationType of an OperationDefinition or RootOperationDefinition.
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.

Type Aliases§

NamedType
A NamedType references by name a GraphQL type defined elsewhere.