Skip to content

Apply type condition in inline fragments to fix panic when using inline fragments with interfaces #816

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

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
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