Skip to content

Commit

Permalink
Apply type condition in inline fragments
Browse files Browse the repository at this point in the history
This fixes a panic when using inline fragments with interfaces (graphql-rust#815)
  • Loading branch information
LukasKalbertodt committed Dec 9, 2020
1 parent 4ffd276 commit c392720
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 31 deletions.
42 changes: 23 additions & 19 deletions juniper/src/types/async_await.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,26 +323,30 @@ where
);

if let Some(ref type_condition) = fragment.type_condition {
let sub_result = instance
.resolve_into_type_async(
info,
type_condition.item,
Some(&fragment.selection_set[..]),
&sub_exec,
)
.await;

if let Ok(Value::Object(obj)) = sub_result {
for (k, v) in obj {
async_values.push(AsyncValueFuture::InlineFragment1(async move {
AsyncValue::Field(AsyncField {
name: k,
value: Some(v),
})
}));
// Check whether the type matches the type condition.
let concrete_type_name = instance.concrete_type_name(sub_exec.context(), info);
if type_condition.item == concrete_type_name {
let sub_result = instance
.resolve_into_type_async(
info,
type_condition.item,
Some(&fragment.selection_set[..]),
&sub_exec,
)
.await;

if let Ok(Value::Object(obj)) = sub_result {
for (k, v) in obj {
async_values.push(AsyncValueFuture::InlineFragment1(async move {
AsyncValue::Field(AsyncField {
name: k,
value: Some(v),
})
}));
}
} else if let Err(e) = sub_result {
sub_exec.push_error_at(e, *start_pos);
}
} else if let Err(e) = sub_result {
sub_exec.push_error_at(e, *start_pos);
}
} else {
async_values.push(AsyncValueFuture::InlineFragment2(async move {
Expand Down
28 changes: 16 additions & 12 deletions juniper/src/types/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,19 +532,23 @@ where
);

if let Some(ref type_condition) = fragment.type_condition {
let sub_result = instance.resolve_into_type(
info,
type_condition.item,
Some(&fragment.selection_set[..]),
&sub_exec,
);

if let Ok(Value::Object(object)) = sub_result {
for (k, v) in object {
merge_key_into(result, &k, v);
// Check whether the type matches the type condition.
let concrete_type_name = instance.concrete_type_name(sub_exec.context(), info);
if type_condition.item == concrete_type_name {
let sub_result = instance.resolve_into_type(
info,
type_condition.item,
Some(&fragment.selection_set[..]),
&sub_exec,
);

if let Ok(Value::Object(object)) = sub_result {
for (k, v) in object {
merge_key_into(result, &k, v);
}
} else if let Err(e) = sub_result {
sub_exec.push_error_at(e, *start_pos);
}
} else if let Err(e) = sub_result {
sub_exec.push_error_at(e, *start_pos);
}
} else if !resolve_selection_set_into(
instance,
Expand Down

0 comments on commit c392720

Please sign in to comment.