Skip to content

Commit 5d3df9e

Browse files
authored
Merge pull request #4 from ZirgVoice/fix-enum-string-value
Fix enum string value
2 parents 454178a + f7378af commit 5d3df9e

File tree

7 files changed

+39
-18
lines changed

7 files changed

+39
-18
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apollo-gateway-rs/src/handler/executor.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl<'e> Executor<'e> {
4242
self.resp.into_inner()
4343
}
4444
RootNode::Subscribe(_) => Response {
45-
data: ConstValue::Null,
45+
data: None,
4646
errors: vec![ServerError {
4747
message: "Not supported".to_string(),
4848
path: Default::default(),
@@ -100,7 +100,7 @@ impl<'e> Executor<'e> {
100100
id,
101101
node.service,
102102
RequestData::new(node.query.to_string())
103-
.variables(node.variables.to_variables()),
103+
.variables(node.variables.to_variables()),
104104
tx.clone(),
105105
)
106106
.with_context(cx))
@@ -113,7 +113,7 @@ impl<'e> Executor<'e> {
113113
vec![KEY_ERROR.string(err.to_string())],
114114
);
115115
Response {
116-
data: ConstValue::Null,
116+
data: None,
117117
errors: vec![ServerError {
118118
message: err.to_string(),
119119
path: Default::default(),
@@ -193,7 +193,12 @@ impl<'e> Executor<'e> {
193193
async fn execute_introspection_node(&self, introspection: &IntrospectionNode) {
194194
let value = IntrospectionRoot.resolve(&introspection.selection_set, self.schema);
195195
let mut current_resp = self.resp.lock().await;
196-
merge_data(&mut current_resp.data, value);
196+
if current_resp.data .is_none() {
197+
current_resp.data = Some(ConstValue::Null)
198+
}
199+
if let Some(data) = &mut current_resp.data {
200+
merge_data(data, value);
201+
}
197202
}
198203

199204
async fn execute_fetch_node(&self, fetcher: &impl Fetcher, fetch: &FetchNode<'_>) {
@@ -231,7 +236,12 @@ impl<'e> Executor<'e> {
231236
if resp.errors.is_empty() {
232237
add_tracing_spans(&mut resp);
233238
current_resp.headers = resp.headers;
234-
merge_data(&mut current_resp.data, resp.data);
239+
if current_resp.data .is_none() {
240+
current_resp.data = Some(ConstValue::Null)
241+
}
242+
if let Some(data) = &mut current_resp.data {
243+
merge_data(data, resp.data.unwrap_or(ConstValue::Null));
244+
}
235245
} else {
236246
rewrite_errors(None, &mut current_resp.errors, resp.errors);
237247
}
@@ -407,7 +417,7 @@ impl<'e> Executor<'e> {
407417
let mut resp = self.resp.lock().await;
408418
get_representations(
409419
&mut representations,
410-
&mut resp.data,
420+
resp.data.as_mut().unwrap_or(&mut ConstValue::Null),
411421
&flatten.path,
412422
flatten.prefix,
413423
);
@@ -467,10 +477,10 @@ impl<'e> Executor<'e> {
467477
Ok(mut resp) => {
468478
if resp.errors.is_empty() {
469479
add_tracing_spans(&mut resp);
470-
if let ConstValue::Object(mut data) = resp.data {
480+
if let ConstValue::Object(mut data) = resp.data.unwrap_or_default() {
471481
if let Some(ConstValue::List(values)) = data.remove("_entities") {
472482
flatten_values(
473-
&mut current_resp.data,
483+
current_resp.data.as_mut().unwrap_or(&mut ConstValue::Null),
474484
&flatten.path,
475485
&mut values.into_iter().fuse(),
476486
&mut flags.into_iter().fuse(),

apollo-gateway-rs/src/handler/shared_route_table.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use parser::types::{ExecutableDocument, Selection, SelectionSet};
1111
use serde::Deserialize;
1212
use tokio::sync::{mpsc, RwLock};
1313
use tokio::time::{Duration, Instant};
14-
use value::ConstValue;
1514
use crate::datasource::RemoteGraphQLDataSource;
1615
use crate::GraphqlSourceMiddleware;
1716

@@ -116,7 +115,7 @@ impl<S: RemoteGraphQLDataSource + GraphqlSourceMiddleware> SharedRouteTable<S> {
116115
.await
117116
.with_context(|| format!("Failed to fetch SDL from '{}'.", service))?;
118117
let resp: ResponseQuery =
119-
value::from_value(resp.data).context("Failed to parse response.")?;
118+
value::from_value(resp.data.unwrap_or_default()).context("Failed to parse response.")?;
120119
let document = parser::parse_schema(resp.service.sdl)
121120
.with_context(|| format!("Invalid SDL from '{}'.", service))?;
122121
Ok::<_, Error>((service.to_string(), document))
@@ -156,7 +155,7 @@ impl<S: RemoteGraphQLDataSource + GraphqlSourceMiddleware> SharedRouteTable<S> {
156155
Ok(_) => {},
157156
Err(e) => {
158157
let response = Response {
159-
data: ConstValue::Null,
158+
data: None,
160159
errors: vec![e],
161160
extensions: Default::default(),
162161
headers: Default::default(),
@@ -175,7 +174,7 @@ impl<S: RemoteGraphQLDataSource + GraphqlSourceMiddleware> SharedRouteTable<S> {
175174
Some((composed_schema, route_table)) => (composed_schema, route_table),
176175
_ => {
177176
let response = Response {
178-
data: ConstValue::Null,
177+
data: None,
179178
errors: vec![ServerError::new("Not ready.")],
180179
extensions: Default::default(),
181180
headers: Default::default(),

apollo-gateway-rs/src/handler/websocket/subscription.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use actix::{Actor, AsyncContext, ActorContext, Handler, StreamHandler};
44
use crate::schema::ComposedSchema;
55
use actix_web_actors::ws;
66
use actix_web_actors::ws::{CloseCode, CloseReason, Message, ProtocolError};
7-
use value::ConstValue;
87
use crate::planner::{Response, ServerError};
98
use crate::{RemoteGraphQLDataSource, Context, ServiceRouteTable, GraphqlSourceMiddleware};
109
use super::protocol::{ClientMessage, ConnectionError, ServerMessage};
@@ -93,7 +92,7 @@ impl<S: RemoteGraphQLDataSource + GraphqlSourceMiddleware> StreamHandler<Result<
9392
Ok(document) => document,
9493
Err(err) => {
9594
let resp = Response {
96-
data: ConstValue::Null,
95+
data: None,
9796
errors: vec![ServerError::new(err.to_string())],
9897
extensions: Default::default(),
9998
headers: Default::default()

apollo-gateway-rs/src/planner/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl<'a> PlanBuilder<'a> {
6262
crate::validation::check_rules(self.schema, &self.document, &self.variables);
6363
if !rule_errors.is_empty() {
6464
return Err(Response {
65-
data: ConstValue::Null,
65+
data: None,
6666
errors: rule_errors
6767
.into_iter()
6868
.map(|err| ServerError {

apollo-gateway-rs/src/planner/response.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ impl ServerError {
3838

3939
#[derive(Debug, Serialize, Deserialize, Default)]
4040
pub struct Response {
41-
pub data: ConstValue,
41+
#[serde(skip_serializing_if = "Option::is_none", default)]
42+
pub data: Option<ConstValue>,
4243

4344
#[serde(skip_serializing_if = "Vec::is_empty", default)]
4445
pub errors: Vec<ServerError>,

apollo-gateway-rs/src/validation/utils.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fmt::{Display, Formatter, Result as FmtResult};
33

44
use crate::schema::{ComposedSchema, TypeKind};
55
use parser::types::{BaseType, Type};
6-
use value::ConstValue;
6+
use value::{ConstValue, Name};
77

88
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
99
pub enum Scope<'a> {
@@ -117,6 +117,18 @@ pub fn is_valid_input_value(
117117
} else {
118118
None
119119
}
120+
} else if let ConstValue::String(v) = value {
121+
if ty.enum_values.contains_key(&Name::new(v.to_string())) {
122+
None
123+
} else {
124+
Some(valid_error(
125+
&path_node,
126+
format!(
127+
"enumeration type \"{}\" does not contain the value \"{}\"",
128+
ty.name, value
129+
)
130+
))
131+
}
120132
} else {
121133
Some(valid_error(
122134
&path_node,

0 commit comments

Comments
 (0)