Skip to content

Commit

Permalink
Merge pull request #3 from Zli-UoA/feature/create-test-server
Browse files Browse the repository at this point in the history
プロジェクトの骨組みを作ったお
  • Loading branch information
eraser5th authored Jul 15, 2024
2 parents 78117f0 + c345938 commit c7451bf
Show file tree
Hide file tree
Showing 16 changed files with 1,772 additions and 2 deletions.
1,610 changes: 1,610 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
async-graphql = "7.0.7"
async-graphql-axum = "7.0.7"
axum = { version = "0.7.5" }
tokio = { version = "1.38.0", features = ["full"] }
async-trait = "0.1.81"
13 changes: 13 additions & 0 deletions src/context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pub struct Context {
pub env: String,
pub db: String,
}

impl Context {
pub fn new() -> Self {
Self {
env: "harukun".to_string(),
db: "yuorei".to_string(),
}
}
}
3 changes: 3 additions & 0 deletions src/handler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod mutation;
pub mod query;
mod test_handler;
5 changes: 5 additions & 0 deletions src/handler/mutation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use crate::handler::test_handler::TestMutation;
use async_graphql::MergedObject;

#[derive(MergedObject, Default)]
pub struct Mutation(TestMutation);
5 changes: 5 additions & 0 deletions src/handler/query.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use crate::handler::test_handler::TestQuery;
use async_graphql::MergedObject;

#[derive(MergedObject, Default)]
pub struct Query(TestQuery);
52 changes: 52 additions & 0 deletions src/handler/test_handler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use crate::context::Context;
use crate::models::test::TestObj;
use crate::usecase;
use async_graphql::{InputObject, MergedObject, Object, SimpleObject};

#[derive(Default)]
pub struct TestQuery;

#[Object]
impl TestQuery {
async fn obj<'ctx>(&self, ctx: &async_graphql::Context<'ctx>) -> TestObj {
let ctx = ctx.data_unchecked::<Context>();
usecase::test::get_obj(ctx)
}
}

#[derive(Default, MergedObject)]
pub struct TestMutation(CreateTestObjMutation);

#[derive(Default)]
pub struct CreateTestObjMutation;

#[derive(InputObject)]
pub struct CreateTestObjInput {
#[graphql(validator(max_length = 255))]
pub name: String,
#[graphql(validator(minimum = 1))]
pub num: usize,
}

#[derive(SimpleObject, Debug)]
pub struct CreateTestObjPayload {
pub obj: TestObj,
}

#[Object]
impl CreateTestObjMutation {
async fn create_obj(
&self,
input: CreateTestObjInput,
) -> Result<CreateTestObjPayload, async_graphql::Error> {
let Ok(obj) = usecase::test::create_obj(input.name, input.num) else {
return Err(async_graphql::Error {
message: "yuorei".to_string(),
source: None,
extensions: None,
});
};

Ok(CreateTestObjPayload { obj })
}
}
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod context;
mod handler;
mod models;
pub mod server;
mod usecase;
9 changes: 7 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
fn main() {
println!("Hello, world!");
use zoo_backend::server::server;

#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
server::run_server().await?;

Ok(())
}
1 change: 1 addition & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod test;
8 changes: 8 additions & 0 deletions src/models/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use async_graphql::SimpleObject;

#[derive(SimpleObject, Debug)]
pub struct TestObj {
pub id: String,
pub name: String,
pub num: usize,
}
2 changes: 2 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod schema;
pub mod server;
9 changes: 9 additions & 0 deletions src/server/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use crate::handler::mutation::Mutation;
use crate::handler::query::Query;
use async_graphql::{EmptySubscription, Schema, SchemaBuilder};

pub fn create_schema() -> SchemaBuilder<Query, Mutation, EmptySubscription> {
let schema_builder = Schema::build(Query::default(), Mutation::default(), EmptySubscription);

schema_builder
}
26 changes: 26 additions & 0 deletions src/server/server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use async_graphql::http::GraphiQLSource;
use async_graphql_axum::GraphQL;
use axum::{
response::{self, IntoResponse},
routing::get,
Router,
};
use tokio::net::TcpListener;

use super::schema::create_schema;
use crate::context::Context;

async fn graphiql() -> impl IntoResponse {
response::Html(GraphiQLSource::build().endpoint("/").finish())
}

pub async fn run_server() -> Result<(), std::io::Error> {
let schema = create_schema().data(Context::new()).finish();
let app = Router::new().route("/", get(graphiql).post_service(GraphQL::new(schema)));

println!("GraphiQL IDE: http://localhost:8000");

axum::serve(TcpListener::bind("127.0.0.1:8000").await.unwrap(), app).await?;

Ok(())
}
1 change: 1 addition & 0 deletions src/usecase.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod test;
20 changes: 20 additions & 0 deletions src/usecase/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::context::Context;
use crate::models::test::TestObj;

pub fn get_obj(ctx: &Context) -> TestObj {
TestObj {
id: ctx.db.clone(),
name: ctx.env.clone(),
num: 1,
}
}

pub fn create_obj(name: String, num: usize) -> Result<TestObj, std::io::Error> {
let input = TestObj {
id: "test_input".to_string(),
name,
num,
};
println!("{:?} created!", input);
Ok(input)
}

0 comments on commit c7451bf

Please sign in to comment.