Organize the project into modules for better extendability

This commit is contained in:
Zeph 2025-09-30 22:07:02 +02:00
parent 3abf4269eb
commit 8e6b086e84
4 changed files with 43 additions and 26 deletions

View file

@ -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 async_graphql_axum::{GraphQLRequest, GraphQLResponse};
use axum::{ use axum::{
Router, Router,
routing::{get, post}, routing::{get, post},
}; };
use schema::build_schema;
use std::net::SocketAddr; 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] #[tokio::main]
async fn main() { async fn main() {
let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription).finish(); let schema = build_schema();
let app = Router::new() let app = Router::new()
.route( .route(

7
src/models.rs Normal file
View file

@ -0,0 +1,7 @@
use async_graphql::SimpleObject;
#[derive(SimpleObject)]
pub struct User {
pub id: i32,
pub name: String,
}

22
src/resolvers.rs Normal file
View file

@ -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"),
}
}
}

8
src/schema.rs Normal file
View file

@ -0,0 +1,8 @@
use crate::resolvers::QueryRoot;
use async_graphql::{EmptyMutation, EmptySubscription, Schema};
pub type AppSchema = Schema<QueryRoot, EmptyMutation, EmptySubscription>;
pub fn build_schema() -> AppSchema {
Schema::build(QueryRoot, EmptyMutation, EmptySubscription).finish()
}