-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests & an example for graphql_client (#71)
been putting this off forever
- Loading branch information
Showing
7 changed files
with
221 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
examples/examples/mulitiple-subscriptions.rs → ...examples/cynic-mulitiple-subscriptions.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
examples/examples/single-subscription.rs → ...les/examples/cynic-single-subscription.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
//! An example of creating a connection and running a single subscription on it | ||
//! using `graphql-client` and `async-tungstenite` | ||
//! | ||
//! Talks to the the tide subscription example in `async-graphql` | ||
|
||
use futures::StreamExt; | ||
use graphql_client::GraphQLQuery; | ||
use graphql_ws_client::graphql::StreamingOperation; | ||
|
||
#[derive(GraphQLQuery)] | ||
#[graphql( | ||
query_path = "examples/graphql-client-subscription.graphql", | ||
schema_path = "../schemas/books.graphql", | ||
response_derives = "Debug" | ||
)] | ||
struct BooksChanged; | ||
|
||
#[async_std::main] | ||
async fn main() { | ||
use async_tungstenite::tungstenite::{client::IntoClientRequest, http::HeaderValue}; | ||
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::async_std::connect_async(request) | ||
.await | ||
.unwrap(); | ||
|
||
println!("Connected"); | ||
|
||
let mut subscription = Client::build(connection) | ||
.subscribe(StreamingOperation::<BooksChanged>::new( | ||
books_changed::Variables, | ||
)) | ||
.await | ||
.unwrap(); | ||
|
||
while let Some(item) = subscription.next().await { | ||
println!("{:?}", item); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
subscription BooksChanged { | ||
books(mutationType: CREATED) { | ||
id | ||
book { | ||
id | ||
name | ||
author | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
subscription BooksChanged { | ||
books(mutationType: CREATED) { | ||
id | ||
book { | ||
id | ||
name | ||
author | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
use std::{future::IntoFuture, time::Duration}; | ||
|
||
use assert_matches::assert_matches; | ||
use graphql_client::GraphQLQuery; | ||
use graphql_ws_client::graphql::StreamingOperation; | ||
use subscription_server::SubscriptionServer; | ||
use tokio::time::sleep; | ||
|
||
mod subscription_server; | ||
|
||
#[derive(GraphQLQuery)] | ||
#[graphql( | ||
query_path = "tests/graphql-client-subscription.graphql", | ||
schema_path = "schemas/books.graphql", | ||
response_derives = "Debug" | ||
)] | ||
struct BooksChanged; | ||
|
||
#[tokio::test] | ||
async fn main_test() { | ||
use async_tungstenite::tungstenite::{client::IntoClientRequest, http::HeaderValue}; | ||
use futures::StreamExt; | ||
|
||
let server = SubscriptionServer::start().await; | ||
|
||
sleep(Duration::from_millis(20)).await; | ||
|
||
let mut request = server.websocket_url().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 (mut client, actor) = graphql_ws_client::Client::build(connection).await.unwrap(); | ||
|
||
tokio::spawn(actor.into_future()); | ||
|
||
let stream = client.subscribe(build_query()).await.unwrap(); | ||
|
||
sleep(Duration::from_millis(100)).await; | ||
|
||
let updates = [ | ||
subscription_server::BookChanged { | ||
id: "123".into(), | ||
book: None, | ||
}, | ||
subscription_server::BookChanged { | ||
id: "456".into(), | ||
book: None, | ||
}, | ||
subscription_server::BookChanged { | ||
id: "789".into(), | ||
book: None, | ||
}, | ||
]; | ||
|
||
futures::join!( | ||
async { | ||
for update in &updates { | ||
server.send(update.to_owned()).unwrap(); | ||
} | ||
}, | ||
async { | ||
let received_updates = stream.take(updates.len()).collect::<Vec<_>>().await; | ||
|
||
for (expected, update) in updates.iter().zip(received_updates) { | ||
let update = update.unwrap(); | ||
assert_matches!(update.errors, None); | ||
let data = update.data.unwrap(); | ||
assert_eq!(data.books.id, expected.id.0); | ||
} | ||
} | ||
); | ||
} | ||
|
||
#[tokio::test] | ||
async fn oneshot_operation_test() { | ||
use async_tungstenite::tungstenite::{client::IntoClientRequest, http::HeaderValue}; | ||
use futures::StreamExt; | ||
|
||
let server = SubscriptionServer::start().await; | ||
|
||
sleep(Duration::from_millis(20)).await; | ||
|
||
let mut request = server.websocket_url().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 stream = graphql_ws_client::Client::build(connection) | ||
.subscribe(build_query()) | ||
.await | ||
.unwrap(); | ||
|
||
let updates = [ | ||
subscription_server::BookChanged { | ||
id: "123".into(), | ||
book: None, | ||
}, | ||
subscription_server::BookChanged { | ||
id: "456".into(), | ||
book: None, | ||
}, | ||
subscription_server::BookChanged { | ||
id: "789".into(), | ||
book: None, | ||
}, | ||
]; | ||
|
||
futures::join!( | ||
async { | ||
sleep(Duration::from_millis(10)).await; | ||
for update in &updates { | ||
server.send(update.to_owned()).unwrap(); | ||
} | ||
}, | ||
async { | ||
let received_updates = stream.take(updates.len()).collect::<Vec<_>>().await; | ||
|
||
for (expected, update) in updates.iter().zip(received_updates) { | ||
let update = update.unwrap(); | ||
assert_matches!(update.errors, None); | ||
let data = update.data.unwrap(); | ||
assert_eq!(data.books.id, expected.id.0); | ||
} | ||
} | ||
); | ||
} | ||
|
||
fn build_query() -> graphql_ws_client::graphql::StreamingOperation<BooksChanged> { | ||
StreamingOperation::new(books_changed::Variables) | ||
} | ||
|
||
impl PartialEq<subscription_server::Book> for books_changed::BooksChangedBooksBook { | ||
fn eq(&self, other: &subscription_server::Book) -> bool { | ||
self.id == other.id.0 && self.name == other.name && self.author == other.author | ||
} | ||
} |