Skip to content

Commit

Permalink
Check type before resolving in inline fragments to fix panic when usi…
Browse files Browse the repository at this point in the history
…ng inline fragments with interfaces (#816, #815)
  • Loading branch information
LukasKalbertodt authored Dec 9, 2020
1 parent 4ffd276 commit 2c15ea7
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 31 deletions.
4 changes: 4 additions & 0 deletions juniper/src/schema/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ where
QueryT::name(info)
}

fn concrete_type_name(&self, context: &Self::Context, info: &Self::TypeInfo) -> String {
self.query_type.concrete_type_name(context, info)
}

fn resolve_field(
&self,
info: &Self::TypeInfo,
Expand Down
34 changes: 34 additions & 0 deletions juniper/src/tests/query_tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
ast::InputValue,
executor::Variables,
graphql_value,
schema::model::RootNode,
tests::fixtures::starwars::schema::{Database, Query},
types::scalars::{EmptyMutation, EmptySubscription},
Expand Down Expand Up @@ -731,3 +732,36 @@ async fn test_object_typename() {
))
);
}

#[tokio::test]
async fn interface_inline_fragment_friends() {
let doc = r#"{
human(id: "1002") {
friends {
name
... on Human { homePlanet }
... on Droid { primaryFunction }
}
}
}"#;
let database = Database::new();
let schema = RootNode::new(
Query,
EmptyMutation::<Database>::new(),
EmptySubscription::<Database>::new(),
);

assert_eq!(
crate::execute(doc, None, &schema, &Variables::new(), &database).await,
Ok((
graphql_value!({"human": {
"friends": [
{"name": "Luke Skywalker", "homePlanet": "Tatooine"},
{"name": "Leia Organa", "homePlanet": "Alderaan"},
{"name": "R2-D2", "primaryFunction": "Astromech"},
],
}}),
vec![],
))
);
}
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 2c15ea7

Please sign in to comment.