From 3bce0eeee09e1eb457ec633110dc873eb7a3e994 Mon Sep 17 00:00:00 2001 From: Zeph Levy <171337931+ZephLevy@users.noreply.github.com> Date: Tue, 30 Sep 2025 22:07:02 +0200 Subject: [PATCH] Organize the project into modules for better extendability --- src/main.rs | 32 ++++++-------------------------- src/models.rs | 7 +++++++ src/resolvers.rs | 22 ++++++++++++++++++++++ src/schema.rs | 8 ++++++++ 4 files changed, 43 insertions(+), 26 deletions(-) create mode 100644 src/models.rs create mode 100644 src/resolvers.rs create mode 100644 src/schema.rs diff --git a/src/main.rs b/src/main.rs index 718b52e..a54ce89 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,38 +1,18 @@ -use async_graphql::{EmptyMutation, EmptySubscription, Object, Schema, SimpleObject}; +mod models; +mod resolvers; +mod schema; + use async_graphql_axum::{GraphQLRequest, GraphQLResponse}; use axum::{ Router, routing::{get, post}, }; +use schema::build_schema; use std::net::SocketAddr; -struct QueryRoot; - -#[Object] -impl QueryRoot { - async fn hello(&self) -> &str { - "Hello, world!" - } - async fn testing(&self) -> &str { - "Testing" - } - async fn user(&self, id: i32) -> User { - User { - id, - name: String::from("Zeph"), - } - } -} - -#[derive(SimpleObject)] -struct User { - id: i32, - name: String, -} - #[tokio::main] async fn main() { - let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription).finish(); + let schema = build_schema(); let app = Router::new() .route( diff --git a/src/models.rs b/src/models.rs new file mode 100644 index 0000000..918bcb7 --- /dev/null +++ b/src/models.rs @@ -0,0 +1,7 @@ +use async_graphql::SimpleObject; + +#[derive(SimpleObject)] +pub struct User { + pub id: i32, + pub name: String, +} diff --git a/src/resolvers.rs b/src/resolvers.rs new file mode 100644 index 0000000..74aece1 --- /dev/null +++ b/src/resolvers.rs @@ -0,0 +1,22 @@ +use crate::models::User; +use async_graphql::Object; + +pub struct QueryRoot; + +#[Object] +impl QueryRoot { + async fn hello(&self) -> &str { + "Hello, world!" + } + + async fn testing(&self) -> &str { + "Testing" + } + + async fn user(&self, id: i32) -> User { + User { + id, + name: String::from("Zeph"), + } + } +} diff --git a/src/schema.rs b/src/schema.rs new file mode 100644 index 0000000..2fa93c1 --- /dev/null +++ b/src/schema.rs @@ -0,0 +1,8 @@ +use crate::resolvers::QueryRoot; +use async_graphql::{EmptyMutation, EmptySubscription, Schema}; + +pub type AppSchema = Schema; + +pub fn build_schema() -> AppSchema { + Schema::build(QueryRoot, EmptyMutation, EmptySubscription).finish() +}