-
I'm trying to employ Juniper with some real-data subscriptions. I'm getting a change feed from my database, and mapping that stream into a User object that Juniper can resolve. It's working great! I now need to know when the subscription has ended so I can close the stream from my database. Is there any parameter that my Subscription function can intake to know when the client has ended the subscription? I need some sort of mechanism to know when the subscription has ended. For reference, my Subscription struct: pub struct Subscription;
#[graphql_subscription(context = Context)]
impl Subscription {
#[graphql()]
async fn watch_user(
#[graphql(context)] ctx: &Context,
#[graphql(description = "The ID of the user to watch")] id: String,
) -> GraphStream<User> {
log::debug!("Starting stream watching user ID \"{}\"", id);
//TODO: Rust might not appreciate the "service" paradigm, wonder if something else fits better...
let inner_stream = ctx.user_service.watch_user(id.clone());
let out_stream = inner_stream
.map(|u| {
u.map_err(|e| {
FieldError::new(e.to_string(), Value::null() as Value<DefaultScalarValue>)
})
})
.boxed();
out_stream
}
} I need to supply some sort of logic (maybe a oneshot Edit: |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Possible answer for nowI've implemented a quick use futures::{
channel::oneshot::{Receiver, Sender},
Stream,
};
use std::pin::Pin;
/// Stream wrapper that sends a message to a oneshot reciever upon being dropped.
#[derive(Debug)]
pub struct DropStream<T>
where
T: Stream + ?Sized,
{
pub stream: Pin<Box<T>>,
pub tx: Option<Sender<()>>,
}
impl<T> DropStream<T>
where
T: Stream + ?Sized,
{
pub fn new(stream: Pin<Box<T>>) -> (DropStream<T>, Receiver<()>) {
let (tx, rx) = futures::channel::oneshot::channel();
let myself = Self {
stream,
tx: Some(tx),
};
return (myself, rx);
}
///Given an existing `Stream` and `Sender`, create a `DropStream` wrapper.
pub fn from_existing_tx(stream: Pin<Box<T>>, tx: Sender<()>) -> DropStream<T> {
let myself = Self {
stream,
tx: Some(tx),
};
return myself;
}
}
impl<T> Drop for DropStream<T>
where
T: Stream + ?Sized,
{
fn drop(&mut self) {
if let Some(tx) = self.tx.take() {
let _ = tx.send(());
};
}
}
impl<T> Stream for DropStream<T>
where
T: Stream + ?Sized,
{
type Item = T::Item;
fn poll_next(
mut self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Option<Self::Item>> {
self.stream.as_mut().poll_next(cx)
}
} Implementation detailsIn my implementation, I create a Subscription Object: #[graphql_subscription(context = Context)]
impl Subscription {
#[graphql()]
async fn watch_user(
#[graphql(context)] ctx: &Context,
#[graphql(description = "The ID of the user to watch")] id: String,
) -> GraphStream<User> {
log::debug!("Starting stream watching user ID \"{}\"", id);
let (tx, rx) = futures::channel::oneshot::channel();
//TODO: Rust might not appreciate the "service" paradigm, wonder if something else fits better...
let inner_stream = ctx.user_service.watch_user(id.clone(), rx);
let mapped_stream = inner_stream.map(|u| {
u.map_err(|e| {
FieldError::new(e.to_string(), Value::null() as Value<DefaultScalarValue>)
})
});
let wired_stream = DropStream::from_existing_tx(mapped_stream.boxed(), tx);
let wired_stream = wired_stream.boxed();
wired_stream
}
} Database connection logic fn watch_user(
&self,
id: String,
close_oneshot: Receiver<()>,
) -> crate::models::user::UserStream {
let conn_result = self.connection();
let mut conn = conn_result.unwrap();
log::debug!("Watching user: {}", id);
let error_title = "Error in watch_user:";
let err_fn = move |e| match e {
reql::Error::Driver(e) => {
let newmsg = format!("Driver error: {:?}", e);
log::error!("{}{}", error_title, newmsg);
newmsg
}
reql::Error::Runtime(e) => {
let newmsg = format!("Runtime error: {}", e.to_string());
log::error!("{}{}", error_title, newmsg);
newmsg
}
any_e => {
let newmsg = format!("Unknown error: {}", any_e.to_string());
log::error!("{}{}", error_title, newmsg);
newmsg
}
};
let change_conn = conn.clone();
let stream = r
.db("main")
.table("users")
.get(id)
.changes(())
.run::<Connection, reql::types::Change<User, User>>(change_conn)
.map(move |change_result| {
log::debug!("Change result {:?}", change_result);
match change_result {
Ok(change) => {
if change.new_val.is_none() {
Err("User not found".to_string())
} else {
Ok(change.new_val.unwrap())
}
}
Err(e) => Err(err_fn(e)),
}
})
.map_err(|e| e.to_string());
log::debug!("Created changes stream...");
actix_web::rt::spawn(async move {
let _msg = close_oneshot.await;
log::debug!("Closing stream...");
let _close_success = conn.close(()).await;
});
return stream.boxed();
} |
Beta Was this translation helpful? Give feedback.
-
We should document something like this in the book! |
Beta Was this translation helpful? Give feedback.
Possible answer for now
I've implemented a quick
Stream
wrapper calledDropStream<T>
.