async_graphql/types/
any.rs

1use crate::{InputValueResult, Scalar, ScalarType, Value};
2
3/// Any scalar (For [Apollo Federation](https://www.apollographql.com/docs/apollo-server/federation/introduction))
4///
5/// The `Any` scalar is used to pass representations of entities from external
6/// services into the root `_entities` field for execution.
7#[derive(Clone, Eq, PartialEq, Debug)]
8pub struct Any(pub Value);
9
10/// The `_Any` scalar is used to pass representations of entities from external
11/// services into the root `_entities` field for execution.
12#[Scalar(internal, name = "_Any")]
13impl ScalarType for Any {
14    fn parse(value: Value) -> InputValueResult<Self> {
15        Ok(Self(value))
16    }
17
18    fn is_valid(_value: &Value) -> bool {
19        true
20    }
21
22    fn to_value(&self) -> Value {
23        self.0.clone()
24    }
25}
26
27impl<T: Into<Value>> From<T> for Any {
28    fn from(value: T) -> Any {
29        Any(value.into())
30    }
31}
32
33#[cfg(test)]
34mod test {
35    use super::*;
36
37    #[test]
38    fn test_conversion_ok() {
39        let value = Value::List(vec![
40            Value::Number(1.into()),
41            Value::Boolean(true),
42            Value::Null,
43        ]);
44        let expected = Any(value.clone());
45        let output: Any = value.into();
46        assert_eq!(output, expected);
47    }
48}