Skip to content

Commit

Permalink
Add tests for #916
Browse files Browse the repository at this point in the history
  • Loading branch information
sunli829 committed May 7, 2022
1 parent f2bac81 commit 336451f
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions tests/oneof_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,53 @@ async fn test_oneof_object_validation() {
r#"Failed to parse "Int": the value is 20, must be less than or equal to 10 (occurred while parsing "MyOneofObj")"#
);
}

#[tokio::test]
async fn test_oneof_object_vec() {
use async_graphql::*;

#[derive(SimpleObject)]
pub struct User {
name: String,
}

#[derive(OneofObject)]
pub enum UserBy {
Email(String),
RegistrationNumber(i64),
}

pub struct Query;

#[Object]
impl Query {
async fn search_users(&self, by: Vec<UserBy>) -> Vec<String> {
by.into_iter()
.map(|user| match user {
UserBy::Email(email) => email,
UserBy::RegistrationNumber(id) => format!("{}", id),
})
.collect()
}
}

let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
let query = r#"
{
searchUsers(by: [
{ email: "a@a.com" },
{ registrationNumber: 100 },
{ registrationNumber: 200 },
])
}
"#;
let data = schema.execute(query).await.into_result().unwrap().data;
assert_eq!(
data,
value!({
"searchUsers": [
"a@a.com", "100", "200"
]
})
);
}

0 comments on commit 336451f

Please sign in to comment.