async-graphql/tests/federation.rs

149 lines
3.1 KiB
Rust
Raw Normal View History

2020-05-01 23:57:34 +00:00
#![allow(unreachable_code)]
2020-04-09 14:03:09 +00:00
use async_graphql::*;
#[async_std::test]
pub async fn test_nested_key() {
#[derive(InputObject)]
struct MyInputA {
a: i32,
b: i32,
c: MyInputB,
2020-04-09 14:03:09 +00:00
}
#[derive(InputObject)]
struct MyInputB {
v: i32,
2020-04-09 14:03:09 +00:00
}
assert_eq!(MyInputB::federation_fields().as_deref(), Some("{ v }"));
assert_eq!(
MyInputA::federation_fields().as_deref(),
Some("{ a b c { v } }")
);
2020-04-09 14:03:09 +00:00
struct QueryRoot;
2020-04-09 14:03:09 +00:00
#[derive(SimpleObject)]
struct MyObj {
a: i32,
b: i32,
c: i32,
2020-04-09 14:03:09 +00:00
}
#[Object]
impl QueryRoot {
#[graphql(entity)]
async fn find_obj(&self, input: MyInputA) -> MyObj {
MyObj {
a: input.a,
b: input.b,
c: input.c.v,
}
}
2020-04-09 14:03:09 +00:00
}
let schema = Schema::new(QueryRoot, EmptyMutation, EmptySubscription);
let query = r#"{
_entities(representations: [{__typename: "MyObj", input: {a: 1, b: 2, c: { v: 3 }}}]) {
__typename
... on MyObj {
a b c
}
}
}"#;
assert_eq!(
schema.execute(query).await.into_result().unwrap().data,
value!({
"_entities": [
{"__typename": "MyObj", "a": 1, "b": 2, "c": 3},
]
})
);
2020-04-09 14:03:09 +00:00
}
#[async_std::test]
pub async fn test_federation() {
struct User {
id: ID,
2020-04-09 14:03:09 +00:00
}
#[Object(extends)]
impl User {
#[graphql(external)]
async fn id(&self) -> &ID {
&self.id
}
async fn reviews(&self) -> Vec<Review> {
todo!()
}
2020-04-09 14:03:09 +00:00
}
struct Review;
#[Object]
impl Review {
async fn body(&self) -> String {
todo!()
}
2020-04-09 14:03:09 +00:00
async fn author(&self) -> User {
todo!()
}
async fn product(&self) -> Product {
todo!()
}
2020-04-09 14:03:09 +00:00
}
struct Product {
upc: String,
}
#[Object(extends)]
impl Product {
#[graphql(external)]
async fn upc(&self) -> &str {
&self.upc
}
async fn reviews(&self) -> Vec<Review> {
todo!()
}
}
struct QueryRoot;
#[Object]
impl QueryRoot {
#[graphql(entity)]
async fn find_user_by_id(&self, id: ID) -> User {
User { id }
}
#[graphql(entity)]
async fn find_product_by_upc(&self, upc: String) -> Product {
Product { upc }
}
2020-04-09 14:03:09 +00:00
}
let schema = Schema::new(QueryRoot, EmptyMutation, EmptySubscription);
2020-05-01 23:57:34 +00:00
let query = r#"{
_entities(representations: [{__typename: "Product", upc: "B00005N5PF"}]) {
2020-04-09 14:03:09 +00:00
__typename
2020-05-01 23:57:34 +00:00
... on Product {
2020-04-09 14:03:09 +00:00
upc
2020-05-01 23:57:34 +00:00
}
}
}"#;
2020-04-09 14:03:09 +00:00
assert_eq!(
2020-09-12 09:29:52 +00:00
schema.execute(query).await.into_result().unwrap().data,
value!({
2020-04-09 14:03:09 +00:00
"_entities": [
{"__typename": "Product", "upc": "B00005N5PF"},
]
})
);
}