async-graphql/src/types/list.rs

82 lines
2.5 KiB
Rust
Raw Normal View History

2020-03-25 03:39:28 +00:00
use crate::{registry, ContextSelectionSet, InputValueType, OutputValueType, Result, Type, Value};
2020-03-02 00:24:49 +00:00
use std::borrow::Cow;
2020-03-19 09:20:12 +00:00
impl<T: Type> Type for Vec<T> {
2020-03-02 00:24:49 +00:00
fn type_name() -> Cow<'static, str> {
2020-03-05 06:23:55 +00:00
Cow::Owned(format!("[{}]", T::qualified_type_name()))
}
fn qualified_type_name() -> String {
2020-03-05 06:23:55 +00:00
format!("[{}]!", T::qualified_type_name())
2020-03-02 00:24:49 +00:00
}
2020-03-03 03:48:00 +00:00
2020-03-03 11:15:18 +00:00
fn create_type_info(registry: &mut registry::Registry) -> String {
2020-03-05 13:34:31 +00:00
T::create_type_info(registry);
Self::qualified_type_name()
2020-03-03 03:48:00 +00:00
}
2020-03-02 00:24:49 +00:00
}
2020-03-19 09:20:12 +00:00
impl<T: InputValueType> InputValueType for Vec<T> {
2020-03-04 02:38:07 +00:00
fn parse(value: &Value) -> Option<Self> {
2020-03-02 00:24:49 +00:00
match value {
Value::List(values) => {
let mut result = Vec::new();
for value in values {
2020-03-19 09:20:12 +00:00
result.push(InputValueType::parse(value)?);
2020-03-02 00:24:49 +00:00
}
2020-03-03 11:15:18 +00:00
Some(result)
2020-03-02 00:24:49 +00:00
}
2020-03-03 11:15:18 +00:00
_ => None,
2020-03-02 00:24:49 +00:00
}
}
}
2020-03-21 01:32:13 +00:00
#[allow(clippy::ptr_arg)]
2020-03-02 00:24:49 +00:00
#[async_trait::async_trait]
2020-03-19 09:20:12 +00:00
impl<T: OutputValueType + Send + Sync> OutputValueType for Vec<T> {
2020-03-25 03:39:28 +00:00
async fn resolve(value: &Self, ctx: &ContextSelectionSet<'_>) -> Result<serde_json::Value> {
let mut futures = Vec::with_capacity(value.len());
2020-03-06 15:58:43 +00:00
for item in value {
2020-03-25 03:39:28 +00:00
futures.push(OutputValueType::resolve(item, &ctx));
2020-03-02 00:24:49 +00:00
}
2020-03-25 03:39:28 +00:00
Ok(futures::future::try_join_all(futures).await?.into())
2020-03-02 00:24:49 +00:00
}
}
2020-03-19 09:20:12 +00:00
impl<T: Type> Type for &[T] {
2020-03-02 00:24:49 +00:00
fn type_name() -> Cow<'static, str> {
2020-03-03 11:15:18 +00:00
Cow::Owned(format!("[{}]", T::type_name()))
2020-03-02 00:24:49 +00:00
}
2020-03-03 03:48:00 +00:00
2020-03-03 11:15:18 +00:00
fn create_type_info(registry: &mut registry::Registry) -> String {
2020-03-03 03:48:00 +00:00
T::create_type_info(registry)
}
2020-03-02 00:24:49 +00:00
}
#[async_trait::async_trait]
2020-03-19 09:20:12 +00:00
impl<T: OutputValueType + Send + Sync> OutputValueType for &[T] {
2020-03-25 03:39:28 +00:00
async fn resolve(value: &Self, ctx: &ContextSelectionSet<'_>) -> Result<serde_json::Value> {
let mut futures = Vec::with_capacity(value.len());
for item in *value {
futures.push(OutputValueType::resolve(item, &ctx));
2020-03-05 13:34:31 +00:00
}
2020-03-25 03:39:28 +00:00
Ok(futures::future::try_join_all(futures).await?.into())
2020-03-05 13:34:31 +00:00
}
}
2020-03-05 06:23:55 +00:00
#[cfg(test)]
mod tests {
2020-03-19 09:20:12 +00:00
use crate::Type;
2020-03-05 06:23:55 +00:00
#[test]
fn test_list_type() {
assert_eq!(Vec::<i32>::type_name(), "[Int!]");
assert_eq!(Vec::<Option<i32>>::type_name(), "[Int]");
assert_eq!(Option::<Vec::<Option<i32>>>::type_name(), "[Int]");
assert_eq!(Vec::<i32>::qualified_type_name(), "[Int!]!");
assert_eq!(Vec::<Option<i32>>::qualified_type_name(), "[Int]!");
assert_eq!(Option::<Vec::<Option<i32>>>::qualified_type_name(), "[Int]");
}
}