async_graphql::resolver_utils

Macro scalar

source
macro_rules! scalar {
    ($ty:ty, $name:literal, $desc:literal, $specified_by_url:literal) => { ... };
    ($ty:ty, $name:literal, $desc:literal) => { ... };
    ($ty:ty, $name:literal) => { ... };
    ($ty:ty) => { ... };
}
Expand description

Define a scalar

If your type implemented serde::Serialize and serde::Deserialize, then you can use this macro to define a scalar more simply. It helps you implement the ScalarType::parse and ScalarType::to_value functions by calling the from_value and to_value functions.

ยงExamples

use async_graphql::*;
use serde::{Serialize, Deserialize};
use std::collections::HashMap;

#[derive(Serialize, Deserialize)]
struct MyValue {
    a: i32,
    b: HashMap<String, i32>,     
}

scalar!(MyValue);

// Rename to `MV`.
// scalar!(MyValue, "MV");

// Rename to `MV` and add description.
// scalar!(MyValue, "MV", "This is my value");

// Rename to `MV`, add description and specifiedByURL.
// scalar!(MyValue, "MV", "This is my value", "https://tools.ietf.org/html/rfc4122");

struct Query;

#[Object]
impl Query {
    async fn value(&self, input: MyValue) -> MyValue {
        input
    }
}

let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
let res = schema.execute(r#"{ value(input: {a: 10, b: {v1: 1, v2: 2} }) }"#).await.into_result().unwrap().data;
assert_eq!(res, value!({
    "value": {
        "a": 10,
        "b": {"v1": 1, "v2": 2},
    }
}));