Skip to content

Commit f044344

Browse files
authored
Fix subscription error extensions (#927)
1 parent 35b6643 commit f044344

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
use juniper::*;
2+
3+
use futures::stream::BoxStream;
4+
5+
#[derive(juniper::GraphQLObject)]
6+
struct User {
7+
name: String,
8+
}
9+
10+
struct Error;
11+
12+
impl<S: ScalarValue> IntoFieldError<S> for Error {
13+
fn into_field_error(self) -> FieldError<S> {
14+
let a = Value::Scalar(S::from(42));
15+
let mut extensions = Object::with_capacity(1);
16+
let _ = extensions.add_field("a", a);
17+
FieldError::new("oops", Value::Object(extensions))
18+
}
19+
}
20+
21+
struct SubscriptionsRoot;
22+
23+
#[graphql_subscription(name = "Subscription")]
24+
impl SubscriptionsRoot {
25+
async fn users() -> Result<BoxStream<'static, User>, Error> {
26+
Ok(users_stream()?)
27+
}
28+
}
29+
30+
fn users_stream() -> Result<BoxStream<'static, User>, Error> {
31+
Err(Error)
32+
}
33+
34+
struct Query;
35+
36+
#[juniper::graphql_object]
37+
impl Query {
38+
fn users() -> Vec<User> {
39+
vec![]
40+
}
41+
}
42+
43+
type Schema = juniper::RootNode<'static, Query, EmptyMutation<()>, SubscriptionsRoot>;
44+
45+
#[tokio::test]
46+
async fn test_error_extensions() {
47+
let sub = r#"
48+
subscription Users {
49+
users {
50+
name
51+
}
52+
}
53+
"#;
54+
55+
let (_, errors) = juniper::resolve_into_stream(
56+
sub,
57+
None,
58+
&Schema::new(Query, EmptyMutation::new(), SubscriptionsRoot),
59+
&Variables::new(),
60+
&(),
61+
)
62+
.await
63+
.unwrap();
64+
65+
assert_eq!(
66+
errors.first().unwrap().error().extensions(),
67+
&graphql_value!({ "a": 42 })
68+
);
69+
}

integration_tests/juniper_tests/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ mod issue_914;
2323
#[cfg(test)]
2424
mod issue_922;
2525
#[cfg(test)]
26+
mod issue_925;
27+
#[cfg(test)]
2628
mod pre_parse;

juniper_codegen/src/util/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1271,7 +1271,7 @@ impl GraphQLTypeDefiniton {
12711271
quote!(
12721272
#name => {
12731273
::juniper::futures::FutureExt::boxed(async move {
1274-
let res #_type = { #code };
1274+
let res #_type = async { #code }.await;
12751275
let res = ::juniper::IntoFieldResult::<_, #scalar>::into_result(res)?;
12761276
let executor= executor.as_owned_executor();
12771277
let f = res.then(move |res| {

0 commit comments

Comments
 (0)