Merge pull request #79 from vkill/async-graphql-tide-request-ext
async-graphql-tide: support Extension Traits
This commit is contained in:
commit
3b3608fee7
@ -15,6 +15,7 @@ categories = ["network-programming", "asynchronous"]
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
async-graphql = { path = "..", version = "1.11.3" }
|
async-graphql = { path = "..", version = "1.11.3" }
|
||||||
tide = "0.8"
|
tide = "0.8"
|
||||||
|
async-trait = "0.1.30"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
async-std = "1.5.0"
|
async-std = "1.5.0"
|
||||||
|
@ -8,6 +8,7 @@ use async_graphql::http::GQLResponse;
|
|||||||
use async_graphql::{
|
use async_graphql::{
|
||||||
IntoQueryBuilder, IntoQueryBuilderOpts, ObjectType, QueryBuilder, Schema, SubscriptionType,
|
IntoQueryBuilder, IntoQueryBuilderOpts, ObjectType, QueryBuilder, Schema, SubscriptionType,
|
||||||
};
|
};
|
||||||
|
use async_trait::async_trait;
|
||||||
use tide::{http::headers, Request, Response, Status, StatusCode};
|
use tide::{http::headers, Request, Response, Status, StatusCode};
|
||||||
|
|
||||||
/// GraphQL request handler
|
/// GraphQL request handler
|
||||||
@ -53,7 +54,7 @@ where
|
|||||||
Mutation: ObjectType + Send + Sync + 'static,
|
Mutation: ObjectType + Send + Sync + 'static,
|
||||||
Subscription: SubscriptionType + Send + Sync + 'static,
|
Subscription: SubscriptionType + Send + Sync + 'static,
|
||||||
TideState: Send + Sync + 'static,
|
TideState: Send + Sync + 'static,
|
||||||
F: Fn(QueryBuilder) -> QueryBuilder,
|
F: Fn(QueryBuilder) -> QueryBuilder + Send,
|
||||||
{
|
{
|
||||||
graphql_opts(req, schema, query_builder_configuration, Default::default()).await
|
graphql_opts(req, schema, query_builder_configuration, Default::default()).await
|
||||||
}
|
}
|
||||||
@ -70,13 +71,95 @@ where
|
|||||||
Mutation: ObjectType + Send + Sync + 'static,
|
Mutation: ObjectType + Send + Sync + 'static,
|
||||||
Subscription: SubscriptionType + Send + Sync + 'static,
|
Subscription: SubscriptionType + Send + Sync + 'static,
|
||||||
TideState: Send + Sync + 'static,
|
TideState: Send + Sync + 'static,
|
||||||
F: Fn(QueryBuilder) -> QueryBuilder,
|
F: Fn(QueryBuilder) -> QueryBuilder + Send,
|
||||||
{
|
{
|
||||||
let content_type = req
|
req.graphql_opts(schema, query_builder_configuration, opts)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Tide request extension
|
||||||
|
///
|
||||||
|
#[async_trait]
|
||||||
|
pub trait RequestExt<State>: Sized {
|
||||||
|
/// ```no_run
|
||||||
|
/// use async_graphql::*;
|
||||||
|
/// use async_std::task;
|
||||||
|
/// use tide::Request;
|
||||||
|
/// use async_graphql_tide::RequestExt as _;
|
||||||
|
///
|
||||||
|
/// struct QueryRoot;
|
||||||
|
/// #[Object]
|
||||||
|
/// impl QueryRoot {
|
||||||
|
/// #[field(desc = "Returns the sum of a and b")]
|
||||||
|
/// async fn add(&self, a: i32, b: i32) -> i32 {
|
||||||
|
/// a + b
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// fn main() -> std::result::Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||||
|
/// task::block_on(async {
|
||||||
|
/// let mut app = tide::new();
|
||||||
|
/// app.at("/").post(|req: Request<()>| async move {
|
||||||
|
/// let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription).finish();
|
||||||
|
/// req.graphql(schema, |query_builder| query_builder).await
|
||||||
|
/// });
|
||||||
|
/// app.listen("0.0.0.0:8000").await?;
|
||||||
|
///
|
||||||
|
/// Ok(())
|
||||||
|
/// })
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
async fn graphql<Query, Mutation, Subscription, F>(
|
||||||
|
self,
|
||||||
|
schema: Schema<Query, Mutation, Subscription>,
|
||||||
|
query_builder_configuration: F,
|
||||||
|
) -> tide::Result<Response>
|
||||||
|
where
|
||||||
|
Query: ObjectType + Send + Sync + 'static,
|
||||||
|
Mutation: ObjectType + Send + Sync + 'static,
|
||||||
|
Subscription: SubscriptionType + Send + Sync + 'static,
|
||||||
|
State: Send + Sync + 'static,
|
||||||
|
F: Fn(QueryBuilder) -> QueryBuilder + Send,
|
||||||
|
{
|
||||||
|
self.graphql_opts(schema, query_builder_configuration, Default::default())
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Similar to graphql, but you can set the options `IntoQueryBuilderOpts`.
|
||||||
|
async fn graphql_opts<Query, Mutation, Subscription, F>(
|
||||||
|
self,
|
||||||
|
schema: Schema<Query, Mutation, Subscription>,
|
||||||
|
query_builder_configuration: F,
|
||||||
|
opts: IntoQueryBuilderOpts,
|
||||||
|
) -> tide::Result<Response>
|
||||||
|
where
|
||||||
|
Query: ObjectType + Send + Sync + 'static,
|
||||||
|
Mutation: ObjectType + Send + Sync + 'static,
|
||||||
|
Subscription: SubscriptionType + Send + Sync + 'static,
|
||||||
|
State: Send + Sync + 'static,
|
||||||
|
F: Fn(QueryBuilder) -> QueryBuilder + Send;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl<State> RequestExt<State> for Request<State> {
|
||||||
|
async fn graphql_opts<Query, Mutation, Subscription, F>(
|
||||||
|
self,
|
||||||
|
schema: Schema<Query, Mutation, Subscription>,
|
||||||
|
query_builder_configuration: F,
|
||||||
|
opts: IntoQueryBuilderOpts,
|
||||||
|
) -> tide::Result<Response>
|
||||||
|
where
|
||||||
|
Query: ObjectType + Send + Sync + 'static,
|
||||||
|
Mutation: ObjectType + Send + Sync + 'static,
|
||||||
|
Subscription: SubscriptionType + Send + Sync + 'static,
|
||||||
|
State: Send + Sync + 'static,
|
||||||
|
F: Fn(QueryBuilder) -> QueryBuilder + Send,
|
||||||
|
{
|
||||||
|
let content_type = self
|
||||||
.header(&headers::CONTENT_TYPE)
|
.header(&headers::CONTENT_TYPE)
|
||||||
.and_then(|values| values.first().map(|value| value.to_string()));
|
.and_then(|values| values.first().map(|value| value.to_string()));
|
||||||
|
|
||||||
let mut query_builder = (content_type, req)
|
let mut query_builder = (content_type, self)
|
||||||
.into_query_builder_opts(&opts)
|
.into_query_builder_opts(&opts)
|
||||||
.await
|
.await
|
||||||
.status(StatusCode::BadRequest)?;
|
.status(StatusCode::BadRequest)?;
|
||||||
@ -91,3 +174,4 @@ where
|
|||||||
|
|
||||||
Ok(resp)
|
Ok(resp)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@ -6,6 +6,7 @@ use std::time::Duration;
|
|||||||
use tide::Request;
|
use tide::Request;
|
||||||
|
|
||||||
use async_graphql::*;
|
use async_graphql::*;
|
||||||
|
use async_graphql_tide::RequestExt as _;
|
||||||
|
|
||||||
type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
|
type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
|
||||||
|
|
||||||
@ -27,7 +28,7 @@ fn quickstart() -> Result<()> {
|
|||||||
let mut app = tide::new();
|
let mut app = tide::new();
|
||||||
app.at("/").post(|req: Request<()>| async move {
|
app.at("/").post(|req: Request<()>| async move {
|
||||||
let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription).finish();
|
let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription).finish();
|
||||||
async_graphql_tide::graphql(req, schema, |query_builder| query_builder).await
|
req.graphql(schema, |query_builder| query_builder).await
|
||||||
});
|
});
|
||||||
app.listen(&listen_addr).await?;
|
app.listen(&listen_addr).await?;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user