2020-03-03 11:15:18 +00:00
|
|
|
use crate::{registry, ContextSelectionSet, GQLInputValue, GQLOutputValue, GQLType, Result, Value};
|
2020-03-02 00:24:49 +00:00
|
|
|
use std::borrow::Cow;
|
|
|
|
|
|
|
|
impl<T: GQLType> GQLType for Vec<T> {
|
|
|
|
fn type_name() -> Cow<'static, str> {
|
2020-03-05 06:23:55 +00:00
|
|
|
Cow::Owned(format!("[{}]", T::qualified_type_name()))
|
2020-03-04 06:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: GQLInputValue> GQLInputValue 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 {
|
|
|
|
result.push(GQLInputValue::parse(value)?);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl<T: GQLOutputValue + Send + Sync> GQLOutputValue for Vec<T> {
|
|
|
|
async fn resolve(&self, ctx: &ContextSelectionSet<'_>) -> Result<serde_json::Value> {
|
|
|
|
let mut res = Vec::new();
|
|
|
|
for item in self {
|
2020-03-03 11:15:18 +00:00
|
|
|
res.push(item.resolve(&ctx).await?);
|
2020-03-02 00:24:49 +00:00
|
|
|
}
|
|
|
|
Ok(res.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: GQLType> GQLType for &[T] {
|
|
|
|
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]
|
|
|
|
impl<T: GQLOutputValue + Send + Sync> GQLOutputValue for &[T] {
|
|
|
|
async fn resolve(&self, ctx: &ContextSelectionSet<'_>) -> Result<serde_json::Value> {
|
|
|
|
let mut res = Vec::new();
|
|
|
|
for item in self.iter() {
|
2020-03-03 11:15:18 +00:00
|
|
|
res.push(item.resolve(&ctx).await?);
|
2020-03-02 00:24:49 +00:00
|
|
|
}
|
|
|
|
Ok(res.into())
|
|
|
|
}
|
|
|
|
}
|
2020-03-05 06:23:55 +00:00
|
|
|
|
2020-03-05 13:34:31 +00:00
|
|
|
impl<T: GQLType> GQLType for &Vec<T> {
|
|
|
|
fn type_name() -> Cow<'static, str> {
|
|
|
|
Cow::Owned(format!("[{}]", T::type_name()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_type_info(registry: &mut registry::Registry) -> String {
|
|
|
|
T::create_type_info(registry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl<T: GQLOutputValue + Send + Sync> GQLOutputValue for &Vec<T> {
|
|
|
|
async fn resolve(&self, ctx: &ContextSelectionSet<'_>) -> Result<serde_json::Value> {
|
|
|
|
let mut res = Vec::new();
|
|
|
|
for item in self.iter() {
|
|
|
|
res.push(item.resolve(&ctx).await?);
|
|
|
|
}
|
|
|
|
Ok(res.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-05 06:23:55 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::GQLType;
|
|
|
|
|
|
|
|
#[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]");
|
|
|
|
}
|
|
|
|
}
|