Skip to content
Clone
8f74012
73 lines 2.1 KB Rust
use async_graphql::{Context, EmptyMutation, EmptySubscription, ErrorExtensions, Object, Schema};
use std::sync::Arc;
use uuid::Uuid;
use crate::graphql::types::LicenseObj;
use crate::service::GitService;
use crate::share::access::authorize_repo_access;
use crate::share::errors::GitError;
use super::context::{RepoCtx, map_git};
use super::repo::Repo;
pub type GitSchema = Schema<Query, EmptyMutation, EmptySubscription>;
pub struct Query;
#[Object]
impl Query {
async fn api_version(&self) -> &'static str {
"1.0.0"
}
async fn license(
&self,
ctx: &Context<'_>,
name: String,
) -> Result<LicenseObj, async_graphql::Error> {
let svc = ctx.data_unchecked::<Arc<GitService>>().clone();
let dto = svc.get_license(&name).await.map_err(map_git)?;
Ok(LicenseObj::from(models::repo::repo_license::Model {
name: dto.name,
display_name: dto.display_name,
description: dto.description,
body: dto.body,
url: dto.url,
created_at: dto.created_at,
updated_at: dto.updated_at,
}))
}
async fn repo(
&self,
ctx: &Context<'_>,
space_name: String,
repo_name: String,
) -> Result<Repo, async_graphql::Error> {
let svc = ctx.data_unchecked::<Arc<GitService>>().clone();
let ctx_data = svc
.context(space_name.clone(), repo_name.clone())
.await
.map_err(map_git)?;
if ctx_data.model.is_private {
let uid = ctx
.data::<Option<Uuid>>()
.ok()
.and_then(|o| *o)
.ok_or_else(|| GitError::Unauthorized.extend())?;
authorize_repo_access(&svc, uid, &ctx_data, false, false)
.await
.map_err(map_git)?;
}
let baby = svc.baby(&ctx_data);
Ok(Repo {
inner: RepoCtx {
svc,
ctx: ctx_data,
space_name,
repo_name,
baby,
},
})
}
}