2020-10-15 13:16:40 +00:00
|
|
|
use std::convert::TryInto;
|
2020-09-21 06:57:33 +00:00
|
|
|
use std::io;
|
|
|
|
use std::io::ErrorKind;
|
2020-10-15 13:16:40 +00:00
|
|
|
|
|
|
|
use async_graphql::http::MultipartOptions;
|
|
|
|
use async_graphql::{BatchRequest, ObjectType, Schema, SubscriptionType};
|
|
|
|
use futures_util::TryStreamExt;
|
2021-01-10 00:06:48 +00:00
|
|
|
use warp::hyper::header::HeaderName;
|
2020-09-21 06:57:33 +00:00
|
|
|
use warp::reply::Response as WarpResponse;
|
|
|
|
use warp::{Buf, Filter, Rejection, Reply};
|
|
|
|
|
2020-10-15 13:16:40 +00:00
|
|
|
use crate::BadRequest;
|
|
|
|
|
2020-09-21 06:57:33 +00:00
|
|
|
/// GraphQL batch request filter
|
|
|
|
///
|
|
|
|
/// It outputs a tuple containing the `async_graphql::Schema` and `async_graphql::BatchRequest`.
|
|
|
|
pub fn graphql_batch<Query, Mutation, Subscription>(
|
|
|
|
schema: Schema<Query, Mutation, Subscription>,
|
2020-10-15 13:16:40 +00:00
|
|
|
) -> impl Filter<Extract = ((Schema<Query, Mutation, Subscription>, BatchRequest),), Error = Rejection>
|
|
|
|
+ Clone
|
2020-09-21 06:57:33 +00:00
|
|
|
where
|
|
|
|
Query: ObjectType + Send + Sync + 'static,
|
|
|
|
Mutation: ObjectType + Send + Sync + 'static,
|
|
|
|
Subscription: SubscriptionType + Send + Sync + 'static,
|
|
|
|
{
|
|
|
|
graphql_batch_opts(schema, Default::default())
|
|
|
|
}
|
|
|
|
|
2020-10-15 13:16:40 +00:00
|
|
|
/// Similar to graphql_batch, but you can set the options with :`async_graphql::MultipartOptions`.
|
2020-09-21 06:57:33 +00:00
|
|
|
pub fn graphql_batch_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>, BatchRequest),), Error = Rejection>
|
|
|
|
+ Clone
|
2020-09-21 06:57:33 +00:00
|
|
|
where
|
|
|
|
Query: ObjectType + Send + Sync + 'static,
|
|
|
|
Mutation: ObjectType + Send + Sync + 'static,
|
|
|
|
Subscription: SubscriptionType + Send + Sync + 'static,
|
|
|
|
{
|
|
|
|
warp::any()
|
2020-10-15 13:16:40 +00:00
|
|
|
.and(warp::get().and(warp::query()).map(BatchRequest::Single))
|
2020-10-15 17:42:09 +00:00
|
|
|
.or(warp::post()
|
2020-10-15 13:16:40 +00:00
|
|
|
.and(warp::header::optional::<String>("content-type"))
|
|
|
|
.and(warp::body::stream())
|
|
|
|
.and_then(move |content_type, body| async move {
|
|
|
|
async_graphql::http::receive_batch_body(
|
2020-09-21 06:57:33 +00:00
|
|
|
content_type,
|
2020-10-15 13:16:40 +00:00
|
|
|
TryStreamExt::map_err(body, |e| io::Error::new(ErrorKind::Other, e))
|
|
|
|
.map_ok(|mut buf| Buf::to_bytes(&mut buf))
|
|
|
|
.into_async_read(),
|
|
|
|
opts,
|
2020-09-21 06:57:33 +00:00
|
|
|
)
|
|
|
|
.await
|
2020-10-15 13:16:40 +00:00
|
|
|
.map_err(|e| warp::reject::custom(BadRequest(e)))
|
|
|
|
}))
|
|
|
|
.unify()
|
|
|
|
.map(move |res| (schema.clone(), res))
|
2020-09-21 06:57:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Reply for `async_graphql::BatchRequest`.
|
2020-10-15 13:16:40 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct BatchResponse(pub async_graphql::BatchResponse);
|
2020-09-21 06:57:33 +00:00
|
|
|
|
|
|
|
impl From<async_graphql::BatchResponse> for BatchResponse {
|
|
|
|
fn from(resp: async_graphql::BatchResponse) -> Self {
|
|
|
|
BatchResponse(resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Reply for BatchResponse {
|
|
|
|
fn into_response(self) -> WarpResponse {
|
|
|
|
let mut resp = warp::reply::with_header(
|
|
|
|
warp::reply::json(&self.0),
|
|
|
|
"content-type",
|
|
|
|
"application/json",
|
|
|
|
)
|
|
|
|
.into_response();
|
2020-10-15 13:16:40 +00:00
|
|
|
|
|
|
|
if self.0.is_ok() {
|
|
|
|
if let Some(cache_control) = self.0.cache_control().value() {
|
2021-01-10 00:06:48 +00:00
|
|
|
if let Ok(value) = cache_control.try_into() {
|
|
|
|
resp.headers_mut().insert("cache-control", value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (name, value) in self.0.http_headers() {
|
|
|
|
if let (Ok(name), Ok(value)) =
|
|
|
|
(TryInto::<HeaderName>::try_into(name), value.try_into())
|
|
|
|
{
|
|
|
|
resp.headers_mut().insert(name, value);
|
|
|
|
}
|
2020-10-15 13:16:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-21 06:57:33 +00:00
|
|
|
resp
|
|
|
|
}
|
|
|
|
}
|