2022-04-19 04:25:11 +00:00
|
|
|
use async_graphql::{
|
|
|
|
http::MultipartOptions, BatchRequest, ObjectType, Request, Schema, SubscriptionType,
|
|
|
|
};
|
|
|
|
use warp::{reply::Response as WarpResponse, Filter, Rejection, Reply};
|
2020-10-15 13:16:40 +00:00
|
|
|
|
2021-11-12 13:48:18 +00:00
|
|
|
use crate::{graphql_batch_opts, GraphQLBadRequest, GraphQLBatchResponse};
|
2020-09-21 06:57:33 +00:00
|
|
|
|
|
|
|
/// GraphQL request filter
|
|
|
|
///
|
2022-04-19 04:25:11 +00:00
|
|
|
/// It outputs a tuple containing the `async_graphql::Schema` and
|
|
|
|
/// `async_graphql::Request`.
|
2020-09-21 06:57:33 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// *[Full Example](<https://github.com/async-graphql/examples/blob/master/warp/starwars/src/main.rs>)*
|
|
|
|
///
|
|
|
|
/// ```no_run
|
2022-04-19 04:25:11 +00:00
|
|
|
/// use std::convert::Infallible;
|
2020-09-21 06:57:33 +00:00
|
|
|
///
|
|
|
|
/// use async_graphql::*;
|
|
|
|
/// use async_graphql_warp::*;
|
|
|
|
/// use warp::Filter;
|
|
|
|
///
|
|
|
|
/// struct QueryRoot;
|
|
|
|
///
|
|
|
|
/// #[Object]
|
|
|
|
/// impl QueryRoot {
|
|
|
|
/// async fn value(&self, ctx: &Context<'_>) -> i32 {
|
|
|
|
/// unimplemented!()
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// type MySchema = Schema<QueryRoot, EmptyMutation, EmptySubscription>;
|
|
|
|
///
|
2021-11-20 03:16:48 +00:00
|
|
|
/// # tokio::runtime::Runtime::new().unwrap().block_on(async {
|
|
|
|
/// let schema = Schema::new(QueryRoot, EmptyMutation, EmptySubscription);
|
2022-04-19 04:25:11 +00:00
|
|
|
/// let filter = async_graphql_warp::graphql(schema).and_then(
|
|
|
|
/// |(schema, request): (MySchema, async_graphql::Request)| async move {
|
|
|
|
/// Ok::<_, Infallible>(async_graphql_warp::GraphQLResponse::from(
|
|
|
|
/// schema.execute(request).await,
|
|
|
|
/// ))
|
|
|
|
/// },
|
|
|
|
/// );
|
2021-11-20 03:16:48 +00:00
|
|
|
/// warp::serve(filter).run(([0, 0, 0, 0], 8000)).await;
|
|
|
|
/// # });
|
2020-09-21 06:57:33 +00:00
|
|
|
/// ```
|
|
|
|
pub fn graphql<Query, Mutation, Subscription>(
|
|
|
|
schema: Schema<Query, Mutation, Subscription>,
|
|
|
|
) -> impl Filter<
|
|
|
|
Extract = ((
|
|
|
|
Schema<Query, Mutation, Subscription>,
|
|
|
|
async_graphql::Request,
|
|
|
|
),),
|
|
|
|
Error = Rejection,
|
|
|
|
> + Clone
|
|
|
|
where
|
2021-01-14 04:41:59 +00:00
|
|
|
Query: ObjectType + 'static,
|
|
|
|
Mutation: ObjectType + 'static,
|
|
|
|
Subscription: SubscriptionType + 'static,
|
2020-09-21 06:57:33 +00:00
|
|
|
{
|
|
|
|
graphql_opts(schema, Default::default())
|
|
|
|
}
|
|
|
|
|
2022-04-19 04:25:11 +00:00
|
|
|
/// Similar to graphql, but you can set the options
|
|
|
|
/// `async_graphql::MultipartOptions`.
|
2020-09-21 06:57:33 +00:00
|
|
|
pub fn graphql_opts<Query, Mutation, Subscription>(
|
|
|
|
schema: Schema<Query, Mutation, Subscription>,
|
|
|
|
opts: MultipartOptions,
|
2020-10-15 13:16:40 +00:00
|
|
|
) -> impl Filter<Extract = ((Schema<Query, Mutation, Subscription>, Request),), Error = Rejection> + Clone
|
2020-09-21 06:57:33 +00:00
|
|
|
where
|
2021-01-14 04:41:59 +00:00
|
|
|
Query: ObjectType + 'static,
|
|
|
|
Mutation: ObjectType + 'static,
|
|
|
|
Subscription: SubscriptionType + 'static,
|
2020-09-21 06:57:33 +00:00
|
|
|
{
|
2020-10-15 13:16:40 +00:00
|
|
|
graphql_batch_opts(schema, opts).and_then(|(schema, batch): (_, BatchRequest)| async move {
|
|
|
|
<Result<_, Rejection>>::Ok((
|
|
|
|
schema,
|
|
|
|
batch
|
|
|
|
.into_single()
|
2021-11-12 13:48:18 +00:00
|
|
|
.map_err(|e| warp::reject::custom(GraphQLBadRequest(e)))?,
|
2020-10-15 13:16:40 +00:00
|
|
|
))
|
|
|
|
})
|
2020-09-21 06:57:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Reply for `async_graphql::Request`.
|
2020-10-15 13:16:40 +00:00
|
|
|
#[derive(Debug)]
|
2021-11-12 13:48:18 +00:00
|
|
|
pub struct GraphQLResponse(pub async_graphql::Response);
|
2020-09-21 06:57:33 +00:00
|
|
|
|
2021-11-12 13:48:18 +00:00
|
|
|
impl From<async_graphql::Response> for GraphQLResponse {
|
2020-09-21 06:57:33 +00:00
|
|
|
fn from(resp: async_graphql::Response) -> Self {
|
2021-11-12 13:48:18 +00:00
|
|
|
GraphQLResponse(resp)
|
2020-09-21 06:57:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-12 13:48:18 +00:00
|
|
|
impl Reply for GraphQLResponse {
|
2020-09-21 06:57:33 +00:00
|
|
|
fn into_response(self) -> WarpResponse {
|
2021-11-12 13:48:18 +00:00
|
|
|
GraphQLBatchResponse(self.0.into()).into_response()
|
2020-09-21 06:57:33 +00:00
|
|
|
}
|
|
|
|
}
|