async-graphql/src/types/list.rs

93 lines
2.9 KiB
Rust
Raw Normal View History

2020-03-25 03:39:28 +00:00
use crate::{registry, ContextSelectionSet, InputValueType, OutputValueType, Result, Type, Value};
use graphql_parser::Pos;
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();
2020-04-05 08:00:26 +00:00
for elem_value in values {
result.push(InputValueType::parse(elem_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-04-05 08:00:26 +00:00
_ => Some(vec![InputValueType::parse(value)?]),
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> {
async fn resolve(
value: &Self,
ctx: &ContextSelectionSet<'_>,
pos: Pos,
) -> Result<serde_json::Value> {
2020-03-25 03:39:28 +00:00
let mut futures = Vec::with_capacity(value.len());
2020-03-26 03:34:28 +00:00
for (idx, item) in value.iter().enumerate() {
let ctx_idx = ctx.with_index(idx);
futures.push(async move { OutputValueType::resolve(item, &ctx_idx, pos).await });
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] {
async fn resolve(
value: &Self,
ctx: &ContextSelectionSet<'_>,
pos: Pos,
) -> Result<serde_json::Value> {
2020-03-25 03:39:28 +00:00
let mut futures = Vec::with_capacity(value.len());
2020-03-26 03:34:28 +00:00
for (idx, item) in (*value).iter().enumerate() {
let ctx_idx = ctx.with_index(idx);
futures.push(async move { OutputValueType::resolve(item, &ctx_idx, pos).await });
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]");
}
}