Fix the problem of scalar macro.

This commit is contained in:
Sunli 2020-11-03 13:50:22 +08:00
parent 927f289c07
commit 03144a47b9
2 changed files with 44 additions and 8 deletions

View File

@ -92,16 +92,12 @@ pub trait ScalarType: Sized + Send {
/// ```
#[macro_export]
macro_rules! scalar {
($ty:ty, $name:expr, $desc:literal) => {
$crate::scalar_internal!(
$ty,
::std::stringify!($ty),
::std::option::Option::Some($desc)
);
($ty:ty, $name:literal, $desc:literal) => {
$crate::scalar_internal!($ty, $name, ::std::option::Option::Some($desc));
};
($ty:ty, $name:expr) => {
$crate::scalar_internal!($ty, ::std::stringify!($ty), ::std::option::Option::None);
($ty:ty, $name:literal) => {
$crate::scalar_internal!($ty, $name, ::std::option::Option::None);
};
($ty:ty) => {

40
tests/scalar.rs Normal file
View File

@ -0,0 +1,40 @@
use async_graphql::*;
mod test_mod {
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct MyValue {
a: i32,
}
}
#[async_std::test]
pub async fn test_scalar_macro() {
scalar!(test_mod::MyValue, "MV", "DESC");
struct Query;
#[Object]
impl Query {
async fn value(&self) -> test_mod::MyValue {
todo!()
}
}
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
assert_eq!(
schema
.execute(r#"{ __type(name:"MV") { name description } }"#)
.await
.into_result()
.unwrap()
.data,
value!({
"__type": {
"name": "MV",
"description": "DESC",
}
})
);
}