[][src]Trait async_graphql::Scalar

pub trait Scalar: Sized + Send {
    fn type_name() -> &'static str;
fn parse(value: &Value) -> Option<Self>;
fn to_json(&self) -> Result<Value>; fn description() -> Option<&'static str> { ... }
fn is_valid(value: &Value) -> bool { ... } }

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.

Loading content...

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.

Loading content...

Implementations on Foreign Types

impl Scalar for bool[src]

impl Scalar for Tz[src]

impl Scalar for DateTime<Utc>[src]

Implement the DateTime scalar

The input/output is a string in RFC3339 format.

impl Scalar for f32[src]

impl Scalar for f64[src]

impl Scalar for i8[src]

impl Scalar for i16[src]

impl Scalar for i32[src]

impl Scalar for i64[src]

impl Scalar for u8[src]

impl Scalar for u16[src]

impl Scalar for u32[src]

impl Scalar for u64[src]

impl Scalar for String[src]

impl Scalar for Url[src]

impl Scalar for ObjectId[src]

impl Scalar for Uuid[src]

Loading content...

Implementors

impl Scalar for Any[src]

impl Scalar for ID[src]

Loading content...