use std::net::IpAddr; use dynamic_graphql::Scalar; use dynamic_graphql::ScalarValue; #[derive(Scalar)] #[graphql(name = "IP")] pub struct IP(pub IpAddr); pub struct StringValue(pub String); impl TryFrom for StringValue { type Error = dynamic_graphql::Error; fn try_from(value: dynamic_graphql::Value) -> Result { match value { dynamic_graphql::Value::String(s) => Ok(StringValue(s)), _ => Err(dynamic_graphql::Error::new( "Expected a string value".to_string(), )), } } } impl ScalarValue for IP { fn from_value(value: dynamic_graphql::Value) -> dynamic_graphql::Result where Self: Sized, { let value = StringValue::try_from(value)?.0; let value = value.parse::()?; Ok(IP(value)) } fn to_value(&self) -> dynamic_graphql::Value { self.0.to_string().into() } } #[derive(Scalar)] pub struct MyString(pub String); impl ScalarValue for MyString { fn from_value(value: dynamic_graphql::Value) -> dynamic_graphql::Result where Self: Sized, { let value = StringValue::try_from(value)?.0; Ok(MyString(value)) } fn to_value(&self) -> dynamic_graphql::Value { self.0.clone().into() } }