pub trait ScalarType: Sized + Send {
    fn parse(value: Value) -> InputValueResult<Self>;
    fn to_value(&self) -> Value;

    fn is_valid(_value: &Value) -> bool { ... }
}
Expand description

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 parse(value: Value) -> InputValueResult<Self> {
        if let Value::Number(n) = &value {
            if let Some(n) = n.as_i64() {
                return Ok(MyInt(n as i32));
            }
        }
        Err(InputValueError::expected_type(value))
    }

    fn to_value(&self) -> Value {
        Value::Number(self.0.into())
    }
}

Required Methods

Parse a scalar value.

Convert the scalar to Value.

Provided Methods

Checks for a valid scalar value.

Implementing this function can find incorrect input values during the verification phase, which can improve performance.

Implementations on Foreign Types

The Boolean scalar type represents true or false.

The Binary scalar type represents binary data.

The Char scalar type represents a unicode char. The input and output values are a string, and there can only be one unicode character in this string.

The Float scalar type represents signed double-precision fractional values as specified by IEEE 754.

The Float scalar type represents signed double-precision fractional values as specified by IEEE 754.

The Int scalar type represents non-fractional whole numeric values.

The Int scalar type represents non-fractional whole numeric values.

The Int scalar type represents non-fractional whole numeric values.

The Int scalar type represents non-fractional whole numeric values.

The Int scalar type represents non-fractional whole numeric values.

The Int scalar type represents non-fractional whole numeric values.

The Int scalar type represents non-fractional whole numeric values.

The Int scalar type represents non-fractional whole numeric values.

The Int scalar type represents non-fractional whole numeric values.

The Int scalar type represents non-fractional whole numeric values.

The Int scalar type represents non-fractional whole numeric values.

The Int scalar type represents non-fractional whole numeric values.

The Int scalar type represents non-fractional whole numeric values.

The Int scalar type represents non-fractional whole numeric values.

The Int scalar type represents non-fractional whole numeric values.

The Int scalar type represents non-fractional whole numeric values.

The Int scalar type represents non-fractional whole numeric values.

The Int scalar type represents non-fractional whole numeric values.

The String scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text.

Implement the DateTime scalar

The input/output is a string in RFC3339 format.

Implement the DateTime scalar

The input/output is a string in RFC3339 format.

Implement the DateTime scalar

The input/output is a string in RFC3339 format.

Implement the Duration scalar

The input/output is a string in ISO8601 format.

ISO 8601 calendar date without timezone. Format: %Y-%m-%d

Examples

  • 1994-11-13
  • 2000-02-24

ISO 8601 time without timezone. Allows for the nanosecond precision and optional leap second representation. Format: %H:%M:%S%.f

Examples

  • 08:59:60.123

ISO 8601 combined date and time without timezone.

Examples

  • 2015-07-01T08:59:60.123,

ISO 8601 calendar date without timezone. Format: %Y-%m-%d

Examples

  • 1994-11-13
  • 2000-02-24

A datetime with timezone offset.

The input is a string in RFC3339 format, e.g. “2022-01-12T04:00:19.12345Z” or “2022-01-12T04:00:19+03:00”. The output is also a string in RFC3339 format, but it is always normalized to the UTC (Z) offset, e.g. “2022-01-12T04:00:19.12345Z”.

A local datetime without timezone offset.

The input/output is a string in ISO 8601 format without timezone, including subseconds. E.g. “2022-01-12T07:30:19.12345”.

URL is a String implementing the URL Standard

A UUID is a unique 128-bit number, stored as 16 octets. UUIDs are parsed as Strings within GraphQL. UUIDs are used to assign unique identifiers to entities without requiring a central allocating authority.

References

A UUID is a unique 128-bit number, stored as 16 octets. UUIDs are parsed as Strings within GraphQL. UUIDs are used to assign unique identifiers to entities without requiring a central allocating authority.

References

Implementors

The _Any scalar is used to pass representations of entities from external services into the root _entities field for execution.