9849736582
- consider `juniper_graphql_transport_ws` crate on CI - implement auto-selection of protocol in `juniper_warp` crate - support `graphql-transport-ws` protocol in `juniper_actix` crate - implement auto-selection of protocol in `juniper_actix` crate Additionally: - move `examples/warp_subscriptions` into `juniper_warp/examples/subscription.rs` - move `examples/actix_subscriptions` into `juniper_actix/examples/subscription.rs` - move `examples/basic_subscriptions` into `juniper_subscriptions/examples/basic.rs` - bump up MSRV of `juniper_actix` crate to 1.68
66 lines
1.6 KiB
Rust
66 lines
1.6 KiB
Rust
#![deny(warnings)]
|
|
|
|
use std::pin::Pin;
|
|
|
|
use futures::{Stream, StreamExt};
|
|
use juniper::{
|
|
graphql_object, graphql_subscription, http::GraphQLRequest, DefaultScalarValue, EmptyMutation,
|
|
FieldError, RootNode, SubscriptionCoordinator,
|
|
};
|
|
use juniper_subscriptions::Coordinator;
|
|
|
|
#[derive(Clone)]
|
|
pub struct Database;
|
|
|
|
impl juniper::Context for Database {}
|
|
|
|
impl Database {
|
|
fn new() -> Self {
|
|
Self
|
|
}
|
|
}
|
|
|
|
pub struct Query;
|
|
|
|
#[graphql_object(context = Database)]
|
|
impl Query {
|
|
fn hello_world() -> &'static str {
|
|
"Hello World!"
|
|
}
|
|
}
|
|
|
|
pub struct Subscription;
|
|
|
|
type StringStream = Pin<Box<dyn Stream<Item = Result<String, FieldError>> + Send>>;
|
|
|
|
#[graphql_subscription(context = Database)]
|
|
impl Subscription {
|
|
async fn hello_world() -> StringStream {
|
|
let stream =
|
|
futures::stream::iter(vec![Ok(String::from("Hello")), Ok(String::from("World!"))]);
|
|
Box::pin(stream)
|
|
}
|
|
}
|
|
|
|
type Schema = RootNode<'static, Query, EmptyMutation<Database>, Subscription>;
|
|
|
|
fn schema() -> Schema {
|
|
Schema::new(Query, EmptyMutation::new(), Subscription)
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() {
|
|
let schema = schema();
|
|
let coordinator = Coordinator::new(schema);
|
|
let req: GraphQLRequest<DefaultScalarValue> = serde_json::from_str(
|
|
r#"{
|
|
"query": "subscription { helloWorld }"
|
|
}"#,
|
|
)
|
|
.unwrap();
|
|
let ctx = Database::new();
|
|
let mut conn = coordinator.subscribe(&req, &ctx).await.unwrap();
|
|
while let Some(result) = conn.next().await {
|
|
println!("{}", serde_json::to_string(&result).unwrap());
|
|
}
|
|
}
|