use std::collections::HashMap;
use crate::schema::CompiledSchema;
#[derive(Debug, Clone)]
pub struct CascadeMetadata {
mutation_entity_map: HashMap<String, String>,
entity_mutations_map: HashMap<String, Vec<String>>,
}
impl CascadeMetadata {
#[must_use]
pub fn new() -> Self {
Self {
mutation_entity_map: HashMap::new(),
entity_mutations_map: HashMap::new(),
}
}
pub fn add_mutation(&mut self, mutation_name: &str, entity_type: &str) {
let mutation_name = mutation_name.to_string();
let entity_type = entity_type.to_string();
self.mutation_entity_map.insert(mutation_name.clone(), entity_type.clone());
self.entity_mutations_map.entry(entity_type).or_default().push(mutation_name);
}
#[must_use]
pub fn get_entity_type(&self, mutation_name: &str) -> Option<&str> {
self.mutation_entity_map.get(mutation_name).map(|s| s.as_str())
}
#[must_use]
pub fn get_mutations_for_entity(&self, entity_type: &str) -> Vec<String> {
self.entity_mutations_map.get(entity_type).cloned().unwrap_or_default()
}
#[must_use]
pub fn count(&self) -> usize {
self.mutation_entity_map.len()
}
#[must_use]
pub fn contains_mutation(&self, mutation_name: &str) -> bool {
self.mutation_entity_map.contains_key(mutation_name)
}
#[must_use]
pub fn from_schema(schema: &CompiledSchema) -> Self {
let mut metadata = Self::new();
for mutation in &schema.mutations {
metadata.add_mutation(&mutation.name, &mutation.return_type);
}
metadata
}
}
impl Default for CascadeMetadata {
fn default() -> Self {
Self::new()
}
}