Merge pull request #457 from Punie/update-rocket-integration
Update rocket integration with new FromForm query parser
This commit is contained in:
commit
a4888aef81
@ -16,7 +16,7 @@ categories = ["network-programming", "asynchronous"]
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
async-graphql = { path = "../..", version = "=2.7.1" }
|
async-graphql = { path = "../..", version = "=2.7.1" }
|
||||||
|
|
||||||
rocket = { git = "https://github.com/SergioBenitez/Rocket", rev = "48fd83a", default-features = false } # TODO: Change to Cargo crate when Rocket 0.5.0 is released
|
rocket = { git = "https://github.com/SergioBenitez/Rocket", rev = "2893ce7", default-features = false } # TODO: Change to Cargo crate when Rocket 0.5.0 is released
|
||||||
serde = "1.0.125"
|
serde = "1.0.125"
|
||||||
serde_json = "1.0.64"
|
serde_json = "1.0.64"
|
||||||
tokio-util = { version = "0.6.5", default-features = false, features = ["compat"] }
|
tokio-util = { version = "0.6.5", default-features = false, features = ["compat"] }
|
||||||
|
@ -15,18 +15,14 @@ use std::io::Cursor;
|
|||||||
|
|
||||||
use async_graphql::http::MultipartOptions;
|
use async_graphql::http::MultipartOptions;
|
||||||
use async_graphql::{ObjectType, ParseRequestError, Schema, SubscriptionType};
|
use async_graphql::{ObjectType, ParseRequestError, Schema, SubscriptionType};
|
||||||
use query_deserializer::QueryDeserializer;
|
|
||||||
use rocket::{
|
use rocket::{
|
||||||
data::{self, Data, FromData, ToByteUnit},
|
data::{self, Data, FromData, ToByteUnit},
|
||||||
|
form::FromForm,
|
||||||
http::{ContentType, Header, Status},
|
http::{ContentType, Header, Status},
|
||||||
request::{self, FromQuery},
|
|
||||||
response::{self, Responder},
|
response::{self, Responder},
|
||||||
};
|
};
|
||||||
use serde::de::Deserialize;
|
|
||||||
use tokio_util::compat::TokioAsyncReadCompatExt;
|
use tokio_util::compat::TokioAsyncReadCompatExt;
|
||||||
|
|
||||||
mod query_deserializer;
|
|
||||||
|
|
||||||
/// A batch request which can be extracted from a request's body.
|
/// A batch request which can be extracted from a request's body.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
@ -56,11 +52,14 @@ impl BatchRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[rocket::async_trait]
|
#[rocket::async_trait]
|
||||||
impl FromData for BatchRequest {
|
impl<'r> FromData<'r> for BatchRequest {
|
||||||
type Error = ParseRequestError;
|
type Error = ParseRequestError;
|
||||||
|
|
||||||
async fn from_data(req: &rocket::Request<'_>, data: Data) -> data::Outcome<Self, Self::Error> {
|
async fn from_data(
|
||||||
let opts: MultipartOptions = req.managed_state().copied().unwrap_or_default();
|
req: &'r rocket::Request<'_>,
|
||||||
|
data: Data,
|
||||||
|
) -> data::Outcome<Self, Self::Error> {
|
||||||
|
let opts: MultipartOptions = req.rocket().state().copied().unwrap_or_default();
|
||||||
|
|
||||||
let request = async_graphql::http::receive_batch_body(
|
let request = async_graphql::http::receive_batch_body(
|
||||||
req.headers().get_one("Content-Type"),
|
req.headers().get_one("Content-Type"),
|
||||||
@ -87,17 +86,12 @@ impl FromData for BatchRequest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A GraphQL request which can be extracted from a query string or the request's body.
|
/// A GraphQL request which can be extracted from the request's body.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```ignore
|
/// ```ignore
|
||||||
/// #[rocket::post("/graphql?<query..>", rank = 2)]
|
/// #[rocket::post("/graphql", data = "<request>", format = "application/json", rank = 2)]
|
||||||
/// async fn graphql_query(schema: State<'_, ExampleSchema>, query: Request) -> Result<Response, Status> {
|
|
||||||
/// query.execute(&schema).await
|
|
||||||
/// }
|
|
||||||
///
|
|
||||||
/// #[rocket::post("/graphql", data = "<request>", format = "application/json", rank = 1)]
|
|
||||||
/// async fn graphql_request(schema: State<'_, ExampleSchema>, request: Request) -> Result<Response, Status> {
|
/// async fn graphql_request(schema: State<'_, ExampleSchema>, request: Request) -> Result<Response, Status> {
|
||||||
/// request.execute(&schema).await
|
/// request.execute(&schema).await
|
||||||
/// }
|
/// }
|
||||||
@ -120,21 +114,66 @@ impl Request {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'q> FromQuery<'q> for Request {
|
impl From<Query> for Request {
|
||||||
type Error = serde::de::value::Error;
|
fn from(query: Query) -> Self {
|
||||||
|
let mut request = async_graphql::Request::new(query.query);
|
||||||
|
|
||||||
fn from_query(query: request::Query<'_>) -> Result<Self, Self::Error> {
|
if let Some(operation_name) = query.operation_name {
|
||||||
Ok(Self(async_graphql::Request::deserialize(
|
request = request.operation_name(operation_name);
|
||||||
QueryDeserializer(query),
|
}
|
||||||
)?))
|
|
||||||
|
if let Some(variables) = query.variables {
|
||||||
|
let value = serde_json::from_str(&variables).unwrap_or_default();
|
||||||
|
let variables = async_graphql::Variables::from_json(value);
|
||||||
|
request = request.variables(variables);
|
||||||
|
}
|
||||||
|
|
||||||
|
Request(request)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A GraphQL request which can be extracted from a query string.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```ignore
|
||||||
|
/// #[rocket::get("/graphql?<query..>")]
|
||||||
|
/// async fn graphql_query(schema: State<'_, ExampleSchema>, query: Query) -> Result<Response, Status> {
|
||||||
|
/// query.execute(&schema).await
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
#[derive(FromForm, Debug)]
|
||||||
|
pub struct Query {
|
||||||
|
query: String,
|
||||||
|
#[field(name = "operationName")]
|
||||||
|
operation_name: Option<String>,
|
||||||
|
variables: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Query {
|
||||||
|
/// Shortcut method to execute the request on the schema.
|
||||||
|
pub async fn execute<Query, Mutation, Subscription>(
|
||||||
|
self,
|
||||||
|
schema: &Schema<Query, Mutation, Subscription>,
|
||||||
|
) -> Response
|
||||||
|
where
|
||||||
|
Query: ObjectType + 'static,
|
||||||
|
Mutation: ObjectType + 'static,
|
||||||
|
Subscription: SubscriptionType + 'static,
|
||||||
|
{
|
||||||
|
let request: Request = self.into();
|
||||||
|
request.execute(schema).await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rocket::async_trait]
|
#[rocket::async_trait]
|
||||||
impl FromData for Request {
|
impl<'r> FromData<'r> for Request {
|
||||||
type Error = ParseRequestError;
|
type Error = ParseRequestError;
|
||||||
|
|
||||||
async fn from_data(req: &rocket::Request<'_>, data: Data) -> data::Outcome<Self, Self::Error> {
|
async fn from_data(
|
||||||
|
req: &'r rocket::Request<'_>,
|
||||||
|
data: Data,
|
||||||
|
) -> data::Outcome<Self, Self::Error> {
|
||||||
BatchRequest::from_data(req, data)
|
BatchRequest::from_data(req, data)
|
||||||
.await
|
.await
|
||||||
.and_then(|request| match request.0.into_single() {
|
.and_then(|request| match request.0.into_single() {
|
||||||
|
@ -1,65 +0,0 @@
|
|||||||
use rocket::http::RawStr;
|
|
||||||
use rocket::request::Query;
|
|
||||||
use serde::de::{DeserializeSeed, Deserializer, Error as _, IntoDeserializer, MapAccess, Visitor};
|
|
||||||
use serde::forward_to_deserialize_any;
|
|
||||||
|
|
||||||
/// A wrapper around `rocket::request::Query` that implements `Deserializer`.
|
|
||||||
pub(crate) struct QueryDeserializer<'q>(pub(crate) Query<'q>);
|
|
||||||
|
|
||||||
impl<'q, 'de> Deserializer<'de> for QueryDeserializer<'q> {
|
|
||||||
type Error = serde::de::value::Error;
|
|
||||||
|
|
||||||
fn deserialize_any<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Self::Error> {
|
|
||||||
visitor.visit_map(QueryMapAccess {
|
|
||||||
query: self.0,
|
|
||||||
value: None,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
forward_to_deserialize_any! {
|
|
||||||
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
|
|
||||||
bytes byte_buf option unit unit_struct newtype_struct seq tuple
|
|
||||||
tuple_struct map struct enum identifier ignored_any
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct QueryMapAccess<'q> {
|
|
||||||
query: Query<'q>,
|
|
||||||
value: Option<&'q RawStr>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'q, 'de> MapAccess<'de> for QueryMapAccess<'q> {
|
|
||||||
type Error = serde::de::value::Error;
|
|
||||||
|
|
||||||
fn next_key_seed<K: DeserializeSeed<'de>>(
|
|
||||||
&mut self,
|
|
||||||
seed: K,
|
|
||||||
) -> Result<Option<K::Value>, Self::Error> {
|
|
||||||
self.query
|
|
||||||
.next()
|
|
||||||
.map(|item| {
|
|
||||||
self.value = Some(item.value);
|
|
||||||
seed.deserialize(
|
|
||||||
item.key
|
|
||||||
.url_decode()
|
|
||||||
.map_err(Self::Error::custom)?
|
|
||||||
.into_deserializer(),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.transpose()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn next_value_seed<V: DeserializeSeed<'de>>(
|
|
||||||
&mut self,
|
|
||||||
seed: V,
|
|
||||||
) -> Result<V::Value, Self::Error> {
|
|
||||||
seed.deserialize(
|
|
||||||
self.value
|
|
||||||
.take()
|
|
||||||
.unwrap()
|
|
||||||
.url_decode()
|
|
||||||
.map_err(Self::Error::custom)?
|
|
||||||
.into_deserializer(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user