2020-03-07 04:40:04 +00:00
|
|
|
use async_graphql::*;
|
|
|
|
|
2021-03-12 04:47:24 +00:00
|
|
|
#[tokio::test]
|
2020-03-07 04:40:04 +00:00
|
|
|
pub async fn test_enum_type() {
|
2020-09-18 00:52:13 +00:00
|
|
|
#[derive(Enum, Copy, Clone, Eq, PartialEq)]
|
2020-03-07 04:40:04 +00:00
|
|
|
enum MyEnum {
|
|
|
|
A,
|
|
|
|
B,
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[derive(InputObject)]
|
2020-03-07 04:40:04 +00:00
|
|
|
struct MyInput {
|
|
|
|
value: MyEnum,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Root {
|
|
|
|
value: MyEnum,
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[Object]
|
2020-03-07 04:40:04 +00:00
|
|
|
impl Root {
|
|
|
|
async fn value(&self) -> MyEnum {
|
|
|
|
self.value
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn test_arg(&self, input: MyEnum) -> MyEnum {
|
|
|
|
input
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn test_input<'a>(&self, input: MyInput) -> MyEnum {
|
|
|
|
input.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 09:20:12 +00:00
|
|
|
let schema = Schema::new(Root { value: MyEnum::A }, EmptyMutation, EmptySubscription);
|
2020-09-01 01:10:12 +00:00
|
|
|
let query = r#"{
|
2020-03-07 04:40:04 +00:00
|
|
|
value
|
2020-03-09 12:00:57 +00:00
|
|
|
testArg(input: A)
|
2020-09-01 01:10:12 +00:00
|
|
|
testInput(input: {value: B})
|
|
|
|
}"#
|
2020-08-31 18:18:02 +00:00
|
|
|
.to_owned();
|
2020-03-07 04:40:04 +00:00
|
|
|
assert_eq!(
|
2020-09-10 11:35:48 +00:00
|
|
|
schema.execute(&query).await.data,
|
2020-10-12 02:17:05 +00:00
|
|
|
value!({
|
2020-03-07 04:40:04 +00:00
|
|
|
"value": "A",
|
2020-03-09 12:00:57 +00:00
|
|
|
"testArg": "A",
|
|
|
|
"testInput": "B",
|
2020-03-07 04:40:04 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2020-04-08 01:05:54 +00:00
|
|
|
|
2021-03-12 04:47:24 +00:00
|
|
|
#[tokio::test]
|
2020-04-08 01:05:54 +00:00
|
|
|
pub async fn test_enum_derive_and_item_attributes() {
|
2020-08-31 18:18:02 +00:00
|
|
|
use serde::Deserialize;
|
2020-04-08 01:05:54 +00:00
|
|
|
|
2020-09-18 00:52:13 +00:00
|
|
|
#[derive(Deserialize, Debug, Enum, Copy, Clone, Eq, PartialEq)]
|
2020-04-08 01:05:54 +00:00
|
|
|
enum Test {
|
|
|
|
#[serde(alias = "Other")]
|
|
|
|
Real,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, PartialEq, Debug)]
|
|
|
|
#[allow(dead_code)]
|
|
|
|
struct TestStruct {
|
|
|
|
value: Test,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(
|
2020-10-13 02:19:30 +00:00
|
|
|
from_value::<TestStruct>(value!({"value": "Other"})).unwrap(),
|
2020-04-08 01:05:54 +00:00
|
|
|
TestStruct { value: Test::Real }
|
|
|
|
);
|
|
|
|
}
|
2020-09-27 08:05:25 +00:00
|
|
|
|
2021-03-12 04:47:24 +00:00
|
|
|
#[tokio::test]
|
2020-09-27 08:05:25 +00:00
|
|
|
pub async fn test_remote_enum() {
|
|
|
|
#[derive(Enum, Copy, Clone, Eq, PartialEq)]
|
|
|
|
#[graphql(remote = "remote::RemoteEnum")]
|
|
|
|
enum LocalEnum {
|
|
|
|
A,
|
|
|
|
B,
|
|
|
|
C,
|
|
|
|
}
|
|
|
|
|
|
|
|
mod remote {
|
|
|
|
pub enum RemoteEnum {
|
|
|
|
A,
|
|
|
|
B,
|
|
|
|
C,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let _: remote::RemoteEnum = LocalEnum::A.into();
|
|
|
|
let _: LocalEnum = remote::RemoteEnum::A.into();
|
|
|
|
}
|