async-graphql/tests/interface.rs

334 lines
6.7 KiB
Rust
Raw Normal View History

2020-04-20 06:37:28 +00:00
use async_graphql::*;
#[async_std::test]
pub async fn test_interface_simple_object() {
#[derive(SimpleObject)]
2020-04-22 05:30:41 +00:00
struct MyObj {
id: i32,
title: String,
}
#[derive(Interface)]
#[graphql(field(name = "id", type = "&i32"))]
2020-05-11 03:25:49 +00:00
enum Node {
MyObj(MyObj),
}
struct Query;
#[Object]
impl Query {
async fn node(&self) -> Node {
MyObj {
id: 33,
title: "haha".to_string(),
}
.into()
}
}
2020-04-27 04:57:52 +00:00
let query = r#"{
node {
... on Node {
id
2020-04-27 04:57:52 +00:00
}
}
}"#;
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
assert_eq!(
schema.execute(query).await.into_result().unwrap().data,
value!({
"node": {
"id": 33,
}
})
);
}
#[async_std::test]
pub async fn test_interface_simple_object2() {
#[derive(SimpleObject)]
2020-04-22 05:30:41 +00:00
struct MyObj {
id: i32,
title: String,
}
#[derive(Interface)]
#[graphql(field(name = "id", type = "&i32"))]
2020-05-11 03:25:49 +00:00
enum Node {
MyObj(MyObj),
}
struct Query;
#[Object]
impl Query {
async fn node(&self) -> Node {
MyObj {
id: 33,
title: "haha".to_string(),
}
2020-04-21 07:40:31 +00:00
.into()
}
}
2020-04-27 04:57:52 +00:00
let query = r#"{
node {
... on Node {
id
2020-04-27 04:57:52 +00:00
}
}
}"#;
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
assert_eq!(
schema.execute(query).await.into_result().unwrap().data,
value!({
"node": {
"id": 33,
}
})
);
}
2020-04-20 06:37:28 +00:00
#[async_std::test]
pub async fn test_multiple_interfaces() {
struct MyObj;
#[Object]
2020-04-20 06:37:28 +00:00
impl MyObj {
async fn value_a(&self) -> i32 {
1
}
async fn value_b(&self) -> i32 {
2
}
async fn value_c(&self) -> i32 {
3
}
}
#[derive(Interface)]
#[graphql(field(name = "value_a", type = "i32"))]
2020-05-11 03:25:49 +00:00
enum InterfaceA {
MyObj(MyObj),
}
2020-04-20 06:37:28 +00:00
#[derive(Interface)]
#[graphql(field(name = "value_b", type = "i32"))]
2020-05-11 03:25:49 +00:00
enum InterfaceB {
MyObj(MyObj),
}
2020-04-20 06:37:28 +00:00
struct Query;
#[Object]
2020-04-20 06:37:28 +00:00
impl Query {
async fn my_obj(&self) -> InterfaceB {
MyObj.into()
}
}
let schema = Schema::build(Query, EmptyMutation, EmptySubscription)
2020-04-21 12:54:38 +00:00
.register_type::<InterfaceA>() // `InterfaceA` is not directly referenced, so manual registration is required.
.finish();
2020-04-27 04:57:52 +00:00
let query = r#"{
myObj {
... on InterfaceA {
2020-04-20 06:37:28 +00:00
valueA
2020-04-27 04:57:52 +00:00
}
... on InterfaceB {
2020-04-20 06:37:28 +00:00
valueB
2020-04-27 04:57:52 +00:00
}
... on MyObj {
2020-04-20 06:37:28 +00:00
valueC
2020-04-27 04:57:52 +00:00
}
}
}"#;
2020-04-20 06:37:28 +00:00
assert_eq!(
schema.execute(query).await.into_result().unwrap().data,
value!({
2020-04-20 06:37:28 +00:00
"myObj": {
"valueA": 1,
"valueB": 2,
"valueC": 3,
}
})
);
}
2020-04-22 05:18:36 +00:00
#[async_std::test]
2020-04-22 07:03:41 +00:00
pub async fn test_multiple_objects_in_multiple_interfaces() {
struct MyObjOne;
#[Object]
2020-04-22 07:03:41 +00:00
impl MyObjOne {
async fn value_a(&self) -> i32 {
1
}
async fn value_b(&self) -> i32 {
2
}
async fn value_c(&self) -> i32 {
3
}
}
struct MyObjTwo;
#[Object]
2020-04-22 07:03:41 +00:00
impl MyObjTwo {
async fn value_a(&self) -> i32 {
1
}
}
#[derive(Interface)]
#[graphql(field(name = "value_a", type = "i32"))]
2020-05-11 03:25:49 +00:00
enum InterfaceA {
MyObjOne(MyObjOne),
MyObjTwo(MyObjTwo),
}
2020-04-22 07:03:41 +00:00
#[derive(Interface)]
#[graphql(field(name = "value_b", type = "i32"))]
2020-05-11 03:25:49 +00:00
enum InterfaceB {
MyObjOne(MyObjOne),
}
2020-04-22 07:03:41 +00:00
struct Query;
#[Object]
2020-04-22 07:03:41 +00:00
impl Query {
async fn my_obj(&self) -> Vec<InterfaceA> {
vec![MyObjOne.into(), MyObjTwo.into()]
}
}
let schema = Schema::build(Query, EmptyMutation, EmptySubscription)
.register_type::<InterfaceB>() // `InterfaceB` is not directly referenced, so manual registration is required.
.finish();
2020-04-27 04:57:52 +00:00
let query = r#"{
myObj {
... on InterfaceA {
valueA
2020-04-27 04:57:52 +00:00
}
... on InterfaceB {
valueB
2020-04-27 04:57:52 +00:00
}
... on MyObjOne {
valueC
2020-04-27 04:57:52 +00:00
}
}
}"#;
2020-04-22 07:03:41 +00:00
assert_eq!(
schema.execute(query).await.into_result().unwrap().data,
value!({
2020-04-22 07:03:41 +00:00
"myObj": [{
"valueA": 1,
"valueB": 2,
"valueC": 3,
}, {
"valueA": 1
}]
})
);
}
2020-04-22 05:30:41 +00:00
#[async_std::test]
pub async fn test_interface_field_result() {
struct MyObj;
2020-04-22 05:18:36 +00:00
#[Object]
2020-04-22 05:30:41 +00:00
impl MyObj {
2020-10-14 02:25:41 +00:00
async fn value(&self) -> FieldResult<i32> {
2020-04-22 05:30:41 +00:00
Ok(10)
2020-04-22 05:18:36 +00:00
}
}
#[derive(Interface)]
2020-10-14 02:25:41 +00:00
#[graphql(field(name = "value", type = "i32"))]
2020-05-11 03:25:49 +00:00
enum Node {
MyObj(MyObj),
}
2020-04-22 05:18:36 +00:00
struct Query;
#[Object]
2020-04-22 05:18:36 +00:00
impl Query {
2020-04-22 05:30:41 +00:00
async fn node(&self) -> Node {
MyObj.into()
2020-04-22 05:18:36 +00:00
}
}
2020-04-27 04:57:52 +00:00
let query = r#"{
node {
... on Node {
2020-04-22 05:30:41 +00:00
value
2020-04-27 04:57:52 +00:00
}
}
}"#;
2020-04-22 05:30:41 +00:00
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
2020-04-22 05:18:36 +00:00
assert_eq!(
schema.execute(query).await.into_result().unwrap().data,
value!({
2020-04-22 05:30:41 +00:00
"node": {
"value": 10,
}
2020-04-22 05:18:36 +00:00
})
);
}
#[async_std::test]
pub async fn test_interface_field_method() {
struct A;
#[Object]
impl A {
2020-09-28 09:44:00 +00:00
#[graphql(name = "created_at")]
pub async fn created_at(&self) -> i32 {
1
}
}
struct B;
#[Object]
impl B {
2020-09-28 09:44:00 +00:00
#[graphql(name = "created_at")]
pub async fn created_at(&self) -> i32 {
2
}
}
#[derive(Interface)]
#[graphql(field(name = "created_at", method = "created_at", type = "i32"))]
enum MyInterface {
A(A),
B(B),
}
struct Query;
#[Object]
impl Query {
async fn test(&self) -> MyInterface {
A.into()
}
}
let query = "{ test { created_at } }";
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
assert_eq!(
schema.execute(query).await.into_result().unwrap().data,
value!({
"test": {
"created_at": 1,
}
})
);
}