-
Notifications
You must be signed in to change notification settings - Fork 440
Description
Hi,
I'm having trouble creating a subscription resolver that pulls data from the database. As an example let's pretend I want to get the time from the database every second. Here is what I tried:
I've got a context containing a sqlx pool:
pub struct Context {
pub db: sqlx::PgPool,
}
impl juniper::Context for Context {
}
and I'm trying to create the resolver like this:
pub struct Subscription;
type StringStream = std::pin::Pin<Box<dyn futures::Stream<Item = Result<String, juniper::FieldError>> + Send>>;
#[juniper::graphql_subscription(context = Context)]
impl Subscription {
async fn time<'a>(context: &Context) -> StringStream {
let mut interval = tokio::time::interval(std::time::Duration::from_secs(1));
let stream = async_stream::stream! {
loop {
interval.tick().await;
yield sqlx::query_scalar("select 1")
.fetch_one(&context.db).await
.map_err(|err: sqlx::Error| {
dbg!(&err);
tracing::error!("{}", &err);
err.into()
})
.and_then(|r: i32| {
tracing::info!("ok");
executor.resolve_with_ctx(info, &r);
Ok( chrono::Utc::now().to_rfc3339())
})
}
};
Box::pin(stream)
}
}
But this wont compile:
error[E0759]:
executor
has lifetime'ref_e
but it needs to satisfy a'static
lifetime requirement
--> src/server/schema.rs:120:1
|
120 | #[juniper::graphql_subscription(context = Context)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| this data with lifetime'ref_e
...
| ...is captured here, requiring it to live as long as'static
|
= note: this error originates in the attribute macrojuniper::graphql_subscription
(in Nightly builds, run with -Z macro-backtrace for more info)
I have no problem making database queries in query and mutation endpoints but here I can't. Can someone tell me what I'm doing wrong ?
thank you