Skip to content
Clone
149bec6
68 lines 2.8 KB Rust
use std::sync::Arc;
use async_graphql::{EmptyMutation, EmptySubscription, Schema};
use crate::service::GitService;
pub mod error;
pub mod query;
pub mod scalars;
pub mod types;
pub use error::to_gql;
pub use query::{GitSchema, Query, Repo};
pub use scalars::{Base64Scalar, DateTimeScalar, ObjectIdScalar};
/// Build a read-only graphql schema. The mutation and subscription
/// types are intentionally empty — gitdata exposes only query.
pub fn build_schema(state: export::AppState) -> GitSchema {
let introspection_enabled = state.config.git.graphql_introspection;
let mut builder = Schema::build(Query, EmptyMutation, EmptySubscription)
.data(Arc::new(GitService::new(state.clone())))
.limit_complexity(state.config.git.graphql_complexity_limit.max(1) as usize)
.limit_depth(15);
if !introspection_enabled {
builder = builder.disable_introspection();
}
builder.finish()
}
#[cfg(test)]
mod tests {
use super::*;
fn schema_with_default_state() -> GitSchema {
// The schema's SDL shape is independent of the embedded
// `GitService` — data is only consulted at request time. Build a
// schema without data; no requests are executed in these tests.
Schema::build(Query, EmptyMutation, EmptySubscription).finish()
}
#[test]
fn schema_contains_root_and_key_types() {
let sdl = schema_with_default_state().sdl();
// Print SDL for diagnostic if any assertion fails.
eprintln!("--- SDL ---\n{sdl}\n---");
assert!(sdl.contains("type Query"), "Query root missing");
assert!(sdl.contains("repo("), "repo entry-point missing");
assert!(sdl.contains("type Commit"), "Commit object missing");
assert!(sdl.contains("type Blob"), "Blob object missing");
assert!(sdl.contains("type Tree"), "Tree object missing");
assert!(sdl.contains("type Tag"), "Tag object missing");
assert!(sdl.contains("type Ref"), "Ref object missing");
assert!(sdl.contains("type Blame"), "Blame object missing");
assert!(sdl.contains("type Submodule"), "Submodule object missing");
assert!(sdl.contains("type LanguageStat"), "LanguageStats missing");
assert!(sdl.contains("type CommitStatus"), "CommitStatus missing");
assert!(sdl.contains("scalar ObjectId"), "ObjectId scalar missing");
assert!(sdl.contains("scalar DateTime"), "DateTime scalar missing");
assert!(sdl.contains("union GitObject"), "GitObject union missing");
assert!(sdl.contains("input PageInput"), "PageInput missing");
assert!(
sdl.contains("input LogOptionsInput"),
"LogOptionsInput missing"
);
// read-only: schema should not declare user-facing Mutation fields
assert!(!sdl.contains("type Mutation"), "Mutation should be empty");
}
}