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
use async_graphql::Context;
use fnv::FnvHashMap;
use std::any::{Any, TypeId};

pub struct SchemaData(FnvHashMap<TypeId, Box<dyn Any + Sync + Send>>);

impl SchemaData {
    pub fn new() -> Self {
        Self(Default::default())
    }
}

impl Default for SchemaData {
    fn default() -> Self {
        Self::new()
    }
}

impl SchemaData {
    pub fn insert<T: Any + Sync + Send>(&mut self, value: T) {
        self.0.insert(TypeId::of::<T>(), Box::new(value));
    }
    pub fn get<T: Any + Sync + Send>(&self) -> Option<&T> {
        self.0
            .get(&TypeId::of::<T>())
            .and_then(|v| v.downcast_ref::<T>())
    }
    pub fn get_mut<T: Any + Sync + Send>(&mut self) -> Option<&mut T> {
        self.0
            .get_mut(&TypeId::of::<T>())
            .and_then(|v| v.downcast_mut::<T>())
    }
    pub fn get_or_default<T: Any + Sync + Send + Default>(&mut self) -> &mut T {
        self.0
            .entry(TypeId::of::<T>())
            .or_insert_with(|| Box::<T>::default())
            .downcast_mut()
            .unwrap()
    }
    pub fn get_mut_or_default<T: Any + Sync + Send + Default>(&mut self) -> &mut T {
        self.0
            .entry(TypeId::of::<T>())
            .or_insert_with(|| Box::<T>::default())
            .downcast_mut()
            .unwrap()
    }
}

pub trait GetSchemaData {
    fn get_schema_data(&self) -> &SchemaData;
}

impl GetSchemaData for Context<'_> {
    fn get_schema_data(&self) -> &SchemaData {
        self.data_unchecked()
    }
}