Skip to content
This repository was archived by the owner on Feb 22, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ async-tungstenite = { version = "0.31", features = [
"tokio-runtime",
] }
cynic = { version = "3" }
futures = { version = "0.3" }
futures = { version = "0.3.31" }
graphql_client = { version = "0.14" }
serde = "1"
tokio = { version = "1.15", features = ["rt-multi-thread", "macros"] }
tokio-tungstenite = "0.27"

[dependencies.graphql-ws-client]
path = "../"
version = "0.12.0"
default-features = false
features = ["cynic", "tungstenite-0.27"]
features = ["client-cynic", "client-graphql-client", "tungstenite-0.27"]

[lints]
workspace = true
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async fn main() {
use futures::StreamExt;
use graphql_ws_client::Client;

let mut request = "ws://localhost:8000".into_client_request().unwrap();
let mut request = "ws://localhost:8000/graphql".into_client_request().unwrap();
request.headers_mut().insert(
"Sec-WebSocket-Protocol",
HeaderValue::from_str("graphql-transport-ws").unwrap(),
Expand Down
69 changes: 69 additions & 0 deletions examples/examples/tokio-tungstenite.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//! An example of using subscriptions with `graphql-ws-client` and
//! `async-tungstenite`
//!
//! Talks to the the tide subscription example in `async-graphql`

use std::future::IntoFuture;

mod schema {
cynic::use_schema!("../schemas/books.graphql");
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema_path = "../schemas/books.graphql", graphql_type = "Book")]
#[allow(dead_code)]
struct Book {
id: String,
name: String,
author: String,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema_path = "../schemas/books.graphql", graphql_type = "BookChanged")]
#[allow(dead_code)]
struct BookChanged {
id: cynic::Id,
book: Option<Book>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(
schema_path = "../schemas/books.graphql",
graphql_type = "SubscriptionRoot"
)]
#[allow(dead_code)]
struct BooksChangedSubscription {
books: BookChanged,
}

#[tokio::main]
async fn main() {
use futures::StreamExt;
use graphql_ws_client::Client;
use tokio_tungstenite::tungstenite::{client::IntoClientRequest, http::HeaderValue};

let mut request = "ws://localhost:8000/graphql".into_client_request().unwrap();
request.headers_mut().insert(
"Sec-WebSocket-Protocol",
HeaderValue::from_str("graphql-transport-ws").unwrap(),
);

let (connection, _) = tokio_tungstenite::connect_async(request).await.unwrap();

println!("Connected");

let (client, actor) = Client::build(connection).await.unwrap();
tokio::spawn(actor.into_future());

let mut stream = client.subscribe(build_query()).await.unwrap();
println!("Running subscription apparently?");
while let Some(item) = stream.next().await {
println!("{item:?}");
}
}

fn build_query() -> cynic::StreamingOperation<BooksChangedSubscription> {
use cynic::SubscriptionBuilder;

BooksChangedSubscription::build(())
}