pub struct GraphQLContext { /* private fields */ }Expand description
GraphQL context for managing request-scoped data
Provides access to request information, user authentication state, data loaders for efficient batch loading, and custom data storage.
§Examples
use reinhardt_graphql::context::GraphQLContext;
use serde_json::json;
let context = GraphQLContext::new();
// Set custom data
context.set_data("api_version".to_string(), json!("v1"));
// Get custom data
let version = context.get_data("api_version");
assert_eq!(version, Some(json!("v1")));Implementations§
Source§impl GraphQLContext
impl GraphQLContext
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new GraphQL context
§Examples
use reinhardt_graphql::context::GraphQLContext;
let context = GraphQLContext::new();
assert!(context.get_data("nonexistent").is_none());Sourcepub fn set_data(&self, key: String, value: Value)
pub fn set_data(&self, key: String, value: Value)
Set custom data in the context
§Examples
use reinhardt_graphql::context::GraphQLContext;
use serde_json::json;
let context = GraphQLContext::new();
context.set_data("user_id".to_string(), json!("123"));
assert_eq!(context.get_data("user_id"), Some(json!("123")));Sourcepub fn get_data(&self, key: &str) -> Option<Value>
pub fn get_data(&self, key: &str) -> Option<Value>
Get custom data from the context
Returns None if the key does not exist. For GraphQL resolvers that
require the data to be present, use require_data
instead to get a proper GraphQL error.
§Examples
use reinhardt_graphql::context::GraphQLContext;
use serde_json::json;
let context = GraphQLContext::new();
context.set_data("count".to_string(), json!(42));
assert_eq!(context.get_data("count"), Some(json!(42)));
assert_eq!(context.get_data("nonexistent"), None);Sourcepub fn require_data(&self, key: &str) -> Result<Value>
pub fn require_data(&self, key: &str) -> Result<Value>
Get required custom data from the context, returning a GraphQL error if missing
This method should be used in GraphQL resolvers where the data is expected
to be present. Instead of panicking on a missing key, it returns a
descriptive async_graphql::Error that will be surfaced as a GraphQL error
in the response.
§Examples
use reinhardt_graphql::context::GraphQLContext;
use serde_json::json;
let context = GraphQLContext::new();
context.set_data("api_version".to_string(), json!("v1"));
// Successful lookup
let version = context.require_data("api_version");
assert!(version.is_ok());
assert_eq!(version.unwrap(), json!("v1"));
// Missing key returns an error
let missing = context.require_data("nonexistent");
assert!(missing.is_err());Sourcepub fn remove_data(&self, key: &str) -> Option<Value>
pub fn remove_data(&self, key: &str) -> Option<Value>
Remove custom data from the context
§Examples
use reinhardt_graphql::context::GraphQLContext;
use serde_json::json;
let context = GraphQLContext::new();
context.set_data("temp".to_string(), json!("value"));
let removed = context.remove_data("temp");
assert_eq!(removed, Some(json!("value")));
assert_eq!(context.get_data("temp"), None);Sourcepub fn clear_data(&self)
pub fn clear_data(&self)
Clear all custom data
§Examples
use reinhardt_graphql::context::GraphQLContext;
use serde_json::json;
let context = GraphQLContext::new();
context.set_data("key1".to_string(), json!("value1"));
context.set_data("key2".to_string(), json!("value2"));
context.clear_data();
assert_eq!(context.get_data("key1"), None);
assert_eq!(context.get_data("key2"), None);Sourcepub fn add_data_loader<T: DataLoader>(&self, loader: Arc<T>)
pub fn add_data_loader<T: DataLoader>(&self, loader: Arc<T>)
Add a data loader to the context
§Examples
use reinhardt_graphql::context::{GraphQLContext, DataLoader, LoaderError};
use async_trait::async_trait;
use std::sync::Arc;
struct SimpleLoader;
#[async_trait]
impl DataLoader for SimpleLoader {
type Key = i32;
type Value = String;
async fn load(&self, key: Self::Key) -> Result<Self::Value, LoaderError> {
Ok(format!("Value {}", key))
}
async fn load_many(&self, keys: Vec<Self::Key>) -> Result<Vec<Self::Value>, LoaderError> {
Ok(keys.iter().map(|k| format!("Value {}", k)).collect())
}
}
let context = GraphQLContext::new();
let loader = Arc::new(SimpleLoader);
context.add_data_loader(loader.clone());
let retrieved = context.get_data_loader::<SimpleLoader>();
assert!(retrieved.is_some());Sourcepub fn get_data_loader<T: DataLoader>(&self) -> Option<Arc<T>>
pub fn get_data_loader<T: DataLoader>(&self) -> Option<Arc<T>>
Get a data loader from the context
Returns None if the loader has not been registered. For GraphQL
resolvers that require the loader, use
require_data_loader instead to get a
proper GraphQL error.
§Examples
use reinhardt_graphql::context::{GraphQLContext, DataLoader, LoaderError};
use async_trait::async_trait;
use std::sync::Arc;
struct TestLoader;
#[async_trait]
impl DataLoader for TestLoader {
type Key = String;
type Value = i32;
async fn load(&self, _key: Self::Key) -> Result<Self::Value, LoaderError> {
Ok(42)
}
async fn load_many(&self, keys: Vec<Self::Key>) -> Result<Vec<Self::Value>, LoaderError> {
Ok(vec![42; keys.len()])
}
}
let context = GraphQLContext::new();
let loader = Arc::new(TestLoader);
context.add_data_loader(loader);
let retrieved = context.get_data_loader::<TestLoader>();
assert!(retrieved.is_some());Sourcepub fn require_data_loader<T: DataLoader>(&self) -> Result<Arc<T>>
pub fn require_data_loader<T: DataLoader>(&self) -> Result<Arc<T>>
Get a required data loader from the context, returning a GraphQL error if missing
This method should be used in GraphQL resolvers where the data loader is
expected to be registered. Instead of panicking on a missing loader, it
returns a descriptive async_graphql::Error that will be surfaced as a
GraphQL error in the response.
§Examples
use reinhardt_graphql::context::{GraphQLContext, DataLoader, LoaderError};
use async_trait::async_trait;
use std::sync::Arc;
struct MyLoader;
#[async_trait]
impl DataLoader for MyLoader {
type Key = String;
type Value = i32;
async fn load(&self, _key: Self::Key) -> Result<Self::Value, LoaderError> {
Ok(42)
}
async fn load_many(&self, keys: Vec<Self::Key>) -> Result<Vec<Self::Value>, LoaderError> {
Ok(vec![42; keys.len()])
}
}
let context = GraphQLContext::new();
// Missing loader returns an error
let result = context.require_data_loader::<MyLoader>();
assert!(result.is_err());
// After adding the loader, it succeeds
context.add_data_loader(Arc::new(MyLoader));
let result = context.require_data_loader::<MyLoader>();
assert!(result.is_ok());Sourcepub fn remove_data_loader<T: DataLoader>(&self)
pub fn remove_data_loader<T: DataLoader>(&self)
Remove a data loader from the context
§Examples
use reinhardt_graphql::context::{GraphQLContext, DataLoader, LoaderError};
use async_trait::async_trait;
use std::sync::Arc;
struct RemovableLoader;
#[async_trait]
impl DataLoader for RemovableLoader {
type Key = u64;
type Value = String;
async fn load(&self, key: Self::Key) -> Result<Self::Value, LoaderError> {
Ok(key.to_string())
}
async fn load_many(&self, keys: Vec<Self::Key>) -> Result<Vec<Self::Value>, LoaderError> {
Ok(keys.iter().map(|k| k.to_string()).collect())
}
}
let context = GraphQLContext::new();
let loader = Arc::new(RemovableLoader);
context.add_data_loader(loader);
context.remove_data_loader::<RemovableLoader>();
assert!(context.get_data_loader::<RemovableLoader>().is_none());Sourcepub fn clear_loaders(&self)
pub fn clear_loaders(&self)
Clear all data loaders
§Examples
use reinhardt_graphql::context::{GraphQLContext, DataLoader, LoaderError};
use async_trait::async_trait;
use std::sync::Arc;
struct Loader1;
struct Loader2;
#[async_trait]
impl DataLoader for Loader1 {
type Key = i32;
type Value = String;
async fn load(&self, key: Self::Key) -> Result<Self::Value, LoaderError> {
Ok(key.to_string())
}
async fn load_many(&self, keys: Vec<Self::Key>) -> Result<Vec<Self::Value>, LoaderError> {
Ok(keys.iter().map(|k| k.to_string()).collect())
}
}
#[async_trait]
impl DataLoader for Loader2 {
type Key = String;
type Value = i32;
async fn load(&self, _key: Self::Key) -> Result<Self::Value, LoaderError> {
Ok(0)
}
async fn load_many(&self, keys: Vec<Self::Key>) -> Result<Vec<Self::Value>, LoaderError> {
Ok(vec![0; keys.len()])
}
}
let context = GraphQLContext::new();
context.add_data_loader(Arc::new(Loader1));
context.add_data_loader(Arc::new(Loader2));
context.clear_loaders();
assert!(context.get_data_loader::<Loader1>().is_none());
assert!(context.get_data_loader::<Loader2>().is_none());Trait Implementations§
Auto Trait Implementations§
impl Freeze for GraphQLContext
impl RefUnwindSafe for GraphQLContext
impl Send for GraphQLContext
impl Sync for GraphQLContext
impl Unpin for GraphQLContext
impl UnsafeUnpin for GraphQLContext
impl UnwindSafe for GraphQLContext
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().