countries/
async_graphql.rs

1extern crate alloc;
2
3use super::StaticMap;
4use alloc::borrow::Cow;
5use async_graphql::{
6    indexmap::IndexMap,
7    parser::types::Field,
8    registry::{MetaType, MetaTypeId, Registry},
9    to_value, ContextSelectionSet, Name, OutputType, Positioned, ServerResult, Value,
10};
11
12#[async_graphql::async_trait::async_trait]
13impl<
14        K: 'static + Send + Sync + Eq + core::hash::Hash + core::fmt::Display,
15        V: 'static + Send + Sync + ::serde::Serialize,
16    > OutputType for StaticMap<K, V>
17{
18    fn type_name() -> Cow<'static, str> {
19        Cow::Borrowed("JSONObject")
20    }
21
22    fn create_type_info(registry: &mut Registry) -> String {
23        registry.create_output_type::<Self, _>(MetaTypeId::Scalar, |_| MetaType::Scalar {
24            name: <Self as OutputType>::type_name().to_string(),
25            description: Some("A scalar that can represent any JSON Object value."),
26            is_valid: |_| true,
27            visible: None,
28            specified_by_url: None,
29            inaccessible: false,
30            tags: &[],
31        })
32    }
33
34    async fn resolve(
35        &self,
36        _ctx: &ContextSelectionSet<'_>,
37        _field: &Positioned<Field>,
38    ) -> ServerResult<Value> {
39        let mut map = IndexMap::new();
40        for (name, value) in self.iter() {
41            map.insert(
42                Name::new(name.to_string()),
43                to_value(value).unwrap_or_default(),
44            );
45        }
46        Ok(Value::Object(map))
47    }
48}