Skip to content
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

Fix enum string value #4

Merged
merged 8 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 18 additions & 8 deletions apollo-gateway-rs/src/handler/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl<'e> Executor<'e> {
self.resp.into_inner()
}
RootNode::Subscribe(_) => Response {
data: ConstValue::Null,
data: None,
errors: vec![ServerError {
message: "Not supported".to_string(),
path: Default::default(),
Expand Down Expand Up @@ -100,7 +100,7 @@ impl<'e> Executor<'e> {
id,
node.service,
RequestData::new(node.query.to_string())
.variables(node.variables.to_variables()),
.variables(node.variables.to_variables()),
tx.clone(),
)
.with_context(cx))
Expand All @@ -113,7 +113,7 @@ impl<'e> Executor<'e> {
vec![KEY_ERROR.string(err.to_string())],
);
Response {
data: ConstValue::Null,
data: None,
errors: vec![ServerError {
message: err.to_string(),
path: Default::default(),
Expand Down Expand Up @@ -193,7 +193,12 @@ impl<'e> Executor<'e> {
async fn execute_introspection_node(&self, introspection: &IntrospectionNode) {
let value = IntrospectionRoot.resolve(&introspection.selection_set, self.schema);
let mut current_resp = self.resp.lock().await;
merge_data(&mut current_resp.data, value);
if current_resp.data .is_none() {
current_resp.data = Some(ConstValue::Null)
}
if let Some(data) = &mut current_resp.data {
merge_data(data, value);
}
}

async fn execute_fetch_node(&self, fetcher: &impl Fetcher, fetch: &FetchNode<'_>) {
Expand Down Expand Up @@ -231,7 +236,12 @@ impl<'e> Executor<'e> {
if resp.errors.is_empty() {
add_tracing_spans(&mut resp);
current_resp.headers = resp.headers;
merge_data(&mut current_resp.data, resp.data);
if current_resp.data .is_none() {
current_resp.data = Some(ConstValue::Null)
}
if let Some(data) = &mut current_resp.data {
merge_data(data, resp.data.unwrap_or(ConstValue::Null));
}
} else {
rewrite_errors(None, &mut current_resp.errors, resp.errors);
}
Expand Down Expand Up @@ -407,7 +417,7 @@ impl<'e> Executor<'e> {
let mut resp = self.resp.lock().await;
get_representations(
&mut representations,
&mut resp.data,
resp.data.as_mut().unwrap_or(&mut ConstValue::Null),
&flatten.path,
flatten.prefix,
);
Expand Down Expand Up @@ -467,10 +477,10 @@ impl<'e> Executor<'e> {
Ok(mut resp) => {
if resp.errors.is_empty() {
add_tracing_spans(&mut resp);
if let ConstValue::Object(mut data) = resp.data {
if let ConstValue::Object(mut data) = resp.data.unwrap_or_default() {
if let Some(ConstValue::List(values)) = data.remove("_entities") {
flatten_values(
&mut current_resp.data,
current_resp.data.as_mut().unwrap_or(&mut ConstValue::Null),
&flatten.path,
&mut values.into_iter().fuse(),
&mut flags.into_iter().fuse(),
Expand Down
7 changes: 3 additions & 4 deletions apollo-gateway-rs/src/handler/shared_route_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use parser::types::{ExecutableDocument, Selection, SelectionSet};
use serde::Deserialize;
use tokio::sync::{mpsc, RwLock};
use tokio::time::{Duration, Instant};
use value::ConstValue;
use crate::datasource::RemoteGraphQLDataSource;
use crate::GraphqlSourceMiddleware;

Expand Down Expand Up @@ -116,7 +115,7 @@ impl<S: RemoteGraphQLDataSource + GraphqlSourceMiddleware> SharedRouteTable<S> {
.await
.with_context(|| format!("Failed to fetch SDL from '{}'.", service))?;
let resp: ResponseQuery =
value::from_value(resp.data).context("Failed to parse response.")?;
value::from_value(resp.data.unwrap_or_default()).context("Failed to parse response.")?;
let document = parser::parse_schema(resp.service.sdl)
.with_context(|| format!("Invalid SDL from '{}'.", service))?;
Ok::<_, Error>((service.to_string(), document))
Expand Down Expand Up @@ -156,7 +155,7 @@ impl<S: RemoteGraphQLDataSource + GraphqlSourceMiddleware> SharedRouteTable<S> {
Ok(_) => {},
Err(e) => {
let response = Response {
data: ConstValue::Null,
data: None,
errors: vec![e],
extensions: Default::default(),
headers: Default::default(),
Expand All @@ -175,7 +174,7 @@ impl<S: RemoteGraphQLDataSource + GraphqlSourceMiddleware> SharedRouteTable<S> {
Some((composed_schema, route_table)) => (composed_schema, route_table),
_ => {
let response = Response {
data: ConstValue::Null,
data: None,
errors: vec![ServerError::new("Not ready.")],
extensions: Default::default(),
headers: Default::default(),
Expand Down
3 changes: 1 addition & 2 deletions apollo-gateway-rs/src/handler/websocket/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use actix::{Actor, AsyncContext, ActorContext, Handler, StreamHandler};
use crate::schema::ComposedSchema;
use actix_web_actors::ws;
use actix_web_actors::ws::{CloseCode, CloseReason, Message, ProtocolError};
use value::ConstValue;
use crate::planner::{Response, ServerError};
use crate::{RemoteGraphQLDataSource, Context, ServiceRouteTable, GraphqlSourceMiddleware};
use super::protocol::{ClientMessage, ConnectionError, ServerMessage};
Expand Down Expand Up @@ -93,7 +92,7 @@ impl<S: RemoteGraphQLDataSource + GraphqlSourceMiddleware> StreamHandler<Result<
Ok(document) => document,
Err(err) => {
let resp = Response {
data: ConstValue::Null,
data: None,
errors: vec![ServerError::new(err.to_string())],
extensions: Default::default(),
headers: Default::default()
Expand Down
2 changes: 1 addition & 1 deletion apollo-gateway-rs/src/planner/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl<'a> PlanBuilder<'a> {
crate::validation::check_rules(self.schema, &self.document, &self.variables);
if !rule_errors.is_empty() {
return Err(Response {
data: ConstValue::Null,
data: None,
errors: rule_errors
.into_iter()
.map(|err| ServerError {
Expand Down
3 changes: 2 additions & 1 deletion apollo-gateway-rs/src/planner/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ impl ServerError {

#[derive(Debug, Serialize, Deserialize, Default)]
pub struct Response {
pub data: ConstValue,
#[serde(skip_serializing_if = "Option::is_none", default)]
pub data: Option<ConstValue>,

#[serde(skip_serializing_if = "Vec::is_empty", default)]
pub errors: Vec<ServerError>,
Expand Down
14 changes: 13 additions & 1 deletion apollo-gateway-rs/src/validation/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt::{Display, Formatter, Result as FmtResult};

use crate::schema::{ComposedSchema, TypeKind};
use parser::types::{BaseType, Type};
use value::ConstValue;
use value::{ConstValue, Name};

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Scope<'a> {
Expand Down Expand Up @@ -117,6 +117,18 @@ pub fn is_valid_input_value(
} else {
None
}
} else if let ConstValue::String(v) = value {
if ty.enum_values.contains_key(&Name::new(v.to_string())) {
None
} else {
Some(valid_error(
&path_node,
format!(
"enumeration type \"{}\" does not contain the value \"{}\"",
ty.name, value
)
))
}
} else {
Some(valid_error(
&path_node,
Expand Down
Loading