[][src]Macro juniper::graphql_scalar

macro_rules! graphql_scalar {
    ( @as_expr $e:expr) => { ... };
    (
        @generate,
        meta = {
            name = $name:ty,
            outname = {$($outname:tt)+},
            scalar = {$($scalar:tt)+},
            $(description = $descr:tt,)*
        },
        resolve = {
            self_var = $resolve_self_var:ident,
            body = $resolve_body: block,
            return_type = $resolve_retun_type: ty,
        },
        from_input_value = {
            arg = $from_input_value_arg: ident,
            result = $from_input_value_result: ty,
            body = $from_input_value_body: block,
        },
        from_str = {
            value_arg = $from_str_arg: ident,
            result = $from_str_result: ty,
            body = $from_str_body: block,
            lifetime = $from_str_lt: tt,
        },

    ) => { ... };
    (
        @parse_functions,
        meta = {
            name = $name:ty,
            outname = {$($outname:tt)+},
            scalar = {$($scalar:tt)+},
            $(description = $descr:tt,)*
        },
        resolve = {$($resolve_body:tt)+},
        from_input_value = {$($from_input_value_body:tt)+},
        from_str = {$($from_str_body:tt)+},
        rest =
    ) => { ... };
    (
        @parse_functions,
        meta = {
            name = $name:ty,
            outname = {$($outname:tt)+},
            scalar = {$($scalar:tt)+},
            $(description = $descr:tt,)*
        },
        $(from_input_value = {$($from_input_value_body:tt)+})*,
        $(from_str = {$($from_str_body:tt)+})*,
        rest =
    ) => { ... };
    (
        @parse_functions,
        meta = {
            name = $name:ty,
            outname = {$($outname:tt)+},
            scalar = {$($scalar:tt)+},
            $(description = $descr:tt,)*
        },
        resolve = {$($resolve_body:tt)+},
        $(from_str = {$($from_str_body:tt)+})*,
        rest =
    ) => { ... };
    (
        @parse_functions,
        meta = {
            name = $name:ty,
            outname = {$($outname:tt)+},
            scalar = {$($scalar:tt)+},
            $(description = $descr:tt,)*
        },
        resolve = {$($resolve_body:tt)+},
        from_input_value = {$($from_input_value_body:tt)+},
        rest =
    ) => { ... };
    (
        @parse_functions,
        meta = {$($meta:tt)*},
        $(resolve = {$($resolve_body:tt)+},)*
        $(from_input_value = {$($from_input_value_body:tt)+},)*
        $(from_str = {$($from_str_body:tt)+},)*
        rest = resolve(&$selfvar:ident) -> $return_ty:ty $body:block $($rest:tt)*
    ) => { ... };
    (
        @parse_functions,
        meta = { $($meta:tt)* },
        $(resolve = {$($resolve_body:tt)+})*,
        $(from_input_value = {$($from_input_value_body:tt)+},)*
        $(from_str = {$($from_str_body:tt)+},)*
        rest = from_input_value($arg:ident: &InputValue) -> $result:ty $body:block $($rest:tt)*
    ) => { ... };
    (
        @parse_functions,
        meta = { $($meta:tt)* },
        $(resolve = {$($resolve_body:tt)+},)*
        $(from_input_value = {$($from_input_value_body:tt)+},)*
        $(from_str = {$($from_str_body:tt)+},)*
        rest = from_str<$from_str_lt: tt>($value_arg:ident: ScalarToken<$ignored_lt2: tt>) -> $result:ty $body:block $($rest:tt)*
    ) => { ... };
    (
        @parse_functions,
        meta = {
            name = $name:ty,
            outname = {$($outname:tt)+},
            scalar = {$($scalar:tt)+},
        },
        $(resolve = {$($resolve_body:tt)+},)*
        $(from_input_value = {$($from_input_value_body:tt)+},)*
        $(from_str = {$($from_str_body:tt)+},)*
        rest = description: $descr:tt $($rest:tt)*
    ) => { ... };
    (
        @parse,
        meta = {
            lifetimes = [],
            name = $name: ty,
            outname = {$($outname:tt)*},
            scalar = {$($scalar:tt)*},
        },
        rest = $($rest:tt)*
    ) => { ... };
    (@$($stuff:tt)*) => { ... };
    ($($rest:tt)*) => { ... };
}

Expose GraphQL scalars

The GraphQL language defines a number of built-in scalars: strings, numbers, and booleans. This macro can be used either to define new types of scalars (e.g. timestamps), or expose other types as one of the built-in scalars (e.g. bigints as numbers or strings).

Since the preferred transport protocol for GraphQL responses is JSON, most custom scalars will be transferred as strings. You therefore need to ensure that the client library you are sending data to can parse the custom value into a datatype appropriate for that platform.

By default the trait is implemented in terms of the default scalar value representation provided by juniper. If that does not fit your needs it is possible to use the same syntax as on graphql_object! to specify a custom representation.

struct UserID(String);

juniper::graphql_scalar!(UserID {
    description: "An opaque identifier, represented as a string"

    resolve(&self) -> Value {
        Value::string(&self.0)
    }

    from_input_value(v: &InputValue) -> Option<UserID> {
        v.as_string_value().map(|s| UserID(s.to_owned()))
    }

    from_str<'a>(value: ScalarToken<'a>) -> ParseScalarResult<'a> {
        <String as ParseScalarValue>::from_str(value)
    }
});

In addition to implementing GraphQLType for the type in question, FromInputValue and ToInputValue is also implemented. This makes the type usable as arguments and default values.