diff --git a/src/schema.rs b/src/schema.rs index dbe1d8a0..5f81f32f 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -251,9 +251,10 @@ where for definition in document.definitions { match definition { Definition::Operation(OperationDefinition::Subscription(s)) => { - if s.name.as_deref() == operation_name || operation_name.is_none() { + if subscription.is_none() + && (s.name.as_deref() == operation_name || operation_name.is_none()) + { subscription = Some(s); - break; } } Definition::Fragment(fragment) => { diff --git a/tests/subscription.rs b/tests/subscription.rs index 8a964d9d..966b982b 100644 --- a/tests/subscription.rs +++ b/tests/subscription.rs @@ -510,3 +510,59 @@ pub async fn test_subscription_fragment() { } assert!(stream.next().await.is_none()); } + +#[async_std::test] +pub async fn test_subscription_fragment2() { + struct QueryRoot; + + #[SimpleObject] + struct Event { + a: i32, + b: i32, + } + + #[Interface(field(name = "a", type = "i32"))] + struct MyInterface(Event); + + #[Object] + impl QueryRoot {} + + struct SubscriptionRoot; + + #[Subscription] + impl SubscriptionRoot { + async fn events(&self, start: i32, end: i32) -> impl Stream { + futures::stream::iter((start..end).map(|n| Event { a: n, b: n * 10 })) + } + } + + let schema = Schema::build(QueryRoot, EmptyMutation, SubscriptionRoot) + .register_type::() + .finish(); + let mut stream = schema + .create_subscription_stream( + r#" + subscription s { + events(start: 10, end: 20) { + ... Frag + } + } + + fragment Frag on Event { + a b + } + "#, + None, + Default::default(), + None, + ) + .await + .unwrap(); + for i in 10..20 { + assert_eq!( + Some(serde_json::json!({ "events": {"a": i, "b": i * 10} })), + stream.next().await + ); + } + assert!(stream.next().await.is_none()); +}