-
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.
An easier one shot subscribe API (#65)
`graphql-ws-client` was designed to allow multiple subscriptions to be run on a single connection. This is still something I want to support, but it's also common for users to only want to run a single subscription per connection. Right now the API for doing that is a bit too verbose. This PR will introduce a `streaming_operation` function on the `ClientBuilder` to handle this use case, combining the connect, subscribe and actor spawning into a single function call. Todo: - [x] Examples - [x] Tests - [x] Changelog
- Loading branch information
Showing
12 changed files
with
447 additions
and
63 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
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
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,85 @@ | ||
//! An example of running a multiple subscriptions on a single connection | ||
//! using `graphql-ws-client` and `async-tungstenite` | ||
//! | ||
//! Talks to the the tide subscription example in `async-graphql` | ||
|
||
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, | ||
} | ||
|
||
#[async_std::main] | ||
async fn main() { | ||
use async_tungstenite::tungstenite::{client::IntoClientRequest, http::HeaderValue}; | ||
use futures::StreamExt; | ||
use graphql_ws_client::CynicClientBuilder; | ||
|
||
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 (sink, stream) = connection.split(); | ||
|
||
let mut client = CynicClientBuilder::new() | ||
.build(stream, sink, async_executors::AsyncStd) | ||
.await | ||
.unwrap(); | ||
|
||
// In reality you'd probably want to different subscriptions, but for the sake of this example | ||
// these are the same subscriptions | ||
let mut first_subscription = client.streaming_operation(build_query()).await.unwrap(); | ||
let mut second_subscription = client.streaming_operation(build_query()).await.unwrap(); | ||
|
||
futures::join!( | ||
async move { | ||
while let Some(item) = first_subscription.next().await { | ||
println!("{:?}", item); | ||
} | ||
}, | ||
async move { | ||
while let Some(item) = second_subscription.next().await { | ||
println!("{:?}", item); | ||
} | ||
} | ||
); | ||
} | ||
|
||
fn build_query() -> cynic::StreamingOperation<BooksChangedSubscription> { | ||
use cynic::SubscriptionBuilder; | ||
|
||
BooksChangedSubscription::build(()) | ||
} |
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,27 @@ | ||
use crate::{next::Message, Error}; | ||
|
||
pub struct Conn; | ||
|
||
#[async_trait::async_trait] | ||
impl crate::next::Connection for Conn { | ||
async fn receive(&mut self) -> Option<Message> { | ||
unimplemented!() | ||
} | ||
|
||
async fn send(&mut self, _: Message) -> Result<(), Error> { | ||
unimplemented!() | ||
} | ||
} | ||
|
||
#[derive(serde::Serialize)] | ||
pub struct Subscription; | ||
|
||
impl crate::graphql::GraphqlOperation for Subscription { | ||
type Response = (); | ||
|
||
type Error = crate::Error; | ||
|
||
fn decode(&self, _data: serde_json::Value) -> Result<Self::Response, Self::Error> { | ||
unimplemented!() | ||
} | ||
} |
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
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
Oops, something went wrong.