2020-04-26 11:53:44 +00:00
|
|
|
//! Async-graphql integration with Tide
|
2020-09-23 17:30:03 +00:00
|
|
|
//!
|
2020-10-15 10:51:21 +00:00
|
|
|
//! Tide [does not support websockets](https://github.com/http-rs/tide/issues/67), so you can't use
|
|
|
|
//! subscriptions with it.
|
|
|
|
//!
|
2020-09-23 17:30:03 +00:00
|
|
|
//! # Examples
|
|
|
|
//! *[Full Example](<https://github.com/async-graphql/examples/blob/master/tide/starwars/src/main.rs>)*
|
2020-04-26 11:53:44 +00:00
|
|
|
|
|
|
|
#![warn(missing_docs)]
|
|
|
|
#![allow(clippy::type_complexity)]
|
|
|
|
#![allow(clippy::needless_doctest_main)]
|
2020-05-29 09:33:19 +00:00
|
|
|
#![forbid(unsafe_code)]
|
2020-04-26 11:53:44 +00:00
|
|
|
|
2020-09-12 16:07:46 +00:00
|
|
|
use async_graphql::http::MultipartOptions;
|
2020-09-29 23:45:48 +00:00
|
|
|
use async_graphql::{ObjectType, ParseRequestError, Schema, SubscriptionType};
|
2020-10-15 10:51:21 +00:00
|
|
|
use tide::utils::async_trait;
|
2020-05-16 02:34:06 +00:00
|
|
|
use tide::{
|
2020-09-23 17:30:03 +00:00
|
|
|
http::{
|
|
|
|
headers::{self, HeaderValue},
|
|
|
|
Method,
|
|
|
|
},
|
2020-09-11 14:17:17 +00:00
|
|
|
Body, Request, Response, StatusCode,
|
2020-05-16 02:34:06 +00:00
|
|
|
};
|
2020-04-26 11:53:44 +00:00
|
|
|
|
2020-09-25 17:03:51 +00:00
|
|
|
/// Create a new GraphQL endpoint with the schema.
|
|
|
|
///
|
|
|
|
/// Default multipart options are used and batch operations are supported.
|
2020-09-23 17:30:03 +00:00
|
|
|
pub fn endpoint<Query, Mutation, Subscription>(
|
2020-04-26 11:53:44 +00:00
|
|
|
schema: Schema<Query, Mutation, Subscription>,
|
2020-09-23 17:30:03 +00:00
|
|
|
) -> Endpoint<Query, Mutation, Subscription> {
|
2020-09-25 17:03:51 +00:00
|
|
|
Endpoint {
|
|
|
|
schema,
|
|
|
|
opts: MultipartOptions::default(),
|
|
|
|
batch: true,
|
|
|
|
}
|
2020-09-23 17:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A GraphQL endpoint.
|
2020-09-25 17:03:51 +00:00
|
|
|
///
|
|
|
|
/// This is created with the [`endpoint`](fn.endpoint.html) function.
|
|
|
|
#[non_exhaustive]
|
2020-09-23 17:30:03 +00:00
|
|
|
pub struct Endpoint<Query, Mutation, Subscription> {
|
|
|
|
/// The schema of the endpoint.
|
|
|
|
pub schema: Schema<Query, Mutation, Subscription>,
|
|
|
|
/// The multipart options of the endpoint.
|
|
|
|
pub opts: MultipartOptions,
|
2020-09-25 17:03:51 +00:00
|
|
|
/// Whether to support batch requests in the endpoint.
|
|
|
|
pub batch: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<Query, Mutation, Subscription> Endpoint<Query, Mutation, Subscription> {
|
|
|
|
/// Set the multipart options of the endpoint.
|
|
|
|
#[must_use]
|
|
|
|
pub fn multipart_opts(self, opts: MultipartOptions) -> Self {
|
|
|
|
Self { opts, ..self }
|
|
|
|
}
|
|
|
|
/// Set whether batch requests are supported in the endpoint.
|
|
|
|
#[must_use]
|
|
|
|
pub fn batch(self, batch: bool) -> Self {
|
|
|
|
Self { batch, ..self }
|
|
|
|
}
|
2020-09-23 17:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Manual impl to remove bounds on generics
|
|
|
|
impl<Query, Mutation, Subscription> Clone for Endpoint<Query, Mutation, Subscription> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
schema: self.schema.clone(),
|
|
|
|
opts: self.opts,
|
2020-09-25 17:03:51 +00:00
|
|
|
batch: self.batch,
|
2020-09-23 17:30:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl<Query, Mutation, Subscription, TideState> tide::Endpoint<TideState>
|
|
|
|
for Endpoint<Query, Mutation, Subscription>
|
2020-04-26 11:53:44 +00:00
|
|
|
where
|
|
|
|
Query: ObjectType + Send + Sync + 'static,
|
|
|
|
Mutation: ObjectType + Send + Sync + 'static,
|
|
|
|
Subscription: SubscriptionType + Send + Sync + 'static,
|
2020-07-19 10:20:00 +00:00
|
|
|
TideState: Clone + Send + Sync + 'static,
|
2020-04-26 11:53:44 +00:00
|
|
|
{
|
2020-09-23 17:30:03 +00:00
|
|
|
async fn call(&self, request: Request<TideState>) -> tide::Result {
|
|
|
|
respond(
|
|
|
|
self.schema
|
2020-09-25 17:03:51 +00:00
|
|
|
.execute_batch(if self.batch {
|
|
|
|
receive_batch_request_opts(request, self.opts).await
|
|
|
|
} else {
|
|
|
|
receive_request_opts(request, self.opts)
|
|
|
|
.await
|
|
|
|
.map(Into::into)
|
|
|
|
}?)
|
2020-09-23 17:30:03 +00:00
|
|
|
.await,
|
|
|
|
)
|
2020-05-12 01:11:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-23 17:30:03 +00:00
|
|
|
/// Convert a Tide request to a GraphQL request.
|
|
|
|
pub async fn receive_request<State: Clone + Send + Sync + 'static>(
|
|
|
|
request: Request<State>,
|
|
|
|
) -> tide::Result<async_graphql::Request> {
|
|
|
|
receive_request_opts(request, Default::default()).await
|
2020-04-26 11:53:44 +00:00
|
|
|
}
|
2020-05-14 07:24:24 +00:00
|
|
|
|
2020-09-23 17:30:03 +00:00
|
|
|
/// Convert a Tide request to a GraphQL request with options on how to receive multipart.
|
|
|
|
pub async fn receive_request_opts<State: Clone + Send + Sync + 'static>(
|
2020-09-25 17:03:51 +00:00
|
|
|
request: Request<State>,
|
2020-09-23 17:30:03 +00:00
|
|
|
opts: MultipartOptions,
|
|
|
|
) -> tide::Result<async_graphql::Request> {
|
2020-09-25 17:03:51 +00:00
|
|
|
receive_batch_request_opts(request, opts)
|
|
|
|
.await?
|
|
|
|
.into_single()
|
|
|
|
.map_err(|e| tide::Error::new(StatusCode::BadRequest, e))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert a Tide request to a GraphQL batch request.
|
|
|
|
pub async fn receive_batch_request<State: Clone + Send + Sync + 'static>(
|
|
|
|
request: Request<State>,
|
|
|
|
) -> tide::Result<async_graphql::BatchRequest> {
|
|
|
|
receive_batch_request_opts(request, Default::default()).await
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert a Tide request to a GraphQL batch request with options on how to receive multipart.
|
|
|
|
pub async fn receive_batch_request_opts<State: Clone + Send + Sync + 'static>(
|
|
|
|
mut request: Request<State>,
|
|
|
|
opts: MultipartOptions,
|
|
|
|
) -> tide::Result<async_graphql::BatchRequest> {
|
2020-09-23 17:30:03 +00:00
|
|
|
if request.method() == Method::Get {
|
2020-09-25 18:50:04 +00:00
|
|
|
request.query::<async_graphql::Request>().map(Into::into)
|
2020-10-15 17:42:09 +00:00
|
|
|
} else if request.method() == Method::Post {
|
2020-09-23 17:30:03 +00:00
|
|
|
let body = request.take_body();
|
|
|
|
let content_type = request
|
|
|
|
.header(headers::CONTENT_TYPE)
|
|
|
|
.and_then(|values| values.get(0))
|
|
|
|
.map(HeaderValue::as_str);
|
2020-05-14 07:24:24 +00:00
|
|
|
|
2020-09-25 17:03:51 +00:00
|
|
|
async_graphql::http::receive_batch_body(content_type, body, opts)
|
2020-09-23 17:30:03 +00:00
|
|
|
.await
|
2020-09-25 17:03:51 +00:00
|
|
|
.map_err(|e| {
|
|
|
|
tide::Error::new(
|
|
|
|
match &e {
|
|
|
|
ParseRequestError::PayloadTooLarge => StatusCode::PayloadTooLarge,
|
|
|
|
_ => StatusCode::BadRequest,
|
|
|
|
},
|
|
|
|
e,
|
|
|
|
)
|
|
|
|
})
|
2020-10-15 17:42:09 +00:00
|
|
|
} else {
|
|
|
|
Err(tide::Error::from_str(
|
|
|
|
StatusCode::MethodNotAllowed,
|
|
|
|
"GraphQL only supports GET and POST requests",
|
|
|
|
))
|
2020-05-14 07:24:24 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-28 14:52:35 +00:00
|
|
|
|
2020-09-23 17:30:03 +00:00
|
|
|
/// Convert a GraphQL response to a Tide response.
|
2020-09-25 17:03:51 +00:00
|
|
|
pub fn respond(gql: impl Into<async_graphql::BatchResponse>) -> tide::Result {
|
|
|
|
let gql = gql.into();
|
|
|
|
|
2020-09-23 17:30:03 +00:00
|
|
|
let mut response = Response::new(StatusCode::Ok);
|
|
|
|
if gql.is_ok() {
|
2020-09-25 17:03:51 +00:00
|
|
|
if let Some(cache_control) = gql.cache_control().value() {
|
2020-09-23 17:30:03 +00:00
|
|
|
response.insert_header(headers::CACHE_CONTROL, cache_control);
|
2020-05-28 14:52:35 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-23 17:30:03 +00:00
|
|
|
response.set_body(Body::from_json(&gql)?);
|
|
|
|
Ok(response)
|
2020-05-28 14:52:35 +00:00
|
|
|
}
|