async-graphql/docs/en/src/context.md

22 lines
535 B
Markdown
Raw Normal View History

2020-04-15 03:15:30 +00:00
# Context
2020-09-01 05:47:22 +00:00
The main goal of `Context` is to acquire global data attached to Schema. **Note that if the return value of resolver function is borrowed from `Context`, you will need to explicitly state the lifetime of the argument.**
The following example shows how to borrow data in `Context`.
```rust
use async_graphql::*;
struct Query;
2020-09-13 04:12:32 +00:00
#[GQLObject]
impl Query {
async fn borrow_from_context_data<'ctx>(
&self,
ctx: &'ctx Context<'_>
) -> FieldResult<&'ctx String> {
ctx.data::<String>()
}
}
2020-06-01 11:01:04 +00:00
```