From c2b09dbb88591d9695c1cd2227c5d559b4a8e775 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Tue, 20 Aug 2024 21:11:43 +0200 Subject: Add state for database --- src/graphql.rs | 47 +++++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 22 deletions(-) (limited to 'src/graphql.rs') diff --git a/src/graphql.rs b/src/graphql.rs index 12b6db3..2843156 100644 --- a/src/graphql.rs +++ b/src/graphql.rs @@ -1,14 +1,11 @@ -use async_graphql::{ - http::GraphiQLSource, Context, EmptyMutation, EmptySubscription, Object, Schema, -}; -use async_graphql_axum::GraphQL; -use axum::{ - response::{self, IntoResponse}, - routing::get, - Router, -}; +use std::sync::Arc; -struct Query; +use async_graphql::{Context, EmptyMutation, EmptySubscription, Object, Schema}; +use async_graphql_axum::{GraphQLRequest, GraphQLResponse}; + +// use crate::state::AppState; + +pub struct Query; #[Object] impl Query { @@ -23,6 +20,19 @@ impl Query { #[graphql(desc = "First value")] a: i32, #[graphql(desc = "Second value")] b: Option, ) -> i32 { + // let state = ctx.data::().unwrap(); + // let client = &*state.client; + // + // // Perform a database query + // let rows = client + // .query("SELECT owner FROM payment", &[]) + // .await + // .unwrap(); + // for row in rows { + // let owner: String = row.get(0); + // println!("{owner}"); + // } + match b { Some(x) => a + x, None => a, @@ -30,16 +40,9 @@ impl Query { } } -pub async fn graphiql() -> impl IntoResponse { - response::Html( - GraphiQLSource::build() - .endpoint("/") - .subscription_endpoint("/ws") - .finish(), - ) -} - -pub fn create_route() -> Router { - let schema = Schema::new(Query, EmptyMutation, EmptySubscription); - Router::new().route("/", get(graphiql).post_service(GraphQL::new(schema))) +pub async fn graphql_handler( + schema: Arc>, + req: GraphQLRequest, +) -> GraphQLResponse { + schema.execute(req.into_inner()).await.into() } -- cgit v1.2.3-18-g5258