use async_graphql::{EmptyMutation, EmptySubscription, Object, Schema};
use std::sync::Arc;
pub struct QueryRoot;
#[Object]
impl QueryRoot {
async fn ping(&self) -> &'static str {
"pong"
}
}
pub fn build_schema() -> Schema<QueryRoot, EmptyMutation, EmptySubscription> {
Schema::build(QueryRoot, EmptyMutation, EmptySubscription).finish()
}
pub fn build_schema_with_context<T: Send + Sync + 'static>(
repo: Arc<T>,
) -> Schema<QueryRoot, EmptyMutation, EmptySubscription> {
Schema::build(QueryRoot, EmptyMutation, EmptySubscription)
.data(repo)
.finish()
}
pub fn export_sdl<Q, M, S>(schema: &Schema<Q, M, S>, path: &str) -> Result<(), std::io::Error>
where
Q: async_graphql::ObjectType + 'static,
M: async_graphql::ObjectType + 'static,
S: async_graphql::SubscriptionType + 'static,
{
let sdl = schema.sdl();
std::fs::write(path, sdl)?;
println!("✅ GraphQL Schema successfully exported to {}", path);
Ok(())
}
pub fn setup() {
println!("ferrox-graphql initialized: Provides async-graphql Schema builders.");
}
#[cfg(test)]
mod tests {
use super::*;
use async_graphql::value;
use serde_json::json;
#[tokio::test]
async fn test_graphql_ping_query() {
let schema = build_schema();
let request = "{ ping }";
let response = schema.execute(request).await;
assert_eq!(
response.data.into_json().unwrap(),
json!({
"ping": "pong"
})
);
}
}