Function async_graphql_warp::graphql_subscription[][src]

pub fn graphql_subscription<Query, Mutation, Subscription>(
    schema: Schema<Query, Mutation, Subscription>
) -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone where
    Query: ObjectType + Sync + Send + 'static,
    Mutation: ObjectType + Sync + Send + 'static,
    Subscription: SubscriptionType + Send + Sync + 'static, 
Expand description

GraphQL subscription filter

Examples

use async_graphql::*;
use async_graphql_warp::*;
use warp::Filter;
use futures_util::stream::{Stream, StreamExt};
use std::time::Duration;

struct QueryRoot;

#[Object]
impl QueryRoot {
    async fn value(&self) -> i32 {
        // A GraphQL Object type must define one or more fields.
        100
    }
}

struct SubscriptionRoot;

#[Subscription]
impl SubscriptionRoot {
    async fn tick(&self) -> impl Stream<Item = String> {
        async_stream::stream! {
            let mut interval = tokio::time::interval(Duration::from_secs(1));
            loop {
                let n = interval.tick().await;
                yield format!("{}", n.elapsed().as_secs_f32());
            }
        }
    }
}

tokio::runtime::Runtime::new().unwrap().block_on(async {
    let schema = Schema::new(QueryRoot, EmptyMutation, SubscriptionRoot);
    let filter = async_graphql_warp::graphql_subscription(schema)
        .or(warp::any().map(|| "Hello, World!"));
    warp::serve(filter).run(([0, 0, 0, 0], 8000)).await;
});