Support batch requests in Tide
This commit is contained in:
parent
38c9c718d6
commit
7b18bd5f80
@ -9,7 +9,7 @@
|
|||||||
#![forbid(unsafe_code)]
|
#![forbid(unsafe_code)]
|
||||||
|
|
||||||
use async_graphql::http::MultipartOptions;
|
use async_graphql::http::MultipartOptions;
|
||||||
use async_graphql::{resolver_utils::ObjectType, Schema, SubscriptionType};
|
use async_graphql::{resolver_utils::ObjectType, ParseRequestError, Schema, SubscriptionType};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use tide::{
|
use tide::{
|
||||||
http::{
|
http::{
|
||||||
@ -19,27 +19,43 @@ use tide::{
|
|||||||
Body, Request, Response, StatusCode,
|
Body, Request, Response, StatusCode,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Create a new GraphQL endpoint with the schema and default multipart options.
|
/// Create a new GraphQL endpoint with the schema.
|
||||||
|
///
|
||||||
|
/// Default multipart options are used and batch operations are supported.
|
||||||
pub fn endpoint<Query, Mutation, Subscription>(
|
pub fn endpoint<Query, Mutation, Subscription>(
|
||||||
schema: Schema<Query, Mutation, Subscription>,
|
schema: Schema<Query, Mutation, Subscription>,
|
||||||
) -> Endpoint<Query, Mutation, Subscription> {
|
) -> Endpoint<Query, Mutation, Subscription> {
|
||||||
endpoint_options(schema, MultipartOptions::default())
|
Endpoint {
|
||||||
}
|
schema,
|
||||||
|
opts: MultipartOptions::default(),
|
||||||
/// Create a new GraphQL endpoint with the schema and custom multipart options.
|
batch: true,
|
||||||
pub fn endpoint_options<Query, Mutation, Subscription>(
|
}
|
||||||
schema: Schema<Query, Mutation, Subscription>,
|
|
||||||
opts: MultipartOptions,
|
|
||||||
) -> Endpoint<Query, Mutation, Subscription> {
|
|
||||||
Endpoint { schema, opts }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A GraphQL endpoint.
|
/// A GraphQL endpoint.
|
||||||
|
///
|
||||||
|
/// This is created with the [`endpoint`](fn.endpoint.html) function.
|
||||||
|
#[non_exhaustive]
|
||||||
pub struct Endpoint<Query, Mutation, Subscription> {
|
pub struct Endpoint<Query, Mutation, Subscription> {
|
||||||
/// The schema of the endpoint.
|
/// The schema of the endpoint.
|
||||||
pub schema: Schema<Query, Mutation, Subscription>,
|
pub schema: Schema<Query, Mutation, Subscription>,
|
||||||
/// The multipart options of the endpoint.
|
/// The multipart options of the endpoint.
|
||||||
pub opts: MultipartOptions,
|
pub opts: MultipartOptions,
|
||||||
|
/// 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 }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Manual impl to remove bounds on generics
|
// Manual impl to remove bounds on generics
|
||||||
@ -48,6 +64,7 @@ impl<Query, Mutation, Subscription> Clone for Endpoint<Query, Mutation, Subscrip
|
|||||||
Self {
|
Self {
|
||||||
schema: self.schema.clone(),
|
schema: self.schema.clone(),
|
||||||
opts: self.opts,
|
opts: self.opts,
|
||||||
|
batch: self.batch,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -64,7 +81,13 @@ where
|
|||||||
async fn call(&self, request: Request<TideState>) -> tide::Result {
|
async fn call(&self, request: Request<TideState>) -> tide::Result {
|
||||||
respond(
|
respond(
|
||||||
self.schema
|
self.schema
|
||||||
.execute(receive_request_opts(request, self.opts).await?)
|
.execute_batch(if self.batch {
|
||||||
|
receive_batch_request_opts(request, self.opts).await
|
||||||
|
} else {
|
||||||
|
receive_request_opts(request, self.opts)
|
||||||
|
.await
|
||||||
|
.map(Into::into)
|
||||||
|
}?)
|
||||||
.await,
|
.await,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -79,9 +102,27 @@ pub async fn receive_request<State: Clone + Send + Sync + 'static>(
|
|||||||
|
|
||||||
/// Convert a Tide request to a GraphQL request with options on how to receive multipart.
|
/// 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>(
|
pub async fn receive_request_opts<State: Clone + Send + Sync + 'static>(
|
||||||
mut request: Request<State>,
|
request: Request<State>,
|
||||||
opts: MultipartOptions,
|
opts: MultipartOptions,
|
||||||
) -> tide::Result<async_graphql::Request> {
|
) -> tide::Result<async_graphql::Request> {
|
||||||
|
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> {
|
||||||
if request.method() == Method::Get {
|
if request.method() == Method::Get {
|
||||||
request.query()
|
request.query()
|
||||||
} else {
|
} else {
|
||||||
@ -91,17 +132,27 @@ pub async fn receive_request_opts<State: Clone + Send + Sync + 'static>(
|
|||||||
.and_then(|values| values.get(0))
|
.and_then(|values| values.get(0))
|
||||||
.map(HeaderValue::as_str);
|
.map(HeaderValue::as_str);
|
||||||
|
|
||||||
async_graphql::http::receive_body(content_type, body, opts)
|
async_graphql::http::receive_batch_body(content_type, body, opts)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| tide::Error::new(StatusCode::BadRequest, e))
|
.map_err(|e| {
|
||||||
|
tide::Error::new(
|
||||||
|
match &e {
|
||||||
|
ParseRequestError::PayloadTooLarge => StatusCode::PayloadTooLarge,
|
||||||
|
_ => StatusCode::BadRequest,
|
||||||
|
},
|
||||||
|
e,
|
||||||
|
)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert a GraphQL response to a Tide response.
|
/// Convert a GraphQL response to a Tide response.
|
||||||
pub fn respond(gql: async_graphql::Response) -> tide::Result {
|
pub fn respond(gql: impl Into<async_graphql::BatchResponse>) -> tide::Result {
|
||||||
|
let gql = gql.into();
|
||||||
|
|
||||||
let mut response = Response::new(StatusCode::Ok);
|
let mut response = Response::new(StatusCode::Ok);
|
||||||
if gql.is_ok() {
|
if gql.is_ok() {
|
||||||
if let Some(cache_control) = gql.cache_control.value() {
|
if let Some(cache_control) = gql.cache_control().value() {
|
||||||
response.insert_header(headers::CACHE_CONTROL, cache_control);
|
response.insert_header(headers::CACHE_CONTROL, cache_control);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user