llm_registry_api/graphql/
mod.rs

1//! GraphQL API implementation
2//!
3//! This module provides a complete GraphQL API for the LLM Registry using async-graphql.
4//! It supports queries, mutations, authentication, and includes a GraphQL Playground.
5
6pub mod mutation;
7pub mod query;
8pub mod types;
9
10use async_graphql::{http::GraphiQLSource, EmptySubscription, Schema};
11use async_graphql_axum::{GraphQLRequest, GraphQLResponse};
12use axum::{extract::State, response::{Html, IntoResponse}, Extension};
13use llm_registry_service::ServiceRegistry;
14use std::sync::Arc;
15
16use crate::auth::AuthUser;
17
18pub use mutation::Mutation;
19pub use query::Query;
20
21/// GraphQL schema type
22pub type AppSchema = Schema<Query, Mutation, EmptySubscription>;
23
24/// Build the GraphQL schema
25pub fn build_schema(services: Arc<ServiceRegistry>) -> AppSchema {
26    Schema::build(Query, Mutation, EmptySubscription)
27        .data(services)
28        .finish()
29}
30
31/// GraphQL handler with optional authentication
32pub async fn graphql_handler(
33    State(schema): State<AppSchema>,
34    auth_user: Option<Extension<AuthUser>>,
35    req: GraphQLRequest,
36) -> GraphQLResponse {
37    let mut request = req.into_inner();
38
39    // Add authenticated user to context if present
40    if let Some(Extension(user)) = auth_user {
41        request = request.data(user);
42    }
43
44    schema.execute(request).await.into()
45}
46
47/// GraphQL Playground handler
48pub async fn graphql_playground() -> impl IntoResponse {
49    Html(
50        GraphiQLSource::build()
51            .endpoint("/graphql")
52            .title("LLM Registry GraphQL Playground")
53            .finish(),
54    )
55}
56
57// TODO: Fix unit tests
58#[cfg(all(test, feature = "incomplete_tests"))]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn test_schema_creation() {
64        use llm_registry_db::DatabaseConfig;
65        use llm_registry_service::ServiceRegistry;
66
67        // This is a smoke test to ensure the schema can be created
68        let db_config = DatabaseConfig::default();
69        let services = ServiceRegistry::new(db_config);
70        let schema = build_schema(Arc::new(services));
71
72        // Schema should have the Query and Mutation types
73        assert!(schema.sdl().contains("type Query"));
74        assert!(schema.sdl().contains("type Mutation"));
75    }
76}