1#![doc = include_str!("readme.md")]
2use oak_core::Range;
3
4type SourceSpan = Range<usize>;
5
6#[derive(Debug, Clone, PartialEq)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9pub struct GraphQLRoot {
10 pub document: Document,
12}
13
14impl GraphQLRoot {
15 pub fn new(document: Document) -> Self {
17 Self { document }
18 }
19}
20
21#[derive(Debug, Clone, PartialEq)]
23#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
24pub struct Document {
25 pub definitions: Vec<Definition>,
27 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
29 pub span: SourceSpan,
30}
31
32impl Document {
33 pub fn new(span: SourceSpan) -> Self {
35 Self { definitions: Vec::new(), span }
36 }
37
38 pub fn with_definition(mut self, definition: Definition) -> Self {
40 self.definitions.push(definition);
41 self
42 }
43}
44
45#[derive(Debug, Clone, PartialEq)]
47#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
48pub enum Definition {
49 Operation(OperationDefinition),
51 Fragment(FragmentDefinition),
53 Schema(SchemaDefinition),
55 Type(TypeDefinition),
57}
58
59#[derive(Debug, Clone, PartialEq)]
61#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
62pub struct OperationDefinition {
63 pub operation_type: OperationType,
65 pub name: Option<String>,
67 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
69 pub span: SourceSpan,
70}
71
72impl OperationDefinition {
73 pub fn new(operation_type: OperationType, span: SourceSpan) -> Self {
75 Self { operation_type, name: None, span }
76 }
77
78 pub fn with_name(mut self, name: impl Into<String>) -> Self {
80 self.name = Some(name.into());
81 self
82 }
83}
84
85#[derive(Debug, Clone, PartialEq)]
87#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
88pub enum OperationType {
89 Query,
91 Mutation,
93 Subscription,
95}
96
97#[derive(Debug, Clone, PartialEq)]
99#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
100pub struct FragmentDefinition {
101 pub name: String,
103 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
105 pub span: SourceSpan,
106}
107
108impl FragmentDefinition {
109 pub fn new(name: impl Into<String>, span: SourceSpan) -> Self {
111 Self { name: name.into(), span }
112 }
113}
114
115#[derive(Debug, Clone, PartialEq)]
117#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
118pub struct SchemaDefinition {
119 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
121 pub span: SourceSpan,
122}
123
124impl SchemaDefinition {
125 pub fn new(span: SourceSpan) -> Self {
127 Self { span }
128 }
129}
130
131#[derive(Debug, Clone, PartialEq)]
133#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
134pub struct TypeDefinition {
135 pub name: String,
137 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
139 pub span: SourceSpan,
140}
141
142impl TypeDefinition {
143 pub fn new(name: impl Into<String>, span: SourceSpan) -> Self {
145 Self { name: name.into(), span }
146 }
147}