[][src]Trait async_graphql::ScalarType

pub trait ScalarType: 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);

#[Scalar]
impl ScalarType 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())
    }
}

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 ScalarType for bool[src]

impl ScalarType for Tz[src]

impl ScalarType for DateTime<Utc>[src]

Implement the DateTime scalar

The input/output is a string in RFC3339 format.

impl ScalarType for f32

impl ScalarType for f64

impl ScalarType for i8

impl ScalarType for i16

impl ScalarType for i32

impl ScalarType for u8

impl ScalarType for u16

impl ScalarType for u32

impl ScalarType for i64

impl ScalarType for u64

impl ScalarType for String[src]

impl ScalarType for Url[src]

impl ScalarType for ObjectId[src]

impl ScalarType for UtcDateTime[src]

impl ScalarType for Uuid[src]

Loading content...

Implementors

impl ScalarType for Any[src]

impl ScalarType for ID[src]

Loading content...