1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
use std::fmt;
use crate::{Directive, StringValue};
/// A GraphQL service’s collective type system capabilities are referred to as
/// that service’s “schema”.
///
/// *SchemaDefinition*:
/// Description? **schema** Directives? **{** RootOperationTypeDefinition* **}**
///
/// Detailed documentation can be found in [GraphQL spec](https://spec.graphql.org/October2021/#sec-Schema).
///
/// ### Example
/// ```rust
/// use apollo_encoder::{SchemaDefinition};
/// use indoc::indoc;
///
/// let mut schema_def = SchemaDefinition::new();
/// schema_def.query("TryingToFindCatQuery".to_string());
/// schema_def.mutation("MyMutation".to_string());
/// schema_def.subscription("MySubscription".to_string());
///
/// assert_eq!(
/// schema_def.to_string(),
/// indoc! { r#"
/// schema {
/// query: TryingToFindCatQuery
/// mutation: MyMutation
/// subscription: MySubscription
/// }
/// "#}
/// );
/// ```
#[derive(Debug, Clone)]
pub struct SchemaDefinition {
// Description may be a String.
description: Option<StringValue>,
// The vector of fields in a schema to represent root operation type
// definition.
query: Option<String>,
mutation: Option<String>,
subscription: Option<String>,
directives: Vec<Directive>,
// Extend a schema
extend: bool,
}
impl SchemaDefinition {
/// Create a new instance of SchemaDef.
pub fn new() -> Self {
Self {
description: None,
query: None,
mutation: None,
subscription: None,
directives: Vec::new(),
extend: false,
}
}
/// Set the SchemaDef's description.
pub fn description(&mut self, description: String) {
self.description = Some(StringValue::Top {
source: description,
});
}
/// Add a directive.
pub fn directive(&mut self, directive: Directive) {
self.directives.push(directive);
}
/// Set as an extension
pub fn extend(&mut self) {
self.extend = true;
}
/// Set the schema def's query type.
pub fn query(&mut self, query: String) {
self.query = Some(query);
}
/// Set the schema def's mutation type.
pub fn mutation(&mut self, mutation: String) {
self.mutation = Some(mutation);
}
/// Set the schema def's subscription type.
pub fn subscription(&mut self, subscription: String) {
self.subscription = Some(subscription);
}
}
impl Default for SchemaDefinition {
fn default() -> Self {
Self::new()
}
}
impl fmt::Display for SchemaDefinition {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.extend {
write!(f, "extend ")?;
// No description when it's an extension schema
} else if let Some(description) = &self.description {
writeln!(f, "{description}")?;
}
write!(f, "schema")?;
for directive in &self.directives {
write!(f, " {directive}")?;
}
if self.query.is_some() || self.mutation.is_some() || self.subscription.is_some() {
writeln!(f, " {{")?;
if let Some(query) = &self.query {
writeln!(f, " query: {query}")?;
}
if let Some(mutation) = &self.mutation {
writeln!(f, " mutation: {mutation}")?;
}
if let Some(subscription) = &self.subscription {
writeln!(f, " subscription: {subscription}")?;
}
writeln!(f, "}}")
} else {
writeln!(f)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
use pretty_assertions::assert_eq;
#[test]
fn it_encodes_schema_with_mutation_and_subscription() {
let mut schema_def = SchemaDefinition::new();
schema_def.query("TryingToFindCatQuery".to_string());
schema_def.mutation("MyMutation".to_string());
schema_def.subscription("MySubscription".to_string());
assert_eq!(
schema_def.to_string(),
indoc! { r#"
schema {
query: TryingToFindCatQuery
mutation: MyMutation
subscription: MySubscription
}
"#}
);
}
#[test]
fn it_encodes_extend_schema_with_mutation_and_subscription() {
let mut schema_def = SchemaDefinition::new();
schema_def.query("TryingToFindCatQuery".to_string());
schema_def.mutation("MyMutation".to_string());
schema_def.subscription("MySubscription".to_string());
schema_def.extend();
assert_eq!(
schema_def.to_string(),
indoc! { r#"
extend schema {
query: TryingToFindCatQuery
mutation: MyMutation
subscription: MySubscription
}
"#}
);
}
#[test]
fn it_encodes_extend_schema_with_directives_only() {
let mut schema_def = SchemaDefinition::new();
schema_def.directive(Directive::new("foo".to_string()));
schema_def.extend();
assert_eq!(
schema_def.to_string(),
indoc! { r#"
extend schema @foo
"#}
)
}
}