diff --git a/async-graphql-actix-web/src/lib.rs b/async-graphql-actix-web/src/lib.rs index 3b8cef19..224e295b 100644 --- a/async-graphql-actix-web/src/lib.rs +++ b/async-graphql-actix-web/src/lib.rs @@ -243,7 +243,7 @@ where .map_err(actix_web::error::ErrorBadRequest)? }; - let mut builder = match gql_request.into_query_builder(schema).await { + let mut builder = match gql_request.into_query_builder(schema) { Ok(builder) => builder, Err(err) => return Ok(web::Json(GQLResponse(Err(err))).respond_to(&req).await?), }; @@ -319,7 +319,7 @@ where let gql_request = web::Json::::from_request(&req, &mut payload.0) .await? .into_inner(); - let mut builder = match gql_request.into_query_builder(schema).await { + let mut builder = match gql_request.into_query_builder(schema) { Ok(builder) => builder, Err(err) => return Ok(web::Json(GQLResponse(Err(err))).respond_to(&req).await?), }; diff --git a/examples/actix-web.rs b/examples/actix-web.rs index 9a097eb8..e816b0d3 100644 --- a/examples/actix-web.rs +++ b/examples/actix-web.rs @@ -9,8 +9,7 @@ type StarWarsSchema = Schema, req: web::Json) -> web::Json { web::Json(GQLResponse( - req.into_inner() - .into_query_builder(&s) + futures::future::ready(req.into_inner().into_query_builder(&s)) .and_then(|builder| builder.execute()) .await, )) diff --git a/examples/error_extensions.rs b/examples/error_extensions.rs index fe1af5be..87b686a6 100644 --- a/examples/error_extensions.rs +++ b/examples/error_extensions.rs @@ -76,8 +76,7 @@ async fn index( req: web::Json, ) -> web::Json { web::Json(GQLResponse( - req.into_inner() - .into_query_builder(&s) + futures::future::ready(req.into_inner().into_query_builder(&s)) .and_then(|builder| builder.execute()) .await, )) diff --git a/examples/tide.rs b/examples/tide.rs index 888f1ec5..db973e3a 100644 --- a/examples/tide.rs +++ b/examples/tide.rs @@ -11,8 +11,7 @@ type StarWarsSchema = Schema) -> Response { let gql_request: GQLRequest = request.body_json().await.unwrap(); let schema = request.state(); - let gql_response = gql_request - .into_query_builder(schema) + let gql_response = futures::future::ready(gql_request.into_query_builder(schema)) .and_then(|builder| builder.execute()) .await; Response::new(200) diff --git a/src/http/mod.rs b/src/http/mod.rs index 573741de..1106b825 100644 --- a/src/http/mod.rs +++ b/src/http/mod.rs @@ -30,7 +30,7 @@ pub struct GQLRequest { impl GQLRequest { /// Into query builder, you can set other parameters or execute queries immediately. - pub async fn into_query_builder( + pub fn into_query_builder( self, schema: &Schema, ) -> Result> diff --git a/src/lib.rs b/src/lib.rs index 2cada826..cb900062 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -220,13 +220,13 @@ pub use types::{EnumItem, EnumType}; /// #[async_std::main] /// async fn main() { /// let schema = Schema::new(QueryRoot{ value: 10 }, EmptyMutation, EmptySubscription); -/// let res = schema.query(r#"{ +/// let res = schema.execute(r#"{ /// value /// valueRef /// valueWithError /// valueWithArg1: valueWithArg /// valueWithArg2: valueWithArg(a: 99) -/// }"#).unwrap().execute().await.unwrap().data; +/// }"#).await.unwrap().data; /// assert_eq!(res, serde_json::json!({ /// "value": 10, /// "valueRef": 10, @@ -273,7 +273,7 @@ pub use async_graphql_derive::Object; /// #[async_std::main] /// async fn main() { /// let schema = Schema::new(QueryRoot{ value: 10 }, EmptyMutation, EmptySubscription); -/// let res = schema.query("{ value }").unwrap().execute().await.unwrap().data; +/// let res = schema.execute("{ value }").await.unwrap().data; /// assert_eq!(res, serde_json::json!({ /// "value": 10, /// })); @@ -330,7 +330,7 @@ pub use async_graphql_derive::SimpleObject; /// #[async_std::main] /// async fn main() { /// let schema = Schema::new(QueryRoot{ value1: MyEnum::A, value2: MyEnum::B }, EmptyMutation, EmptySubscription); -/// let res = schema.query("{ value1 value2 }").unwrap().execute().await.unwrap().data; +/// let res = schema.execute("{ value1 value2 }").await.unwrap().data; /// assert_eq!(res, serde_json::json!({ "value1": "A", "value2": "b" })); /// } /// ``` @@ -379,11 +379,11 @@ pub use async_graphql_derive::Enum; /// #[async_std::main] /// async fn main() { /// let schema = Schema::new(QueryRoot, EmptyMutation, EmptySubscription); -/// let res = schema.query(r#" +/// let res = schema.execute(r#" /// { /// value1: value(input:{a:9, b:3}) /// value2: value(input:{a:9}) -/// }"#).unwrap().execute().await.unwrap().data; +/// }"#).await.unwrap().data; /// assert_eq!(res, serde_json::json!({ "value1": 27, "value2": 90 })); /// } /// ``` @@ -484,14 +484,14 @@ pub use async_graphql_derive::InputObject; /// #[async_std::main] /// async fn main() { /// let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription).data("hello".to_string()).finish(); -/// let res = schema.query(r#" +/// let res = schema.execute(r#" /// { /// typeA { /// valueA /// valueB /// valueC(a: 3, b: 2) /// } -/// }"#).unwrap().execute().await.unwrap().data; +/// }"#).await.unwrap().data; /// assert_eq!(res, serde_json::json!({ /// "typeA": { /// "valueA": "hello", diff --git a/src/schema.rs b/src/schema.rs index 946395ea..ee61fe26 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -7,12 +7,12 @@ use crate::subscription::{SubscriptionConnectionBuilder, SubscriptionStub, Subsc use crate::types::QueryRoot; use crate::validation::{check_rules, CheckResult}; use crate::{ - ContextSelectionSet, Error, ObjectType, Pos, QueryError, Result, SubscriptionType, Type, - Variables, + ContextSelectionSet, Error, ObjectType, Pos, QueryError, QueryResponse, Result, + SubscriptionType, Type, Variables, }; use futures::channel::mpsc; use futures::lock::Mutex; -use futures::SinkExt; +use futures::{SinkExt, TryFutureExt}; use graphql_parser::parse_query; use graphql_parser::query::{ Definition, Field, FragmentDefinition, OperationDefinition, Selection, @@ -253,6 +253,13 @@ where }) } + /// Execute query without create the `QueryBuilder`. + pub async fn execute(&self, source: &str) -> Result { + futures::future::ready(self.query(source)) + .and_then(|builder| builder.execute()) + .await + } + /// Create subscription stub, typically called inside the `SubscriptionTransport::handle_request` method/ pub fn create_subscription_stub( &self, diff --git a/src/types/connection/mod.rs b/src/types/connection/mod.rs index 001f6e53..b6a967b2 100644 --- a/src/types/connection/mod.rs +++ b/src/types/connection/mod.rs @@ -106,7 +106,7 @@ impl EmptyEdgeFields {} /// async fn main() { /// let schema = Schema::new(QueryRoot, EmptyMutation, EmptySubscription); /// -/// assert_eq!(schema.query("{ numbers(first: 2) { edges { node } } }").unwrap().execute().await.unwrap().data, serde_json::json!({ +/// assert_eq!(schema.execute("{ numbers(first: 2) { edges { node } } }").await.unwrap().data, serde_json::json!({ /// "numbers": { /// "edges": [ /// {"node": 0}, @@ -115,7 +115,7 @@ impl EmptyEdgeFields {} /// }, /// })); /// -/// assert_eq!(schema.query("{ numbers(last: 2) { edges { node diff } } }").unwrap().execute().await.unwrap().data, serde_json::json!({ +/// assert_eq!(schema.execute("{ numbers(last: 2) { edges { node diff } } }").await.unwrap().data, serde_json::json!({ /// "numbers": { /// "edges": [ /// {"node": -2, "diff": -1002}, diff --git a/tests/enum.rs b/tests/enum.rs index 6f8bb9f5..9fbf9712 100644 --- a/tests/enum.rs +++ b/tests/enum.rs @@ -44,7 +44,7 @@ pub async fn test_enum_type() { "# ); assert_eq!( - schema.query(&query).unwrap().execute().await.unwrap().data, + schema.execute(&query).await.unwrap().data, serde_json::json!({ "value": "A", "testArg": "A", diff --git a/tests/input_object.rs b/tests/input_object.rs index b571c685..d0e73450 100644 --- a/tests/input_object.rs +++ b/tests/input_object.rs @@ -81,7 +81,7 @@ pub async fn test_input_object_default_value() { }}"# ); assert_eq!( - schema.query(&query).unwrap().execute().await.unwrap().data, + schema.execute(&query).await.unwrap().data, serde_json::json!({ "a": { "a": 999, diff --git a/tests/list.rs b/tests/list.rs index 25b58f0f..c3509365 100644 --- a/tests/list.rs +++ b/tests/list.rs @@ -52,7 +52,7 @@ pub async fn test_list_type() { json_value ); assert_eq!( - schema.query(&query).unwrap().execute().await.unwrap().data, + schema.execute(&query).await.unwrap().data, serde_json::json!({ "valueVec": vec![1, 2, 3, 4, 5], "valueSlice": vec![1, 2, 3, 4, 5], diff --git a/tests/optional.rs b/tests/optional.rs index c86b1f74..d345b744 100644 --- a/tests/optional.rs +++ b/tests/optional.rs @@ -66,7 +66,7 @@ pub async fn test_optional_type() { }}"# ); assert_eq!( - schema.query(&query).unwrap().execute().await.unwrap().data, + schema.execute(&query).await.unwrap().data, serde_json::json!({ "value1": 10, "value1Ref": 10, diff --git a/tests/scalars.rs b/tests/scalars.rs index 3fc15a55..ddff4b9e 100644 --- a/tests/scalars.rs +++ b/tests/scalars.rs @@ -35,7 +35,7 @@ macro_rules! test_scalars { let json_value: serde_json::Value = $value.into(); let query = format!("{{ value testArg(input: {0}) testInput(input: {{value: {0}}}) }}", json_value); assert_eq!( - schema.query(&query).unwrap().execute().await.unwrap().data, + schema.execute(&query).await.unwrap().data, serde_json::json!({ "value": $value, "testArg": $value, "testInput": $value }) ); }