2020-03-26 03:34:28 +00:00
|
|
|
//! A helper module that supports HTTP
|
|
|
|
|
2020-03-05 00:39:56 +00:00
|
|
|
mod graphiql_source;
|
2020-09-10 08:43:24 +00:00
|
|
|
mod multipart;
|
2020-03-05 00:39:56 +00:00
|
|
|
mod playground_source;
|
2020-09-13 06:40:34 +00:00
|
|
|
pub mod websocket;
|
2020-03-05 00:39:56 +00:00
|
|
|
|
|
|
|
pub use graphiql_source::graphiql_source;
|
2020-09-10 08:43:24 +00:00
|
|
|
pub use multipart::{receive_multipart, MultipartOptions};
|
2020-06-06 01:55:53 +00:00
|
|
|
pub use playground_source::{playground_source, GraphQLPlaygroundConfig};
|
2020-03-05 00:39:56 +00:00
|
|
|
|
2020-09-12 16:07:46 +00:00
|
|
|
use crate::{ParseRequestError, Request};
|
2020-09-10 07:04:24 +00:00
|
|
|
use futures::io::AsyncRead;
|
2020-09-10 08:43:24 +00:00
|
|
|
use futures::AsyncReadExt;
|
2020-09-10 08:54:38 +00:00
|
|
|
|
2020-09-10 07:04:24 +00:00
|
|
|
/// Receive a GraphQL request from a content type and body.
|
|
|
|
pub async fn receive_body(
|
|
|
|
content_type: Option<impl AsRef<str>>,
|
2020-09-10 11:35:48 +00:00
|
|
|
mut body: impl AsyncRead + Unpin + Send + 'static,
|
2020-09-10 07:04:24 +00:00
|
|
|
opts: MultipartOptions,
|
2020-09-10 08:54:38 +00:00
|
|
|
) -> Result<Request, ParseRequestError> {
|
2020-09-10 07:04:24 +00:00
|
|
|
if let Some(Ok(boundary)) = content_type.map(multer::parse_boundary) {
|
2020-09-10 08:54:38 +00:00
|
|
|
receive_multipart(body, boundary, opts).await
|
2020-09-10 07:04:24 +00:00
|
|
|
} else {
|
|
|
|
let mut data = Vec::new();
|
2020-09-10 08:43:24 +00:00
|
|
|
body.read_to_end(&mut data)
|
|
|
|
.await
|
|
|
|
.map_err(ParseRequestError::Io)?;
|
2020-09-12 16:07:46 +00:00
|
|
|
Ok(serde_json::from_slice::<Request>(&data).map_err(ParseRequestError::InvalidRequest)?)
|
2020-03-05 00:39:56 +00:00
|
|
|
}
|
|
|
|
}
|