Macro async_graphql::scalar [−][src]
macro_rules! scalar {
($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");
struct Query;
#[Object]
impl Query {
async fn value(&self, input: MyValue) -> MyValue {
input
}
}
tokio::runtime::Runtime::new().unwrap().block_on(async move {
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},
}
}));
});