-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Zli-UoA/feature/create-test-server
プロジェクトの骨組みを作ったお
- Loading branch information
Showing
16 changed files
with
1,772 additions
and
2 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod mutation; | ||
pub mod query; | ||
mod test_handler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod test; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
mod schema; | ||
pub mod server; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod test; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |