2022-04-19 04:25:11 +00:00
|
|
|
use std::{borrow::Cow, cmp::Eq, collections::HashSet as StdHashSet, hash::Hash};
|
2021-12-28 10:11:40 +00:00
|
|
|
|
|
|
|
use hashbrown::HashSet;
|
|
|
|
|
|
|
|
use crate::{
|
2022-04-19 04:25:11 +00:00
|
|
|
parser::types::Field, registry, resolver_utils::resolve_list, ContextSelectionSet, InputType,
|
|
|
|
InputValueError, InputValueResult, OutputType, Positioned, Result, ServerResult, Value,
|
2021-12-28 10:11:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
impl<T: InputType + Hash + Eq> InputType for HashSet<T> {
|
|
|
|
type RawValueType = Self;
|
|
|
|
|
|
|
|
fn type_name() -> Cow<'static, str> {
|
2021-12-28 10:25:30 +00:00
|
|
|
<StdHashSet<T> as InputType>::type_name()
|
2021-12-28 10:11:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn qualified_type_name() -> String {
|
2021-12-28 10:25:30 +00:00
|
|
|
<StdHashSet<T> as InputType>::qualified_type_name()
|
2021-12-28 10:11:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn create_type_info(registry: &mut registry::Registry) -> String {
|
2021-12-28 10:25:30 +00:00
|
|
|
<StdHashSet<T> as InputType>::create_type_info(registry)
|
2021-12-28 10:11:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn parse(value: Option<Value>) -> InputValueResult<Self> {
|
|
|
|
match value.unwrap_or_default() {
|
|
|
|
Value::List(values) => values
|
|
|
|
.into_iter()
|
|
|
|
.map(|value| InputType::parse(Some(value)))
|
|
|
|
.collect::<Result<_, _>>()
|
|
|
|
.map_err(InputValueError::propagate),
|
|
|
|
value => Ok({
|
|
|
|
let mut result = Self::default();
|
|
|
|
result.insert(InputType::parse(Some(value)).map_err(InputValueError::propagate)?);
|
|
|
|
result
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_value(&self) -> Value {
|
|
|
|
Value::List(self.iter().map(InputType::to_value).collect())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_raw_value(&self) -> Option<&Self::RawValueType> {
|
|
|
|
Some(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl<T: OutputType + Hash + Eq> OutputType for HashSet<T> {
|
|
|
|
fn type_name() -> Cow<'static, str> {
|
2021-12-28 10:25:30 +00:00
|
|
|
<StdHashSet<T> as OutputType>::type_name()
|
2021-12-28 10:11:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn qualified_type_name() -> String {
|
2021-12-28 10:25:30 +00:00
|
|
|
<StdHashSet<T> as OutputType>::qualified_type_name()
|
2021-12-28 10:11:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn create_type_info(registry: &mut registry::Registry) -> String {
|
2021-12-28 10:25:30 +00:00
|
|
|
<StdHashSet<T> as OutputType>::create_type_info(registry)
|
2021-12-28 10:11:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn resolve(
|
|
|
|
&self,
|
|
|
|
ctx: &ContextSelectionSet<'_>,
|
|
|
|
field: &Positioned<Field>,
|
|
|
|
) -> ServerResult<Value> {
|
|
|
|
resolve_list(ctx, field, self, Some(self.len())).await
|
|
|
|
}
|
|
|
|
}
|