async_graphql/types/external/list/
vec_deque.rs

1use std::{borrow::Cow, collections::VecDeque};
2
3use crate::{
4    ContextSelectionSet, InputType, InputValueError, InputValueResult, OutputType, Positioned,
5    ServerResult, Value, parser::types::Field, registry, resolver_utils::resolve_list,
6};
7
8impl<T: InputType> InputType for VecDeque<T> {
9    type RawValueType = Self;
10
11    fn type_name() -> Cow<'static, str> {
12        Cow::Owned(format!("[{}]", T::qualified_type_name()))
13    }
14
15    fn qualified_type_name() -> String {
16        format!("[{}]!", T::qualified_type_name())
17    }
18
19    fn create_type_info(registry: &mut registry::Registry) -> String {
20        T::create_type_info(registry);
21        Self::qualified_type_name()
22    }
23
24    fn parse(value: Option<Value>) -> InputValueResult<Self> {
25        match value.unwrap_or_default() {
26            Value::List(values) => values
27                .into_iter()
28                .map(|value| InputType::parse(Some(value)))
29                .collect::<Result<_, _>>()
30                .map_err(InputValueError::propagate),
31            value => Ok({
32                let mut result = Self::default();
33                result
34                    .push_back(InputType::parse(Some(value)).map_err(InputValueError::propagate)?);
35                result
36            }),
37        }
38    }
39
40    fn to_value(&self) -> Value {
41        Value::List(self.iter().map(InputType::to_value).collect())
42    }
43
44    fn as_raw_value(&self) -> Option<&Self::RawValueType> {
45        Some(self)
46    }
47}
48
49#[cfg_attr(feature = "boxed-trait", async_trait::async_trait)]
50impl<T: OutputType> OutputType for VecDeque<T> {
51    fn type_name() -> Cow<'static, str> {
52        Cow::Owned(format!("[{}]", T::qualified_type_name()))
53    }
54
55    fn qualified_type_name() -> String {
56        format!("[{}]!", T::qualified_type_name())
57    }
58
59    fn create_type_info(registry: &mut registry::Registry) -> String {
60        T::create_type_info(registry);
61        Self::qualified_type_name()
62    }
63
64    async fn resolve(
65        &self,
66        ctx: &ContextSelectionSet<'_>,
67        field: &Positioned<Field>,
68    ) -> ServerResult<Value> {
69        resolve_list(ctx, field, self, Some(self.len())).await
70    }
71}