tinycongress_api/
graphql.rs1use crate::build_info::{BuildInfo as BuildInfoObject, BuildInfoProvider};
2use async_graphql::http::{playground_source, GraphQLPlaygroundConfig};
3use async_graphql::{Context, EmptySubscription, Object, Result, Schema};
4use async_graphql_axum::{GraphQLRequest, GraphQLResponse};
5use axum::extract::Extension;
6use axum::response::{Html, IntoResponse};
7
8pub type ApiSchema = Schema<QueryRoot, MutationRoot, EmptySubscription>;
10
11pub struct QueryRoot;
13
14#[Object]
15impl QueryRoot {
16 #[allow(clippy::unused_async)]
18 async fn build_info(&self, ctx: &Context<'_>) -> Result<BuildInfoObject> {
19 let provider = ctx.data::<BuildInfoProvider>()?;
20 Ok(provider.build_info())
21 }
22}
23
24pub struct MutationRoot;
26
27#[Object]
28impl MutationRoot {
29 #[allow(clippy::unused_async)]
34 async fn echo(&self, _ctx: &Context<'_>, message: String) -> String {
35 message
36 }
37}
38
39#[allow(clippy::unused_async)]
41pub async fn graphql_playground() -> impl IntoResponse {
42 Html(playground_source(GraphQLPlaygroundConfig::new("/graphql")))
43}
44
45pub async fn graphql_handler(schema: Extension<ApiSchema>, req: GraphQLRequest) -> GraphQLResponse {
47 schema.execute(req.into_inner()).await.into()
48}