async-graphql/tests/input_object.rs

107 lines
2.1 KiB
Rust
Raw Normal View History

2020-03-07 12:54:03 +00:00
use async_graphql::*;
#[async_std::test]
pub async fn test_input_object_default_value() {
#[InputObject]
struct MyInput {
#[field(default = 999)]
2020-03-07 12:54:03 +00:00
a: i32,
#[field(default_with = "vec![1, 2, 3]")]
2020-03-07 12:54:03 +00:00
b: Vec<i32>,
#[field(default = "abc")]
2020-03-07 12:54:03 +00:00
c: String,
#[field(default = 999)]
d: i32,
2020-03-07 12:54:03 +00:00
#[field(default = 999)]
e: i32,
2020-03-07 12:54:03 +00:00
}
struct MyOutput {
a: i32,
b: Vec<i32>,
c: String,
d: Option<i32>,
e: Option<i32>,
}
#[Object]
impl MyOutput {
async fn a(&self) -> i32 {
self.a
}
async fn b(&self) -> &Vec<i32> {
&self.b
}
async fn c(&self) -> &String {
&self.c
}
async fn d(&self) -> &Option<i32> {
&self.d
}
async fn e(&self) -> &Option<i32> {
&self.e
}
}
struct Root;
#[Object]
impl Root {
async fn a(&self, input: MyInput) -> MyOutput {
MyOutput {
a: input.a,
b: input.b,
c: input.c,
d: Some(input.d),
e: Some(input.e),
2020-03-07 12:54:03 +00:00
}
}
}
2020-03-19 09:20:12 +00:00
let schema = Schema::new(Root, EmptyMutation, EmptySubscription);
2020-09-01 01:10:12 +00:00
let query = r#"{
a(input:{e:777}) {
2020-03-07 12:54:03 +00:00
a b c d e
2020-09-01 01:10:12 +00:00
}
}"#
.to_owned();
2020-03-07 12:54:03 +00:00
assert_eq!(
2020-04-02 04:53:53 +00:00
schema.execute(&query).await.unwrap().data,
2020-03-07 12:54:03 +00:00
serde_json::json!({
"a": {
"a": 999,
"b": [1, 2, 3],
"c": "abc",
"d": 999,
2020-03-09 01:33:36 +00:00
"e": 777,
2020-03-07 12:54:03 +00:00
}
})
);
}
#[async_std::test]
pub async fn test_inputobject_derive_and_item_attributes() {
use serde::Deserialize;
#[async_graphql::InputObject]
#[derive(Deserialize, PartialEq, Debug)]
struct MyInputObject {
#[field]
#[serde(alias = "other")]
real: i32,
}
assert_eq!(
serde_json::from_str::<MyInputObject>(r#"{ "other" : 100 }"#).unwrap(),
MyInputObject { real: 100 }
);
}