#550: add unit test for generic Object/Simple/ComplexObject

This commit is contained in:
Julien Nicoulaud 2021-07-03 18:00:16 +02:00
parent 854529c330
commit 32d75ec4c9
No known key found for this signature in database
GPG Key ID: 9E76BFBEDAB0B521

View File

@ -1,4 +1,5 @@
use async_graphql::*;
use core::marker::PhantomData;
#[tokio::test]
pub async fn test_complex_object() {
@ -64,3 +65,62 @@ pub async fn test_complex_object() {
})
);
}
#[tokio::test]
pub async fn test_complex_object_with_generic_context_data() {
trait MyData: Send + Sync {
fn answer(&self) -> i64;
}
struct DefaultMyData {}
impl MyData for DefaultMyData {
fn answer(&self) -> i64 { 42 }
}
struct MyQuery<D: MyData> {
marker: PhantomData<D>,
}
#[Object]
impl<D> MyQuery<D> where D: 'static + MyData {
#[graphql(skip)]
pub fn new() -> Self {
Self { marker: PhantomData }
}
async fn obj(&self, ctx: &Context<'_>) -> MyObject<D> {
MyObject::new(ctx.data_unchecked::<D>().answer())
}
}
#[derive(SimpleObject, Debug, Clone, Hash, Eq, PartialEq)]
#[graphql(complex)]
struct MyObject<D: MyData> {
my_val: i64,
#[graphql(skip)]
marker: PhantomData<D>,
}
#[ComplexObject]
impl<D: MyData> MyObject<D> {
#[graphql(skip)]
pub fn new(my_val: i64) -> Self {
Self { my_val, marker: PhantomData }
}
}
let schema = Schema::build(MyQuery::<DefaultMyData>::new(), EmptyMutation, EmptySubscription)
.data(DefaultMyData {})
.finish();
assert_eq!(
schema.execute("{ obj { myVal } }").await.data,
value!({
"obj": {
"myVal": 42,
}
})
);
}