2020-04-15 03:15:30 +00:00
|
|
|
|
# 查询上下文(Context)
|
2020-04-16 03:06:09 +00:00
|
|
|
|
|
|
|
|
|
查询上下文(Context)的主要作用是获取附加到Schema的全局数据,**需要注意的是,如果你的Resolve函数返回的数据借用了Context内保存的数据,需要明确指定生命周期参数**。
|
|
|
|
|
|
|
|
|
|
下面是一个返回值借用Context内数据的例子:
|
|
|
|
|
|
|
|
|
|
```rust
|
2020-04-16 07:09:09 +00:00
|
|
|
|
use async_graphql::*;
|
|
|
|
|
|
2020-04-16 03:06:09 +00:00
|
|
|
|
struct Query;
|
|
|
|
|
|
|
|
|
|
#[Object]
|
|
|
|
|
impl Query {
|
|
|
|
|
async fn borrow_from_context_data<'ctx'>(
|
|
|
|
|
&self,
|
|
|
|
|
ctx: &'ctx Context<'_>
|
|
|
|
|
) -> &'ctx String {
|
|
|
|
|
ctx.data::<String>
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|