#![doc = include_str!("readme.md")]
use oak_core::Range;
type SourceSpan = Range<usize>;
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct GraphQLRoot {
pub document: Document,
}
impl GraphQLRoot {
pub fn new(document: Document) -> Self {
Self { document }
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Document {
pub definitions: Vec<Definition>,
#[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
pub span: SourceSpan,
}
impl Document {
pub fn new(span: SourceSpan) -> Self {
Self { definitions: Vec::new(), span }
}
pub fn with_definition(mut self, definition: Definition) -> Self {
self.definitions.push(definition);
self
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Definition {
Operation(OperationDefinition),
Fragment(FragmentDefinition),
Schema(SchemaDefinition),
Type(TypeDefinition),
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct OperationDefinition {
pub operation_type: OperationType,
pub name: Option<String>,
#[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
pub span: SourceSpan,
}
impl OperationDefinition {
pub fn new(operation_type: OperationType, span: SourceSpan) -> Self {
Self { operation_type, name: None, span }
}
pub fn with_name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum OperationType {
Query,
Mutation,
Subscription,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FragmentDefinition {
pub name: String,
#[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
pub span: SourceSpan,
}
impl FragmentDefinition {
pub fn new(name: impl Into<String>, span: SourceSpan) -> Self {
Self { name: name.into(), span }
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SchemaDefinition {
#[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
pub span: SourceSpan,
}
impl SchemaDefinition {
pub fn new(span: SourceSpan) -> Self {
Self { span }
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TypeDefinition {
pub name: String,
#[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
pub span: SourceSpan,
}
impl TypeDefinition {
pub fn new(name: impl Into<String>, span: SourceSpan) -> Self {
Self { name: name.into(), span }
}
}