diff --git a/integration_tests/juniper_tests/src/issue_372.rs b/integration_tests/juniper_tests/src/issue_372.rs new file mode 100644 index 00000000..a78cf0d1 --- /dev/null +++ b/integration_tests/juniper_tests/src/issue_372.rs @@ -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 { + 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"))), + ); + } + } +} diff --git a/integration_tests/juniper_tests/src/lib.rs b/integration_tests/juniper_tests/src/lib.rs index f94c4d72..3a07b3fc 100644 --- a/integration_tests/juniper_tests/src/lib.rs +++ b/integration_tests/juniper_tests/src/lib.rs @@ -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;