[][src]Trait async_graphql::ScalarType

pub trait ScalarType: Sized + Send {
    fn type_name() -> &'static str;
fn parse(value: Value) -> InputValueResult<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) -> InputValueResult<Self> {
        if let Value::Int(n) = value {
            Ok(MyInt(n as i32))
        } else {
            Err(InputValueError::ExpectedType(value))
        }
    }

    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) -> InputValueResult<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.

Implementing this function can find incorrect input values during the verification phase, which can 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[src]

impl ScalarType for f64[src]

impl ScalarType for i8[src]

impl ScalarType for i16[src]

impl ScalarType for i32[src]

impl ScalarType for u8[src]

impl ScalarType for u16[src]

impl ScalarType for u32[src]

impl ScalarType for i64[src]

impl ScalarType for u64[src]

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 Cursor[src]

impl ScalarType for ID[src]

impl<T: DeserializeOwned + Serialize + Send + Sync> ScalarType for Json<T>[src]

Loading content...