Organize the project into modules for better extendability
This commit is contained in:
parent
3abf4269eb
commit
8e6b086e84
4 changed files with 43 additions and 26 deletions
32
src/main.rs
32
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 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
7
src/models.rs
Normal 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
22
src/resolvers.rs
Normal 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
8
src/schema.rs
Normal 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()
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue