async-graphql/src/subscription/subscription_type.rs

111 lines
4.0 KiB
Rust
Raw Normal View History

use crate::context::Environment;
use crate::parser::ast::{Selection, TypeCondition};
use crate::{Context, ContextSelectionSet, ObjectType, Result, Schema, Type};
2020-04-07 06:30:46 +00:00
use futures::{Future, Stream};
use std::pin::Pin;
use std::sync::Arc;
2020-03-29 12:02:52 +00:00
/// Represents a GraphQL subscription object
#[async_trait::async_trait]
pub trait SubscriptionType: Type {
/// This function returns true of type `EmptySubscription` only
#[doc(hidden)]
fn is_empty() -> bool {
false
}
#[doc(hidden)]
2020-04-07 06:30:46 +00:00
async fn create_field_stream<Query, Mutation>(
2020-03-29 12:02:52 +00:00
&self,
ctx: &Context<'_>,
schema: &Schema<Query, Mutation, Self>,
environment: Arc<Environment>,
) -> Result<Pin<Box<dyn Stream<Item = Result<serde_json::Value>> + Send>>>
where
Query: ObjectType + Send + Sync + 'static,
Mutation: ObjectType + Send + Sync + 'static,
Self: Send + Sync + 'static + Sized;
}
2020-04-07 06:30:46 +00:00
type BoxCreateStreamFuture<'a> = Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>>;
pub fn create_subscription_stream<'a, Query, Mutation, Subscription>(
schema: &'a Schema<Query, Mutation, Subscription>,
environment: Arc<Environment>,
2020-04-07 06:30:46 +00:00
ctx: &'a ContextSelectionSet<'_>,
streams: &'a mut Vec<Pin<Box<dyn Stream<Item = Result<serde_json::Value>> + Send>>>,
2020-04-07 06:30:46 +00:00
) -> BoxCreateStreamFuture<'a>
where
Query: ObjectType + Send + Sync + 'static,
Mutation: ObjectType + Send + Sync + 'static,
Subscription: SubscriptionType + Send + Sync + 'static + Sized,
{
2020-04-07 06:30:46 +00:00
Box::pin(async move {
for selection in &ctx.items {
match &selection.node {
2020-04-07 06:30:46 +00:00
Selection::Field(field) => {
if ctx.is_skip(&field.directives)? {
continue;
}
streams.push(
schema
.0
.subscription
.create_field_stream(
&ctx.with_field(field),
schema,
environment.clone(),
)
.await?,
)
}
2020-04-07 06:30:46 +00:00
Selection::FragmentSpread(fragment_spread) => {
if ctx.is_skip(&fragment_spread.directives)? {
continue;
}
2020-04-07 06:30:46 +00:00
if let Some(fragment) =
ctx.fragments.get(fragment_spread.fragment_name.as_str())
{
create_subscription_stream(
schema,
environment.clone(),
&ctx.with_selection_set(&fragment.selection_set),
streams,
)
.await?;
}
}
2020-04-07 06:30:46 +00:00
Selection::InlineFragment(inline_fragment) => {
if ctx.is_skip(&inline_fragment.directives)? {
continue;
}
if let Some(TypeCondition::On(name)) =
inline_fragment.type_condition.as_ref().map(|v| &v.node)
{
2020-04-07 06:30:46 +00:00
if name.as_str() == Subscription::type_name() {
create_subscription_stream(
schema,
environment.clone(),
&ctx.with_selection_set(&inline_fragment.selection_set),
streams,
)
.await?;
}
} else {
create_subscription_stream(
schema,
environment.clone(),
&ctx.with_selection_set(&inline_fragment.selection_set),
streams,
2020-04-07 06:30:46 +00:00
)
.await?;
}
}
}
}
2020-04-07 06:30:46 +00:00
Ok(())
})
2020-03-29 12:02:52 +00:00
}