diff --git a/async-graphql-tide/Cargo.toml b/async-graphql-tide/Cargo.toml index fae5bf62..1ca95fa3 100644 --- a/async-graphql-tide/Cargo.toml +++ b/async-graphql-tide/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "async-graphql-tide" -version = "1.4.4" +version = "1.4.5" authors = ["vkill "] edition = "2018" description = "async-graphql for tide" @@ -16,9 +16,9 @@ categories = ["network-programming", "asynchronous"] async-graphql = { path = "..", version = "1.12.4" } tide = "0.8" async-trait = "0.1.30" +serde_json = "1.0.51" [dev-dependencies] async-std = "1.5.0" -serde_json = "1.0.51" smol = { version = "0.1", features = ["tokio02"] } reqwest = "0.10.4" diff --git a/async-graphql-tide/src/lib.rs b/async-graphql-tide/src/lib.rs index 5564af46..4abfef61 100644 --- a/async-graphql-tide/src/lib.rs +++ b/async-graphql-tide/src/lib.rs @@ -6,8 +6,8 @@ use async_graphql::http::GQLResponse; use async_graphql::{ - IntoQueryBuilder, IntoQueryBuilderOpts, ObjectType, ParseRequestError, QueryBuilder, Schema, - SubscriptionType, + IntoQueryBuilder, IntoQueryBuilderOpts, ObjectType, ParseRequestError, QueryBuilder, + QueryResponse, Schema, SubscriptionType, }; use async_trait::async_trait; use tide::{http::headers, Request, Response, Status, StatusCode}; @@ -57,7 +57,7 @@ where TideState: Send + Sync + 'static, F: Fn(QueryBuilder) -> QueryBuilder + Send, { - let query_builder = req.graphql().await.status(StatusCode::BadRequest)?; + let query_builder = req.body_graphql().await.status(StatusCode::BadRequest)?; let resp = GQLResponse( query_builder_configuration(query_builder) .execute(&schema) @@ -81,7 +81,7 @@ where F: Fn(QueryBuilder) -> QueryBuilder + Send, { let query_builder = req - .graphql_opts(opts) + .body_graphql_opts(opts) .await .status(StatusCode::BadRequest)?; let resp = GQLResponse( @@ -97,12 +97,12 @@ where #[async_trait] pub trait RequestExt: Sized { /// Convert a query to `async_graphql::QueryBuilder`. - async fn graphql(self) -> Result { - self.graphql_opts(Default::default()).await + async fn body_graphql(self) -> Result { + self.body_graphql_opts(Default::default()).await } /// Similar to graphql, but you can set the options `IntoQueryBuilderOpts`. - async fn graphql_opts( + async fn body_graphql_opts( self, opts: IntoQueryBuilderOpts, ) -> Result; @@ -110,7 +110,7 @@ pub trait RequestExt: Sized { #[async_trait] impl RequestExt for Request { - async fn graphql_opts( + async fn body_graphql_opts( self, opts: IntoQueryBuilderOpts, ) -> Result { @@ -120,3 +120,16 @@ impl RequestExt for Request { (content_type, self).into_query_builder_opts(&opts).await } } + +/// Tide response extension +/// +pub trait ResponseExt: Sized { + /// Set Body as the result of a GraphQL query. + fn body_graphql(self, res: async_graphql::Result) -> serde_json::Result; +} + +impl ResponseExt for Response { + fn body_graphql(self, res: async_graphql::Result) -> serde_json::Result { + self.body_json(&GQLResponse(res)) + } +}