Skip to main content

oak_graphql/ast/
mod.rs

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