Request Context
The Request Context feature enables the creation of mutable state through the lifecycle of a request.
Usage
1. Define RequestContext struct
Define a struct that contains mutable information to be available for the lifetime of a request. The request context must implement Clone, Debug, Sync, Send, and Warpgrapher RequestContext.
request_id: String,
}
impl RequestContext for AppRequestContext {
type DBEndpointType = Neo4jEndpoint;
fn new() -> AppRequestContext {
// generate random request id
let request_id = "1234".to_string();
AppRequestContext { request_id }
}
}
fn resolve_echo_request(facade: ResolverFacade<AppRequestContext>) -> BoxFuture<ExecutionResult> {
Box::pin(async move {
2. Create Engine with RequestContext type parameter
The RequestContext is specified in the second type paramter of Engine.
.with_resolvers(resolvers)
.build()
.expect("Failed to build engine");
3. Access Context inside resolver
Box::pin(async move {
let request_context = facade.request_context().unwrap();
let request_id = request_context.request_id.clone();
facade.resolve_scalar(format!("echo! (request_id: {})", request_id))
})
}
Full Example
View on Github.