58 lines
1.4 KiB
Rust
58 lines
1.4 KiB
Rust
use dataloader::non_cached::Loader;
|
|
use juniper::FieldResult;
|
|
use juniper::IntoFieldError;
|
|
use uuid_simd::UuidExt;
|
|
use uuidv7::Uuid;
|
|
|
|
use crate::{
|
|
api::{loaders, Error},
|
|
db, worker,
|
|
};
|
|
|
|
#[derive(Clone)]
|
|
pub struct Loaders {
|
|
pub user: loaders::user::UserLoader,
|
|
pub repository: loaders::repository::RepositoryLoader,
|
|
}
|
|
|
|
impl Default for Loaders {
|
|
fn default() -> Self {
|
|
Self {
|
|
user: Loader::new(loaders::user::UserBatcher)
|
|
.with_yield_count(loaders::user::YIELD_COUNT),
|
|
repository: Loader::new(loaders::repository::RepositoryBatcher)
|
|
.with_yield_count(loaders::repository::YIELD_COUNT),
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct Context {
|
|
pub db_pool: db::Pool,
|
|
pub worker_pool: worker::Pool,
|
|
pub loaders: Loaders,
|
|
pub logged_in: bool,
|
|
}
|
|
|
|
impl Context {
|
|
pub fn get_db_conn(&self) -> FieldResult<db::PoolConnection> {
|
|
self.db_pool
|
|
.get()
|
|
.map_or(Err(Error::Internal.into_field_error()), Ok)
|
|
}
|
|
|
|
pub async fn get_worker_conn(
|
|
&self,
|
|
) -> FieldResult<bb8::PooledConnection<worker::ConnectionManager>> {
|
|
self.worker_pool
|
|
.get()
|
|
.await
|
|
.map_or(Err(Error::Internal.into_field_error()), Ok)
|
|
}
|
|
|
|
pub fn parse_uuid(id: &[u8]) -> FieldResult<Uuid> {
|
|
Uuid::parse(id).map_err(|_| Error::InvalidUuid.into_field_error())
|
|
}
|
|
}
|
|
|
|
impl juniper::Context for Context {}
|