[−][src]Trait async_graphql::Scalar
Represents a GraphQL scalar
You can implement the trait to create a custom scalar.
Examples
use async_graphql::*; struct MyInt(i32); impl Scalar for MyInt { fn type_name() -> &'static str { "MyInt" } fn parse(value: &Value) -> Option<Self> { if let Value::Int(n) = value { Some(MyInt(n.as_i64().unwrap() as i32)) } else { None } } fn to_json(&self) -> Result<serde_json::Value> { Ok(self.0.into()) } } impl_scalar!(MyInt); // // Don't forget this one
Required methods
fn type_name() -> &'static str
The type name of a scalar.
fn parse(value: &Value) -> Option<Self>
Parse a scalar value, return Some(Self)
if successful, otherwise return None
.
fn to_json(&self) -> Result<Value>
Convert the scalar value to json value.
Provided methods
fn description() -> Option<&'static str>
The description of a scalar.
fn is_valid(value: &Value) -> bool
Checks for a valid scalar value.
The default implementation is to try to parse it, and in some cases you can implement this on your own to improve performance.