Skip to content

Commit

Permalink
Add poem/subscription-redis example
Browse files Browse the repository at this point in the history
  • Loading branch information
sunli829 committed Jan 6, 2022
1 parent 09ba4b9 commit c821907
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ members = [

"poem/starwars",
"poem/subscription",
"poem/subscription-redis",
"poem/token-from-header",

"actix-web/token-from-header",
Expand Down
2 changes: 1 addition & 1 deletion poem/starwars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ async-graphql = { path = "../../.." }
async-graphql-poem = { path = "../../../integrations/poem" }
tokio = { version = "1.8", features = ["macros", "rt-multi-thread"] }
starwars = { path = "../../models/starwars" }
poem = "1.2.2"
poem = "1.2.25"
12 changes: 12 additions & 0 deletions poem/subscription-redis/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "subscription-redis"
version = "0.1.0"
edition = "2021"

[dependencies]
async-graphql = { path = "../../.." }
async-graphql-poem = { path = "../../../integrations/poem" }
tokio = { version = "1.8", features = ["macros", "rt-multi-thread"] }
poem = { version = "1.2.25", features = ["websocket"] }
redis = { version = "0.21.4", features = ["aio", "tokio-comp"] }
futures-util = "0.3.19"
72 changes: 72 additions & 0 deletions poem/subscription-redis/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use async_graphql::http::{playground_source, GraphQLPlaygroundConfig};
use async_graphql::{Context, Object, Result, Schema, Subscription};
use async_graphql_poem::{GraphQL, GraphQLSubscription};
use futures_util::{Stream, StreamExt};
use poem::listener::TcpListener;
use poem::web::Html;
use poem::{get, handler, IntoResponse, Route, Server};
use redis::{AsyncCommands, Client};

struct QueryRoot;

#[Object]
impl QueryRoot {
async fn version(&self) -> &'static str {
std::env!("CARGO_PKG_VERSION")
}
}

struct MutationRoot;

#[Object]
impl MutationRoot {
async fn publish(&self, ctx: &Context<'_>, value: String) -> Result<bool> {
let client = ctx.data_unchecked::<Client>();
let mut conn = client.get_async_connection().await?;
conn.publish("values", value).await?;
Ok(true)
}
}

struct SubscriptionRoot;

#[Subscription]
impl SubscriptionRoot {
async fn values(&self, ctx: &Context<'_>) -> Result<impl Stream<Item = String>> {
let client = ctx.data_unchecked::<Client>();
let mut conn = client.get_async_connection().await?.into_pubsub();
conn.subscribe("values").await?;
Ok(conn
.into_on_message()
.filter_map(|msg| async move { msg.get_payload().ok() }))
}
}

#[handler]
async fn graphql_playground() -> impl IntoResponse {
Html(playground_source(
GraphQLPlaygroundConfig::new("/").subscription_endpoint("/ws"),
))
}

#[tokio::main]
async fn main() {
let client = Client::open("redis://127.0.0.1/").unwrap();

let schema = Schema::build(QueryRoot, MutationRoot, SubscriptionRoot)
.data(client)
.finish();

let app = Route::new()
.at(
"/",
get(graphql_playground).post(GraphQL::new(schema.clone())),
)
.at("/ws", get(GraphQLSubscription::new(schema)));

println!("Playground: http://localhost:8000");
Server::new(TcpListener::bind("0.0.0.0:8000"))
.run(app)
.await
.unwrap();
}
2 changes: 1 addition & 1 deletion poem/subscription/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ async-graphql = { path = "../../.." }
async-graphql-poem = { path = "../../../integrations/poem" }
tokio = { version = "1.8", features = ["macros", "rt-multi-thread"] }
books = { path = "../../models/books" }
poem = { version = "1.2.2", features = ["websocket"] }
poem = { version = "1.2.25", features = ["websocket"] }
2 changes: 1 addition & 1 deletion poem/token-from-header/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ edition = "2021"
async-graphql = { path = "../../.." }
async-graphql-poem = { path = "../../../integrations/poem" }
token = { path = "../../models/token" }
poem = { version = "1.2.2", features = ["websocket"] }
poem = { version = "1.2.25", features = ["websocket"] }
tokio = { version = "1.8", features = ["macros", "rt-multi-thread"] }

0 comments on commit c821907

Please sign in to comment.