Expand description
Typed Concrete Syntax Tree module to access nodes in the tree.
The nodes described here are those also described in the GraphQL grammar,
with a few exceptions. For example, for easy of querying the CST we do not
separate Definition into ExecutableDefinition and
TypeSystemDefinitionOrExtension. Instead, all possible definitions and
extensions can be accessed with Definition.
Each struct in this module has getter methods to access information that’s
part of its node. For example, as per spec a UnionTypeDefinition is defined as follows:
UnionTypeDefinition =
Description? 'union' Name Directives? UnionMemberTypes?It will then have getters for Description, union token, Name,
Directives and UnionMemberTypes. Checkout documentation for the Struct
you’re working with to find out its exact API.
§Example
This example parses a subgraph schema and looks at the various Definition Names.
use apollo_parser::{cst, Parser};
let schema = r#"
directive @tag(name: String!) repeatable on FIELD_DEFINITION
type ProductVariation {
id: ID!
}
scalar UUID @specifiedBy(url: "https://tools.ietf.org/html/rfc4122")
union SearchResult = Photo | Person
extend type Query {
allProducts: [Product]
product(id: ID!): Product
}
"#;
let parser = Parser::new(schema);
let cst = parser.parse();
assert_eq!(0, cst.errors().len());
let document = cst.document();
for definition in document.definitions() {
match definition {
cst::Definition::DirectiveDefinition(directive) => {
assert_eq!(
directive
.name()
.expect("Cannot get directive name.")
.text()
.as_ref(),
"tag"
)
}
cst::Definition::ObjectTypeDefinition(object_type) => {
assert_eq!(
object_type
.name()
.expect("Cannot get object type definition name.")
.text()
.as_ref(),
"ProductVariation"
)
}
cst::Definition::UnionTypeDefinition(union_type) => {
assert_eq!(
union_type
.name()
.expect("Cannot get union type definition name.")
.text()
.as_ref(),
"SearchResult"
)
}
cst::Definition::ScalarTypeDefinition(scalar_type) => {
assert_eq!(
scalar_type
.name()
.expect("Cannot get scalar type definition name.")
.text()
.as_ref(),
"UUID"
)
}
cst::Definition::ObjectTypeExtension(object_type) => {
assert_eq!(
object_type
.name()
.expect("Cannot get object type extension name.")
.text()
.as_ref(),
"Query"
)
}
_ => unimplemented!(),
}
}Re-exports§
pub use crate::SyntaxNode;
Structs§
- Alias
- Argument
- Arguments
- Arguments
Definition - Boolean
Value - CstChildren
- An iterator over
SyntaxNodechildren of a particular CST type. - Default
Value - Description
- Directive
- Directive
Definition - Directive
Location - Directive
Locations - Directives
- Document
- Enum
Type Definition - Enum
Type Extension - Enum
Value - Enum
Value Definition - Enum
Values Definition - Field
- Field
Definition - Fields
Definition - Float
Value - Fragment
Definition - Fragment
Name - Fragment
Spread - Implements
Interfaces - Inline
Fragment - Input
Fields Definition - Input
Object Type Definition - Input
Object Type Extension - Input
Value Definition - IntValue
- Interface
Type Definition - Interface
Type Extension - List
Type - List
Value - Name
- Named
Type - NonNull
Type - Null
Value - Object
Field - Object
Type Definition - Object
Type Extension - Object
Value - Operation
Definition - Operation
Type - Root
Operation Type Definition - Scalar
Type Definition - Scalar
Type Extension - Schema
Definition - Schema
Extension - Selection
Set - String
Value - Type
Condition - Union
Member Types - Union
Type Definition - Union
Type Extension - Variable
- Variable
Definition - Variable
Definitions
Enums§
Traits§
- CstNode
- The main trait to go from untyped
SyntaxNodeto a typed CST. The conversion itself has zero runtime cost: CST and syntax nodes have exactly the same representation: a pointer to the tree root and a pointer to the node itself. - CstToken
- Like
CstNode, but wraps tokens rather than interior nodes.
Type Aliases§
- Syntax
Node Ptr - A wrapper around
SyntaxNodePtr.