-
Notifications
You must be signed in to change notification settings - Fork 272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support the incremental response field #1551
Changes from all commits
0aea472
b707d75
eabe2a3
0e488f6
64bc983
7a82e47
b495720
6f27fd3
b3dd1fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,12 +42,16 @@ pub struct Response { | |
|
||
#[serde(skip_serializing)] | ||
pub subselection: Option<String>, | ||
|
||
#[serde(skip_serializing_if = "Vec::is_empty", default)] | ||
pub incremental: Vec<IncrementalResponse>, | ||
} | ||
|
||
#[buildstructor::buildstructor] | ||
impl Response { | ||
/// Constructor | ||
#[builder(visibility = "pub")] | ||
#[allow(clippy::too_many_arguments)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is done automagically since 0.5.0 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here the lint was still complaining, unfortunately :/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. interesting, we should open an issue |
||
fn new( | ||
label: Option<String>, | ||
data: Option<Value>, | ||
|
@@ -56,6 +60,7 @@ impl Response { | |
extensions: Map<ByteString, Value>, | ||
subselection: Option<String>, | ||
has_next: Option<bool>, | ||
incremental: Vec<IncrementalResponse>, | ||
) -> Self { | ||
Self { | ||
label, | ||
|
@@ -65,10 +70,11 @@ impl Response { | |
extensions, | ||
subselection, | ||
has_next, | ||
incremental, | ||
} | ||
} | ||
|
||
/// If path is None, this is a primary query. | ||
/// If path is None, this is a primary response. | ||
pub fn is_primary(&self) -> bool { | ||
self.path.is_none() | ||
} | ||
|
@@ -128,6 +134,24 @@ impl Response { | |
service: service_name.to_string(), | ||
reason: err.to_string(), | ||
})?; | ||
let incremental = | ||
extract_key_value_from_object!(object, "incremental", Value::Array(a) => a).map_err( | ||
o0Ignition0o marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|err| FetchError::SubrequestMalformedResponse { | ||
service: service_name.to_string(), | ||
reason: err.to_string(), | ||
}, | ||
)?; | ||
let incremental: Vec<IncrementalResponse> = match incremental { | ||
Some(v) => v | ||
.into_iter() | ||
.map(serde_json_bytes::from_value) | ||
.collect::<Result<Vec<IncrementalResponse>, _>>() | ||
.map_err(|err| FetchError::SubrequestMalformedResponse { | ||
service: service_name.to_string(), | ||
reason: err.to_string(), | ||
})?, | ||
None => vec![], | ||
}; | ||
|
||
Ok(Response { | ||
label, | ||
|
@@ -137,10 +161,62 @@ impl Response { | |
extensions, | ||
subselection: None, | ||
has_next, | ||
incremental, | ||
}) | ||
} | ||
} | ||
|
||
/// A graphql incremental response. | ||
/// Used with `@defer` | ||
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Default)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct IncrementalResponse { | ||
/// The label that was passed to the defer or stream directive for this patch. | ||
#[serde(skip_serializing_if = "Option::is_none", default)] | ||
pub label: Option<String>, | ||
|
||
/// The response data. | ||
#[serde(skip_serializing_if = "Option::is_none", default)] | ||
pub data: Option<Value>, | ||
|
||
/// The path that the data should be merged at. | ||
#[serde(skip_serializing_if = "Option::is_none", default)] | ||
pub path: Option<Path>, | ||
|
||
/// The optional graphql errors encountered. | ||
#[serde(skip_serializing_if = "Vec::is_empty", default)] | ||
pub errors: Vec<Error>, | ||
|
||
/// The optional graphql extensions. | ||
#[serde(skip_serializing_if = "Object::is_empty", default)] | ||
pub extensions: Object, | ||
} | ||
|
||
#[buildstructor::buildstructor] | ||
impl IncrementalResponse { | ||
/// Constructor | ||
#[builder(visibility = "pub")] | ||
fn new( | ||
label: Option<String>, | ||
data: Option<Value>, | ||
path: Option<Path>, | ||
errors: Vec<Error>, | ||
extensions: Map<ByteString, Value>, | ||
) -> Self { | ||
Self { | ||
label, | ||
data, | ||
path, | ||
errors, | ||
extensions, | ||
} | ||
} | ||
|
||
/// append_errors default the errors `path` with the one provided. | ||
pub fn append_errors(&mut self, errors: &mut Vec<Error>) { | ||
self.errors.append(errors) | ||
} | ||
} | ||
#[cfg(test)] | ||
mod tests { | ||
use serde_json::json; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
source: apollo-router/tests/integration_tests.rs | ||
assertion_line: 679 | ||
expression: second | ||
--- | ||
{ | ||
"hasNext": true, | ||
"incremental": [ | ||
{ | ||
"label": "name", | ||
"data": { | ||
"name": "Ada Lovelace" | ||
}, | ||
"path": [ | ||
"me" | ||
] | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
source: apollo-router/tests/integration_tests.rs | ||
assertion_line: 686 | ||
expression: first | ||
--- | ||
{ | ||
"data": { | ||
"me": { | ||
"id": "1" | ||
} | ||
}, | ||
"hasNext": true | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😅