Skip to main content

oak_graphql/ast/
mod.rs

1#![doc = include_str!("readme.md")]
2use core::range::Range;
3
4type SourceSpan = Range<usize>;
5
6/// GraphQL root node.
7#[derive(Debug, Clone, PartialEq)]
8pub struct GraphQLRoot {
9    /// The document node.
10    pub document: Document,
11}
12
13impl GraphQLRoot {
14    /// Creates a new GraphQL root.
15    pub fn new(document: Document) -> Self {
16        Self { document }
17    }
18}
19
20/// GraphQL document.
21#[derive(Debug, Clone, PartialEq)]
22pub struct Document {
23    /// List of definitions.
24    pub definitions: Vec<Definition>,
25    /// Source span.
26    pub span: SourceSpan,
27}
28
29impl Document {
30    /// Creates a new document with the given span.
31    pub fn new(span: SourceSpan) -> Self {
32        Self { definitions: Vec::new(), span }
33    }
34
35    /// Adds a definition to the document.
36    pub fn with_definition(mut self, definition: Definition) -> Self {
37        self.definitions.push(definition);
38        self
39    }
40}
41
42/// GraphQL definition.
43#[derive(Debug, Clone, PartialEq)]
44pub enum Definition {
45    /// An operation definition.
46    Operation(OperationDefinition),
47    /// A fragment definition.
48    Fragment(FragmentDefinition),
49    /// A schema definition.
50    Schema(SchemaDefinition),
51    /// A type definition.
52    Type(TypeDefinition),
53}
54
55/// GraphQL operation definition.
56#[derive(Debug, Clone, PartialEq)]
57pub struct OperationDefinition {
58    /// The type of operation.
59    pub operation_type: OperationType,
60    /// Optional name of the operation.
61    pub name: Option<String>,
62    /// Source span.
63    pub span: SourceSpan,
64}
65
66impl OperationDefinition {
67    /// Creates a new operation definition.
68    pub fn new(operation_type: OperationType, span: SourceSpan) -> Self {
69        Self { operation_type, name: None, span }
70    }
71
72    /// Sets the name of the operation.
73    pub fn with_name(mut self, name: impl Into<String>) -> Self {
74        self.name = Some(name.into());
75        self
76    }
77}
78
79/// GraphQL operation type.
80#[derive(Debug, Clone, PartialEq)]
81pub enum OperationType {
82    /// A query operation.
83    Query,
84    /// A mutation operation.
85    Mutation,
86    /// A subscription operation.
87    Subscription,
88}
89
90/// GraphQL fragment definition.
91#[derive(Debug, Clone, PartialEq)]
92pub struct FragmentDefinition {
93    /// Name of the fragment.
94    pub name: String,
95    /// Source span.
96    pub span: SourceSpan,
97}
98
99impl FragmentDefinition {
100    /// Creates a new fragment definition.
101    pub fn new(name: impl Into<String>, span: SourceSpan) -> Self {
102        Self { name: name.into(), span }
103    }
104}
105
106/// GraphQL schema definition.
107#[derive(Debug, Clone, PartialEq)]
108pub struct SchemaDefinition {
109    /// Source span.
110    pub span: SourceSpan,
111}
112
113impl SchemaDefinition {
114    /// Creates a new schema definition.
115    pub fn new(span: SourceSpan) -> Self {
116        Self { span }
117    }
118}
119
120/// GraphQL type definition.
121#[derive(Debug, Clone, PartialEq)]
122pub struct TypeDefinition {
123    /// Name of the type.
124    pub name: String,
125    /// Source span.
126    pub span: SourceSpan,
127}
128
129impl TypeDefinition {
130    /// Creates a new type definition.
131    pub fn new(name: impl Into<String>, span: SourceSpan) -> Self {
132        Self { name: name.into(), span }
133    }
134}