gurkle_parser/query/
ast.rs

1//! Query Language Abstract Syntax Tree (AST)
2//!
3//! The types and fields here resemble official [graphql grammar] whenever it
4//! makes sense for rust.
5//!
6//! [graphql grammar]: http://facebook.github.io/graphql/October2016/#sec-Appendix-Grammar-Summary
7//!
8pub use crate::common::{Directive, Name, Number, Type, Value};
9use crate::position::Pos;
10
11/// Root of query data
12#[derive(Debug, Clone, PartialEq)]
13pub struct Document {
14    pub definitions: Vec<Definition>,
15}
16
17#[derive(Debug, Clone, PartialEq)]
18pub enum Definition {
19    Operation(OperationDefinition),
20    Fragment(FragmentDefinition),
21}
22
23#[derive(Debug, Clone, PartialEq)]
24pub struct FragmentDefinition {
25    pub position: Pos,
26    pub name: Name,
27    pub type_condition: TypeCondition,
28    pub directives: Vec<Directive>,
29    pub selection_set: SelectionSet,
30}
31
32#[derive(Debug, Clone, PartialEq)]
33pub enum OperationDefinition {
34    SelectionSet(SelectionSet),
35    Query(Query),
36    Mutation(Mutation),
37    Subscription(Subscription),
38}
39
40#[derive(Debug, Clone, PartialEq)]
41pub struct Query {
42    pub position: Pos,
43    pub name: Option<Name>,
44    pub variable_definitions: Vec<VariableDefinition>,
45    pub directives: Vec<Directive>,
46    pub selection_set: SelectionSet,
47}
48
49#[derive(Debug, Clone, PartialEq)]
50pub struct Mutation {
51    pub position: Pos,
52    pub name: Option<Name>,
53    pub variable_definitions: Vec<VariableDefinition>,
54    pub directives: Vec<Directive>,
55    pub selection_set: SelectionSet,
56}
57
58#[derive(Debug, Clone, PartialEq)]
59pub struct Subscription {
60    pub position: Pos,
61    pub name: Option<Name>,
62    pub variable_definitions: Vec<VariableDefinition>,
63    pub directives: Vec<Directive>,
64    pub selection_set: SelectionSet,
65}
66
67#[derive(Debug, Clone, PartialEq)]
68pub struct SelectionSet {
69    pub span: (Pos, Pos),
70    pub items: Vec<Selection>,
71}
72
73#[derive(Debug, Clone, PartialEq)]
74pub struct VariableDefinition {
75    pub position: Pos,
76    pub name: Name,
77    pub var_type: Type,
78    pub default_value: Option<Value>,
79}
80
81#[derive(Debug, Clone, PartialEq)]
82pub enum Selection {
83    Field(Field),
84    FragmentSpread(FragmentSpread),
85    InlineFragment(InlineFragment),
86}
87
88#[derive(Debug, Clone, PartialEq)]
89pub struct Field {
90    pub position: Pos,
91    pub alias: Option<Name>,
92    pub name: Name,
93    pub arguments: Vec<(Name, Value)>,
94    pub directives: Vec<Directive>,
95    pub selection_set: SelectionSet,
96}
97
98#[derive(Debug, Clone, PartialEq)]
99pub struct FragmentSpread {
100    pub position: Pos,
101    pub fragment_name: Name,
102    pub directives: Vec<Directive>,
103}
104
105#[derive(Debug, Clone, PartialEq)]
106pub enum TypeCondition {
107    On(Name),
108}
109
110#[derive(Debug, Clone, PartialEq)]
111pub struct InlineFragment {
112    pub position: Pos,
113    pub type_condition: Option<TypeCondition>,
114    pub directives: Vec<Directive>,
115    pub selection_set: SelectionSet,
116}