async-graphql/docs/zh-CN/src/context.md
2020-04-17 19:33:28 +08:00

535 B
Raw Blame History

查询上下文(Context)

查询上下文(Context)的主要作用是获取附加到Schema的全局数据需要注意的是如果你的Resolve函数返回的数据借用了Context内保存的数据需要明确指定生命周期参数

下面是一个返回值借用Context内数据的例子

use async_graphql::*;

struct Query;

#[Object]
impl Query {
    async fn borrow_from_context_data<'ctx'>(
        &self,
        ctx: &'ctx Context<'_>
    ) -> &'ctx String {
        ctx.data::<String>
    }
}