背景:
我无法sqlx
与juniper
订阅集成。
我Pin<Box<dyn Stream<Item = Result<User, sqlx::Error>> + 'e + Send>>
从sqlx::query::QueryAs::fetch()
.
juniper
需要订阅返回为Pin<Box<dyn Stream<Item = Result<User, juniper::FieldError>> + Send>>
.
注意从Result<User, sqlx::Error>
到的变化Result<User, juniper::FieldError>
。使用map_err()
fromfutures::TryStreamExt
,我创建了以下代码来执行查询并转换错误类型。
type UsersStream =
Pin<Box<dyn Stream<Item = Result<User, FieldError>> + Send>>;
#[juniper::graphql_subscription(Context = Context)]
impl SubscriptionRoot {
async fn users(context: &Context) -> UsersStream {
let sqlx::query_as!(User, "SELECT * FROM users")
.fetch(&context.pool)
.map_err(|e| {
FieldError::new(
"Database error",
graphql_value!(format!("{}", e)))
})
.boxed()
}
}
这在编译时失败并出现以下错误:
error[E0759]: `executor` has lifetime `'ref_e` but it needs to satisfy a `'static` lifetime requirement
--> server/src/graphql/subscription.rs:27:1
|
27 | #[juniper::graphql_subscription(Context = Context)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| this data with lifetime `'ref_e`...
| ...is captured here...
...
63 | / sqlx::query_as!(User, "SELECT * FROM users")
64 | | .fetch(&context.pool)
65 | | .map_err(|e| {
66 | | FieldError::new(
... |
69 | | })
70 | | .boxed()
| |____________________- ...and is required to live as long as `'static` here
|
= note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to previous error
我对Stream
s 或生命周期不够熟悉,无法理解此错误的含义。在进一步研究之后,这似乎ref_e
是订阅对juniper
's的引用的生命周期Executor
。
尝试:
- 提供生命周期,
juniper::Context
如graphql-rust/juniper#143中所述。 - 更高等级的特征界限
版本:
sqlx-0.4.1
juniper
cd66bdb
固定承诺master