2020-09-18 00:52:13 +00:00
|
|
|
use crate::{InputValueResult, Scalar, ScalarType, Value};
|
2020-04-09 14:03:09 +00:00
|
|
|
|
2020-09-17 03:22:09 +00:00
|
|
|
/// Any scalar (For [Apollo Federation](https://www.apollographql.com/docs/apollo-server/federation/introduction))
|
2020-04-09 14:03:09 +00:00
|
|
|
///
|
2022-04-19 04:25:11 +00:00
|
|
|
/// The `Any` scalar is used to pass representations of entities from external
|
|
|
|
/// services into the root `_entities` field for execution.
|
2022-08-12 06:05:46 +00:00
|
|
|
#[derive(Clone, Eq, PartialEq, Debug)]
|
2020-04-09 14:03:09 +00:00
|
|
|
pub struct Any(pub Value);
|
|
|
|
|
2022-04-19 04:25:11 +00:00
|
|
|
/// The `_Any` scalar is used to pass representations of entities from external
|
|
|
|
/// services into the root `_entities` field for execution.
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Scalar(internal, name = "_Any")]
|
2020-04-27 09:58:10 +00:00
|
|
|
impl ScalarType for Any {
|
2020-05-11 13:47:24 +00:00
|
|
|
fn parse(value: Value) -> InputValueResult<Self> {
|
|
|
|
Ok(Self(value))
|
2020-04-09 14:03:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_valid(_value: &Value) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2020-05-26 10:34:43 +00:00
|
|
|
fn to_value(&self) -> Value {
|
|
|
|
self.0.clone()
|
2020-04-09 14:03:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-08 08:21:27 +00:00
|
|
|
impl<T: Into<Value>> From<T> for Any {
|
2020-04-29 10:36:13 +00:00
|
|
|
fn from(value: T) -> Any {
|
|
|
|
Any(value.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_conversion_ok() {
|
2020-07-28 14:13:07 +00:00
|
|
|
let value = Value::List(vec![
|
|
|
|
Value::Number(1.into()),
|
|
|
|
Value::Boolean(true),
|
|
|
|
Value::Null,
|
|
|
|
]);
|
2020-04-29 10:36:13 +00:00
|
|
|
let expected = Any(value.clone());
|
|
|
|
let output: Any = value.into();
|
|
|
|
assert_eq!(output, expected);
|
|
|
|
}
|
|
|
|
}
|