Ensure operation roots resolve their __typename (#372)

This commit is contained in:
tyranron 2021-07-21 19:14:13 +03:00
parent ad64025572
commit d211f4a8ef
No known key found for this signature in database
GPG key ID: 762E144FB230A4F0
2 changed files with 146 additions and 0 deletions

View file

@ -0,0 +1,144 @@
use futures::{stream, StreamExt as _};
use juniper::{graphql_object, graphql_subscription, graphql_value, RootNode, Value, Variables};
pub struct Query;
#[graphql_object]
impl Query {
fn some() -> bool {
true
}
}
pub struct Mutation;
#[graphql_object]
impl Mutation {
fn another() -> bool {
false
}
}
pub struct Subscription;
#[graphql_subscription]
impl Subscription {
async fn another() -> stream::Empty<bool> {
stream::empty()
}
}
#[tokio::test]
async fn implicit_query_typename() {
let query = r#"{ __typename }"#;
let schema = RootNode::new(Query, Mutation, Subscription);
let (res, errors) = juniper::execute(query, None, &schema, &Variables::new(), &())
.await
.unwrap();
assert_eq!(errors.len(), 0);
assert_eq!(res, graphql_value!({"__typename": "Query"}));
}
#[tokio::test]
async fn query_typename() {
let query = r#"query { __typename }"#;
let schema = RootNode::new(Query, Mutation, Subscription);
let (res, errors) = juniper::execute(query, None, &schema, &Variables::new(), &())
.await
.unwrap();
assert_eq!(errors.len(), 0);
assert_eq!(res, graphql_value!({"__typename": "Query"}));
}
#[tokio::test]
async fn explicit_query_typename() {
let query = r#"query Query { __typename }"#;
let schema = RootNode::new(Query, Mutation, Subscription);
let (res, errors) = juniper::execute(query, None, &schema, &Variables::new(), &())
.await
.unwrap();
assert_eq!(errors.len(), 0);
assert_eq!(res, graphql_value!({"__typename": "Query"}));
}
#[tokio::test]
async fn mutation_typename() {
let query = r#"mutation { __typename }"#;
let schema = RootNode::new(Query, Mutation, Subscription);
let (res, errors) = juniper::execute(query, None, &schema, &Variables::new(), &())
.await
.unwrap();
assert_eq!(errors.len(), 0);
assert_eq!(res, graphql_value!({"__typename": "Mutation"}));
}
#[tokio::test]
async fn explicit_mutation_typename() {
let query = r#"mutation Mutation { __typename }"#;
let schema = RootNode::new(Query, Mutation, Subscription);
let (res, errors) = juniper::execute(query, None, &schema, &Variables::new(), &())
.await
.unwrap();
assert_eq!(errors.len(), 0);
assert_eq!(res, graphql_value!({"__typename": "Mutation"}));
}
#[tokio::test]
async fn subscription_typename() {
let query = r#"subscription { __typename }"#;
let schema = RootNode::new(Query, Mutation, Subscription);
let (res, errors) = juniper::resolve_into_stream(query, None, &schema, &Variables::new(), &())
.await
.unwrap();
assert_eq!(errors.len(), 0);
assert!(matches!(res, Value::Object(_)));
if let Value::Object(mut obj) = res {
assert!(obj.contains_field("__typename"));
let val = obj.get_mut_field_value("__typename").unwrap();
assert!(matches!(val, Value::Scalar(_)));
if let Value::Scalar(ref mut stream) = val {
assert_eq!(
stream.next().await,
Some(Ok(graphql_value!("Subscription"))),
);
}
}
}
#[tokio::test]
async fn explicit_subscription_typename() {
let query = r#"subscription Subscription { __typename }"#;
let schema = RootNode::new(Query, Mutation, Subscription);
let (res, errors) = juniper::resolve_into_stream(query, None, &schema, &Variables::new(), &())
.await
.unwrap();
assert_eq!(errors.len(), 0);
assert!(matches!(res, Value::Object(_)));
if let Value::Object(mut obj) = res {
assert!(obj.contains_field("__typename"));
let val = obj.get_mut_field_value("__typename").unwrap();
assert!(matches!(val, Value::Scalar(_)));
if let Value::Scalar(ref mut stream) = val {
assert_eq!(
stream.next().await,
Some(Ok(graphql_value!("Subscription"))),
);
}
}
}

View file

@ -13,6 +13,8 @@ mod infallible_as_field_error;
#[cfg(test)]
mod issue_371;
#[cfg(test)]
mod issue_372;
#[cfg(test)]
mod issue_398;
#[cfg(test)]
mod issue_407;