Fix relay @defer requires a label in the second chunk. #146
This commit is contained in:
parent
0949a022a0
commit
37943fa30a
|
@ -93,16 +93,22 @@ where
|
||||||
let opts = Arc::new(opts);
|
let opts = Arc::new(opts);
|
||||||
warp::any()
|
warp::any()
|
||||||
.and(warp::method())
|
.and(warp::method())
|
||||||
.and(warp::query::raw())
|
.and(warp::query::raw().or(warp::any().map(|| String::new())).unify())
|
||||||
.and(warp::header::optional::<String>("content-type"))
|
.and(warp::header::optional::<String>("content-type"))
|
||||||
.and(warp::body::stream())
|
.and(warp::body::stream())
|
||||||
.and(warp::any().map(move || opts.clone()))
|
.and(warp::any().map(move || opts.clone()))
|
||||||
.and(warp::any().map(move || schema.clone()))
|
.and(warp::any().map(move || schema.clone()))
|
||||||
.and_then(
|
.and_then(
|
||||||
|method, query: String, content_type, body, opts: Arc<IntoQueryBuilderOpts>, schema| async move {
|
|method,
|
||||||
|
query: String,
|
||||||
|
content_type,
|
||||||
|
body,
|
||||||
|
opts: Arc<IntoQueryBuilderOpts>,
|
||||||
|
schema| async move {
|
||||||
if method == Method::GET {
|
if method == Method::GET {
|
||||||
let gql_request: GQLRequest = serde_urlencoded::from_str(&query)
|
let gql_request: GQLRequest =
|
||||||
.map_err(|err| warp::reject::custom(BadRequest(err.into())))?;
|
serde_urlencoded::from_str(&query)
|
||||||
|
.map_err(|err| warp::reject::custom(BadRequest(err.into())))?;
|
||||||
let builder = gql_request
|
let builder = gql_request
|
||||||
.into_query_builder_opts(&opts)
|
.into_query_builder_opts(&opts)
|
||||||
.await
|
.await
|
||||||
|
|
|
@ -61,6 +61,10 @@ impl Serialize for GQLResponse {
|
||||||
match &self.0 {
|
match &self.0 {
|
||||||
Ok(res) => {
|
Ok(res) => {
|
||||||
let mut map = serializer.serialize_map(None)?;
|
let mut map = serializer.serialize_map(None)?;
|
||||||
|
if let Some(label) = &res.label {
|
||||||
|
map.serialize_key("label")?;
|
||||||
|
map.serialize_value(label)?;
|
||||||
|
}
|
||||||
if let Some(path) = &res.path {
|
if let Some(path) = &res.path {
|
||||||
map.serialize_key("path")?;
|
map.serialize_key("path")?;
|
||||||
map.serialize_value(path)?;
|
map.serialize_value(path)?;
|
||||||
|
@ -215,6 +219,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_response_data() {
|
fn test_response_data() {
|
||||||
let resp = GQLResponse(Ok(QueryResponse {
|
let resp = GQLResponse(Ok(QueryResponse {
|
||||||
|
label: None,
|
||||||
path: None,
|
path: None,
|
||||||
data: json!({"ok": true}),
|
data: json!({"ok": true}),
|
||||||
extensions: None,
|
extensions: None,
|
||||||
|
|
31
src/query.rs
31
src/query.rs
|
@ -9,7 +9,9 @@ use crate::{
|
||||||
};
|
};
|
||||||
use async_graphql_parser::query::OperationType;
|
use async_graphql_parser::query::OperationType;
|
||||||
use futures::{Stream, StreamExt};
|
use futures::{Stream, StreamExt};
|
||||||
|
use itertools::Itertools;
|
||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
|
use std::borrow::Cow;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::sync::atomic::AtomicUsize;
|
use std::sync::atomic::AtomicUsize;
|
||||||
|
@ -41,6 +43,11 @@ pub trait IntoQueryBuilder: Sized {
|
||||||
/// Query response
|
/// Query response
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct QueryResponse {
|
pub struct QueryResponse {
|
||||||
|
/// Label for RelayModernQueryExecutor
|
||||||
|
///
|
||||||
|
/// https://github.com/facebook/relay/blob/2859aa8df4df7d4d6d9eef4c9dc1134286773710/packages/relay-runtime/store/RelayModernQueryExecutor.js#L1267
|
||||||
|
pub label: Option<String>,
|
||||||
|
|
||||||
/// Path for subsequent response
|
/// Path for subsequent response
|
||||||
pub path: Option<Vec<serde_json::Value>>,
|
pub path: Option<Vec<serde_json::Value>>,
|
||||||
|
|
||||||
|
@ -62,6 +69,19 @@ impl QueryResponse {
|
||||||
} else {
|
} else {
|
||||||
self.path = Some(prefix);
|
self.path = Some(prefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.label = self.path.as_ref().map(|path| {
|
||||||
|
path.iter()
|
||||||
|
.map(|value| {
|
||||||
|
if let serde_json::Value::String(s) = value {
|
||||||
|
Cow::Borrowed(s.as_str())
|
||||||
|
} else {
|
||||||
|
Cow::Owned(value.to_string())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.join("$")
|
||||||
|
});
|
||||||
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -209,7 +229,15 @@ impl QueryBuilder {
|
||||||
next_path_prefix.extend(defer_list.path_prefix.clone());
|
next_path_prefix.extend(defer_list.path_prefix.clone());
|
||||||
next_defer_list.push((next_path_prefix, fut));
|
next_defer_list.push((next_path_prefix, fut));
|
||||||
}
|
}
|
||||||
yield res.apply_path_prefix(path_prefix);
|
let mut new_res = res.apply_path_prefix(path_prefix);
|
||||||
|
new_res.label = new_res.path.as_ref().map(|path| path.iter().map(|value| {
|
||||||
|
if let serde_json::Value::String(s) = value {
|
||||||
|
s.to_string()
|
||||||
|
} else {
|
||||||
|
value.to_string()
|
||||||
|
}
|
||||||
|
}).join("$"));
|
||||||
|
yield new_res;
|
||||||
}
|
}
|
||||||
if next_defer_list.is_empty() {
|
if next_defer_list.is_empty() {
|
||||||
break;
|
break;
|
||||||
|
@ -290,6 +318,7 @@ impl QueryBuilder {
|
||||||
env.extensions.execution_end();
|
env.extensions.execution_end();
|
||||||
|
|
||||||
let res = QueryResponse {
|
let res = QueryResponse {
|
||||||
|
label: None,
|
||||||
path: None,
|
path: None,
|
||||||
data,
|
data,
|
||||||
extensions: env.extensions.result(),
|
extensions: env.extensions.result(),
|
||||||
|
|
|
@ -141,6 +141,7 @@ impl SubscriptionTransport for WebSocketTransport {
|
||||||
id: Some(id.clone()),
|
id: Some(id.clone()),
|
||||||
payload: Some(
|
payload: Some(
|
||||||
serde_json::to_value(GQLResponse(Ok(QueryResponse {
|
serde_json::to_value(GQLResponse(Ok(QueryResponse {
|
||||||
|
label: None,
|
||||||
path: None,
|
path: None,
|
||||||
data: value,
|
data: value,
|
||||||
extensions: None,
|
extensions: None,
|
||||||
|
|
|
@ -76,6 +76,7 @@ impl<T: OutputValueType + Send + Sync + 'static> OutputValueType for Deferred<T>
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
QueryResponse {
|
QueryResponse {
|
||||||
|
label: None,
|
||||||
path: Some(path_prefix),
|
path: Some(path_prefix),
|
||||||
data,
|
data,
|
||||||
extensions: None,
|
extensions: None,
|
||||||
|
|
|
@ -87,6 +87,7 @@ impl<T: OutputValueType + Send + Sync + 'static> OutputValueType for Streamed<T>
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
QueryResponse {
|
QueryResponse {
|
||||||
|
label: None,
|
||||||
path: Some(path_prefix),
|
path: Some(path_prefix),
|
||||||
data,
|
data,
|
||||||
extensions: None,
|
extensions: None,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user