use axum::{
Json,
extract::State,
http::StatusCode,
response::{IntoResponse, Response},
};
use fraiseql_core::db::traits::DatabaseAdapter;
use serde::Serialize;
use crate::routes::{
api::types::{ApiError, ApiResponse},
graphql::AppState,
};
#[derive(Debug, Serialize)]
pub struct GraphQLSchemaResponse {
pub schema: String,
}
#[derive(Debug, Serialize)]
pub struct JsonSchemaResponse {
pub schema: serde_json::Value,
}
pub async fn export_sdl_handler<A: DatabaseAdapter>(
State(state): State<AppState<A>>,
) -> Result<Response, ApiError> {
let schema_sdl = state.executor().schema().raw_schema();
Ok((StatusCode::OK, schema_sdl).into_response())
}
pub async fn export_json_handler<A: DatabaseAdapter>(
State(state): State<AppState<A>>,
) -> Result<Json<ApiResponse<JsonSchemaResponse>>, ApiError> {
let schema_json = serde_json::to_value(state.executor().schema())
.map_err(|e| ApiError::internal_error(format!("Failed to serialize schema: {e}")))?;
let response = JsonSchemaResponse {
schema: schema_json,
};
Ok(Json(ApiResponse {
status: "success".to_string(),
data: response,
}))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_graphql_response_creation() {
let response = GraphQLSchemaResponse {
schema: "type Query { hello: String }".to_string(),
};
assert_eq!(response.schema, "type Query { hello: String }");
}
#[test]
fn test_json_response_creation() {
let response = JsonSchemaResponse {
schema: serde_json::json!({"types": []}),
};
assert!(response.schema.is_object());
}
}