[][src]Trait async_graphql::GQLScalar

pub trait GQLScalar: 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 GQLScalar 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 GQLScalar for bool[src]

impl GQLScalar for f32[src]

impl GQLScalar for f64[src]

impl GQLScalar for i8[src]

impl GQLScalar for i16[src]

impl GQLScalar for i32[src]

impl GQLScalar for i64[src]

impl GQLScalar for u8[src]

impl GQLScalar for u16[src]

impl GQLScalar for u32[src]

impl GQLScalar for u64[src]

impl GQLScalar for String[src]

impl GQLScalar for DateTime<Utc>[src]

impl GQLScalar for Uuid[src]

Loading content...

Implementors

impl GQLScalar for ID[src]

Loading content...