2020-04-27 09:58:10 +00:00
|
|
|
use crate::{Result, ScalarType, Value};
|
|
|
|
use async_graphql_derive::Scalar;
|
2020-04-09 14:03:09 +00:00
|
|
|
use itertools::Itertools;
|
2020-05-07 10:49:09 +00:00
|
|
|
use serde::de::DeserializeOwned;
|
2020-04-09 14:03:09 +00:00
|
|
|
|
|
|
|
/// Any scalar
|
|
|
|
///
|
|
|
|
/// The `Any` scalar is used to pass representations of entities from external services into the root `_entities` field for execution.
|
|
|
|
#[derive(Clone, PartialEq, Debug)]
|
|
|
|
pub struct Any(pub Value);
|
|
|
|
|
2020-04-27 09:58:10 +00:00
|
|
|
#[Scalar(internal)]
|
|
|
|
impl ScalarType for Any {
|
2020-04-09 14:03:09 +00:00
|
|
|
fn type_name() -> &'static str {
|
|
|
|
"_Any"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn description() -> Option<&'static str> {
|
|
|
|
Some("The `_Any` scalar is used to pass representations of entities from external services into the root `_entities` field for execution.")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse(value: &Value) -> Option<Self> {
|
|
|
|
Some(Self(value.clone()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_valid(_value: &Value) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_json(&self) -> Result<serde_json::Value> {
|
2020-05-07 12:41:00 +00:00
|
|
|
Ok(gql_value_to_json_value(self.0.clone()))
|
2020-04-09 14:03:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 10:49:09 +00:00
|
|
|
impl Any {
|
|
|
|
/// Parse this `Any` value to T by `serde_json`.
|
2020-05-07 10:50:47 +00:00
|
|
|
pub fn parse_value<T: DeserializeOwned>(&self) -> std::result::Result<T, serde_json::Error> {
|
2020-05-07 10:49:09 +00:00
|
|
|
serde_json::from_value(self.to_json().unwrap())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 12:41:00 +00:00
|
|
|
pub(crate) fn gql_value_to_json_value(value: Value) -> serde_json::Value {
|
2020-04-09 14:03:09 +00:00
|
|
|
match value {
|
|
|
|
Value::Null => serde_json::Value::Null,
|
2020-05-07 12:44:24 +00:00
|
|
|
Value::Variable(name) => name.into(),
|
2020-05-09 09:55:04 +00:00
|
|
|
Value::Int(n) => n.into(),
|
2020-05-07 12:41:00 +00:00
|
|
|
Value::Float(n) => n.into(),
|
|
|
|
Value::String(s) => s.into(),
|
|
|
|
Value::Boolean(v) => v.into(),
|
|
|
|
Value::Enum(e) => e.into(),
|
2020-04-09 14:03:09 +00:00
|
|
|
Value::List(values) => values
|
2020-05-07 12:41:00 +00:00
|
|
|
.into_iter()
|
2020-05-07 12:44:24 +00:00
|
|
|
.map(gql_value_to_json_value)
|
2020-04-09 14:03:09 +00:00
|
|
|
.collect_vec()
|
|
|
|
.into(),
|
|
|
|
Value::Object(obj) => serde_json::Value::Object(
|
2020-05-07 12:41:00 +00:00
|
|
|
obj.into_iter()
|
2020-05-07 12:44:24 +00:00
|
|
|
.map(|(k, v)| (k, gql_value_to_json_value(v)))
|
2020-04-09 14:03:09 +00:00
|
|
|
.collect(),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
2020-04-29 10:36:13 +00:00
|
|
|
|
|
|
|
impl<T> From<T> for Any
|
|
|
|
where
|
|
|
|
T: Into<Value>,
|
|
|
|
{
|
|
|
|
fn from(value: T) -> Any {
|
|
|
|
Any(value.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_conversion_ok() {
|
|
|
|
let value = Value::List(vec![Value::Int(1.into()), Value::Float(2.0), Value::Null]);
|
|
|
|
let expected = Any(value.clone());
|
|
|
|
let output: Any = value.into();
|
|
|
|
assert_eq!(output, expected);
|
|
|
|
}
|
|
|
|
}
|