Expand description
AST types for representing parsed GraphQL documents.
This AST structure provides a zero-copy AST for GraphQL documents. All node
types are parameterized over a 'src lifetime that borrows strings
(whenever possible) from the source text via Cow<'src, str>.
Each AST node has two conceptual layers:
-
Semantic layer (always present): Typed structs with names, values, directives, and all GraphQL semantics.
-
Syntax layer (optional): Each node has an
Option<XyzSyntax<'src>>field that, when populated, contains keyword/punctuation tokens with their trivia (whitespace, comments, commas). This enables lossless source reconstruction for formatter and IDE tooling.
§Example
use libgraphql_parser::ast;
use libgraphql_parser::GraphQLParser;
// Parse a string into an AST
let parser = GraphQLParser::new("type Query { hello: String }");
let result = parser.parse_schema_document();
// Extract the `Query` type definition
let doc: &ast::Document<'_> = result.valid().unwrap().0;
let query_def = match &doc.definitions[0] {
ast::Definition::TypeDefinition(
ast::TypeDefinition::Object(obj),
) => obj,
_ => panic!("expected an object type definition"),
};
// Count the number of fields on the `Query` object
let num_query_fields = query_def.fields.len();
println!("The `Query` object has {num_query_fields} fields.");Structs§
- Argument
- A single argument in a field selection or directive annotation.
- Argument
Syntax - Syntax detail for an
Argument. - Boolean
Value - A GraphQL boolean value (
trueorfalse). - Boolean
Value Syntax - Syntax detail for a
BooleanValue. - Delimiter
Pair - A matched pair of delimiter tokens (parentheses, brackets, or braces). Bundled into one struct so that an open delimiter without a matching close is unrepresentable.
- Directive
Annotation - A directive annotation applied to a definition or field
(e.g.
@deprecated(reason: "Use newField")). - Directive
Annotation Syntax - Syntax detail for a
DirectiveAnnotation. - Directive
Definition - A directive definition.
- Directive
Definition Syntax - Syntax detail for a
DirectiveDefinition. - Directive
Location - A directive location with its own span (unlike
graphql_parserwhich uses a plain enum). - Directive
Location Syntax - Syntax detail for a
DirectiveLocation. - Document
- Root AST node for any GraphQL document.
- Document
Syntax - Syntax detail for a
Document. - Enum
Type Definition - An enum type definition.
- Enum
Type Definition Syntax - Syntax detail for an
EnumTypeDefinition. - Enum
Type Extension - An enum type extension.
- Enum
Type Extension Syntax - Syntax detail for an
EnumTypeExtension. - Enum
Value - A GraphQL enum value (an unquoted name that is not
true,false, ornull). - Enum
Value Definition - An enum value definition within an enum type.
- Enum
Value Syntax - Syntax detail for an
EnumValue(the enum value literal, not the enum value definition). - Field
Definition - A field definition within an object type or interface type.
- Field
Definition Syntax - Syntax detail for a
FieldDefinition. - Field
Selection - A field selection within a selection set, optionally aliased, with arguments, directives, and a nested selection set.
- Field
Selection Syntax - Syntax detail for a
FieldSelection. - Float
Value - A GraphQL float value.
- Float
Value Syntax - Syntax detail for a
FloatValue. - Fragment
Definition - A named fragment definition.
- Fragment
Definition Syntax - Syntax detail for a
FragmentDefinition. - Fragment
Spread - A named fragment spread (
...FragmentName). - Fragment
Spread Syntax - Syntax detail for a
FragmentSpread. - Inline
Fragment - An inline fragment (
... on Type { ... }or... { ... }). - Inline
Fragment Syntax - Syntax detail for an
InlineFragment. - Input
Object Type Definition - An input object type definition.
- Input
Object Type Definition Syntax - Syntax detail for an
InputObjectTypeDefinition. - Input
Object Type Extension - An input object type extension.
- Input
Object Type Extension Syntax - Syntax detail for an
InputObjectTypeExtension. - Input
Value Definition - An input value definition, used for field arguments and input object fields.
- Input
Value Definition Syntax - Syntax detail for an
InputValueDefinition. - IntValue
- A GraphQL integer value.
- IntValue
Syntax - Syntax detail for an
IntValue. - Interface
Type Definition - An interface type definition.
- Interface
Type Definition Syntax - Syntax detail for an
InterfaceTypeDefinition. - Interface
Type Extension - An interface type extension.
- Interface
Type Extension Syntax - Syntax detail for an
InterfaceTypeExtension. - List
Type Annotation - A list type reference (e.g.
[String],[String!]!). - List
Type Annotation Syntax - Syntax detail for a
ListTypeAnnotation. - List
Value - A GraphQL list value (e.g.,
[1, 2, 3]). - List
Value Syntax - Syntax detail for a
ListValue. - Name
- A GraphQL name (identifier).
- Name
Syntax - Syntax detail for a
Name. - Named
Type Annotation - A named type reference (e.g.
String,String!). - Null
Value - A GraphQL null literal.
- Null
Value Syntax - Syntax detail for a
NullValue. - Object
Field - A single field within a GraphQL input object value.
- Object
Field Syntax - Syntax detail for an
ObjectField. - Object
Type Definition - An object type definition.
- Object
Type Definition Syntax - Syntax detail for an
ObjectTypeDefinition. - Object
Type Extension - An object type extension.
- Object
Type Extension Syntax - Syntax detail for an
ObjectTypeExtension. - Object
Value - A GraphQL input object value (e.g.,
{x: 1, y: 2}). - Object
Value Syntax - Syntax detail for an
ObjectValue. - Operation
Definition - An operation definition (query, mutation, or subscription).
- Operation
Definition Syntax - Syntax detail for an
OperationDefinition. - Root
Operation Type Definition - A root operation type definition within a schema
definition (e.g.
query: Query). - Root
Operation Type Definition Syntax - Syntax detail for a
RootOperationTypeDefinition. - Scalar
Type Definition - A scalar type definition (e.g.
scalar DateTime). - Scalar
Type Definition Syntax - Syntax detail for a
ScalarTypeDefinition. - Scalar
Type Extension - A scalar type extension.
- Scalar
Type Extension Syntax - Syntax detail for a
ScalarTypeExtension. - Schema
Definition - A GraphQL schema definition.
- Schema
Definition Syntax - Syntax detail for a
SchemaDefinition. - Schema
Extension - A schema extension.
- Schema
Extension Syntax - Syntax detail for a
SchemaExtension. - Selection
Set - A selection set — the set of fields and fragments
selected within braces
{ ... }. - Selection
SetSyntax - Syntax detail for a
SelectionSet. - String
Value - A GraphQL string value.
- String
Value Syntax - Syntax detail for a
StringValue. - Type
Condition - A type condition (e.g.,
on User) used in fragment definitions and inline fragments. - Type
Condition Syntax - Syntax detail for a
TypeCondition. - Union
Type Definition - A union type definition.
- Union
Type Definition Syntax - Syntax detail for a
UnionTypeDefinition. - Union
Type Extension - A union type extension.
- Union
Type Extension Syntax - Syntax detail for a
UnionTypeExtension. - Variable
Definition - A variable definition within an operation’s
variable list (e.g.
$id: ID! = "default"). - Variable
Definition Syntax - Syntax detail for a
VariableDefinition. - Variable
Reference - A variable reference in a GraphQL value position
(e.g.,
$id). - Variable
Reference Syntax - Syntax detail for a
VariableReference.
Enums§
- Definition
- A top-level definition in a GraphQL document.
- Definition
Kind - The kind of definition found in a GraphQL document.
- Directive
Location Kind - The kind of location where a directive may be applied.
- Document
Kind - The kind of GraphQL document being parsed.
- Nullability
- The nullability of a type reference.
- Operation
Kind - The kind of a GraphQL operation.
- Selection
- A single selection within a selection set.
- Type
Annotation - A GraphQL type reference (type annotation).
- Type
Definition - A type definition in a GraphQL schema.
- Type
Extension - A type extension in a GraphQL schema.
- Value
- A GraphQL input value.
Traits§
- AstNode
- Trait implemented by all AST node types. Provides source reconstruction and span access methods.