Skip to content

Commit a11c91a

Browse files
committed
No assumptions
1 parent f6b7080 commit a11c91a

File tree

2 files changed

+28
-25
lines changed

2 files changed

+28
-25
lines changed

lib/executor/src/projection/plan.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ pub enum ProjectionValueSource {
3232
#[derive(Debug, Clone)]
3333
pub struct FieldProjectionPlan {
3434
pub field_name: String,
35-
pub field_type: String,
3635
pub response_key: String,
3736
pub conditions: FieldProjectionCondition,
3837
pub value: ProjectionValueSource,
@@ -264,7 +263,6 @@ impl FieldProjectionPlan {
264263

265264
let new_plan = FieldProjectionPlan {
266265
field_name: field_name.to_string(),
267-
field_type: field_type.clone(),
268266
response_key,
269267
conditions: condition_for_field,
270268
value: ProjectionValueSource::ResponseData {
@@ -322,7 +320,6 @@ impl FieldProjectionPlan {
322320
pub fn with_new_value(&self, new_value: ProjectionValueSource) -> FieldProjectionPlan {
323321
FieldProjectionPlan {
324322
field_name: self.field_name.clone(),
325-
field_type: self.field_type.clone(),
326323
response_key: self.response_key.clone(),
327324
conditions: self.conditions.clone(),
328325
value: new_value,

lib/executor/src/projection/response.rs

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub fn project_by_operation(
4545
&mut errors,
4646
selections,
4747
variable_values,
48-
operation_type_name,
48+
Some(operation_type_name),
4949
&mut buffer,
5050
&mut first,
5151
);
@@ -153,9 +153,8 @@ fn project_selection_set(
153153
selections: Some(selections),
154154
} => {
155155
let mut first = true;
156-
let type_name = get_value_by_key(obj, TYPENAME_FIELD_NAME)
157-
.and_then(|v| v.as_str())
158-
.unwrap_or(selection.field_type.as_str());
156+
let type_name =
157+
get_value_by_key(obj, TYPENAME_FIELD_NAME).and_then(|v| v.as_str());
159158
project_selection_set_with_map(
160159
obj,
161160
errors,
@@ -193,7 +192,7 @@ fn project_selection_set(
193192
fn project_field(
194193
field_val: Option<&Value>,
195194
plan: &FieldProjectionPlan,
196-
parent_type_name: &str,
195+
parent_type_name: Option<&str>,
197196
variable_values: &Option<HashMap<String, sonic_rs::Value>>,
198197
buffer: &mut Vec<u8>,
199198
first: &mut bool,
@@ -280,10 +279,14 @@ fn project_field(
280279
if let Some(field_val) = field_val {
281280
project_selection_set(field_val, errors, plan, variable_values, buffer);
282281
} else if plan.field_name == TYPENAME_FIELD_NAME {
283-
// If the field is TYPENAME_FIELD, we should set it to the parent type name
284-
buffer.put(QUOTE);
285-
buffer.put(parent_type_name.as_bytes());
286-
buffer.put(QUOTE);
282+
if let Some(parent_type_name) = parent_type_name {
283+
// If the field is TYPENAME_FIELD, we should set it to the parent type name
284+
buffer.put(QUOTE);
285+
buffer.put(parent_type_name.as_bytes());
286+
buffer.put(QUOTE);
287+
} else {
288+
buffer.put(NULL);
289+
}
287290
} else {
288291
// If the field is not found in the object, set it to Null
289292
buffer.put(NULL);
@@ -379,7 +382,7 @@ fn project_selection_set_with_map(
379382
errors: &mut Vec<GraphQLError>,
380383
plans: &IndexMap<String, FieldProjectionPlan>,
381384
variable_values: &Option<HashMap<String, sonic_rs::Value>>,
382-
parent_type_name: &str,
385+
parent_type_name: Option<&str>,
383386
buffer: &mut Vec<u8>,
384387
first: &mut bool,
385388
) {
@@ -400,7 +403,7 @@ fn project_selection_set_with_map(
400403

401404
fn check<'a>(
402405
cond: &'a FieldProjectionCondition,
403-
parent_type_name: &'a str,
406+
parent_type_name: Option<&'a str>,
404407
field_type_name: Option<&'a str>,
405408
field_value: Option<&'a Value>,
406409
variable_values: &'a Option<HashMap<String, sonic_rs::Value>>,
@@ -464,18 +467,21 @@ fn check<'a>(
464467
Ok(())
465468
}
466469
FieldProjectionCondition::ParentTypeCondition(type_condition) => {
467-
let is_valid = match type_condition {
468-
TypeCondition::Exact(expected_type) => parent_type_name == expected_type,
469-
TypeCondition::OneOf(possible_types) => possible_types.contains(parent_type_name),
470-
};
471-
if is_valid {
472-
Ok(())
473-
} else {
474-
Err(FieldProjectionConditionError::InvalidParentType {
475-
expected: type_condition,
476-
found: parent_type_name,
477-
})
470+
if let Some(parent_type_name) = parent_type_name {
471+
let is_valid = match type_condition {
472+
TypeCondition::Exact(expected_type) => parent_type_name == expected_type,
473+
TypeCondition::OneOf(possible_types) => {
474+
possible_types.contains(parent_type_name)
475+
}
476+
};
477+
if !is_valid {
478+
return Err(FieldProjectionConditionError::InvalidParentType {
479+
expected: type_condition,
480+
found: parent_type_name,
481+
});
482+
}
478483
}
484+
Ok(())
479485
}
480486
FieldProjectionCondition::FieldTypeCondition(type_condition) => {
481487
if let Some(field_type_name) = field_type_name {

0 commit comments

Comments
 (0)