forked from obmarg/graphql-ws-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync-tungstenite-tokio.rs
More file actions
71 lines (58 loc) · 1.86 KB
/
Copy pathasync-tungstenite-tokio.rs
File metadata and controls
71 lines (58 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//! 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 async_tungstenite::tungstenite::{client::IntoClientRequest, http::HeaderValue};
use futures::StreamExt;
use graphql_ws_client::Client;
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, _) = async_tungstenite::tokio::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(())
}