From b14c9469d36d0daf7577d8635a197ad95301b46e Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Fri, 3 May 2024 13:56:35 +0800 Subject: [PATCH 01/13] Added `ColumnType::Blob` --- .github/workflows/tests.yaml | 4 ++++ Cargo.toml | 6 +++++- generator/Cargo.toml | 2 +- src/builder_context/filter_types_map.rs | 1 + src/builder_context/types_map.rs | 6 ++++-- 5 files changed, 15 insertions(+), 4 deletions(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 0e5adfaa..0080e087 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -78,6 +78,7 @@ jobs: with: crate: sea-orm-cli git: https://github.com/SeaQL/sea-orm + branch: column-type-blob - name: Remove generated folder run: rm -rf ./examples/sqlite/src - name: Copy sample database @@ -118,6 +119,7 @@ jobs: with: crate: sea-orm-cli git: https://github.com/SeaQL/sea-orm + branch: column-type-blob - name: Remove generated folder run: rm -rf ./examples/sqlite/src - name: Copy sample database @@ -173,6 +175,7 @@ jobs: with: crate: sea-orm-cli git: https://github.com/SeaQL/sea-orm + branch: column-type-blob - name: Remove generated folder run: rm -rf ./examples/mysql/src - name: Create DB @@ -237,6 +240,7 @@ jobs: with: crate: sea-orm-cli git: https://github.com/SeaQL/sea-orm + branch: column-type-blob - name: Remove generated folder run: rm -rf ./examples/postgres/src - name: Create DB diff --git a/Cargo.toml b/Cargo.toml index 3ed72846..525eaa3e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,4 +33,8 @@ with-decimal = ["sea-orm/with-rust_decimal", "async-graphql/decimal"] with-bigdecimal = ["sea-orm/with-bigdecimal", "async-graphql/bigdecimal"] with-postgres-array = ["sea-orm/postgres-array"] # with-ipnetwork = ["sea-orm/with-ipnetwork"] -# with-mac_address = ["sea-orm/with-mac_address"] \ No newline at end of file +# with-mac_address = ["sea-orm/with-mac_address"] + +[patch.crates-io] +sea-query = { git = "https://github.com/SeaQL/sea-query", branch = "column-type-blob" } +sea-orm = { git = "https://github.com/SeaQL/sea-orm", branch = "column-type-blob" } diff --git a/generator/Cargo.toml b/generator/Cargo.toml index 52caf1cb..ad115a0d 100644 --- a/generator/Cargo.toml +++ b/generator/Cargo.toml @@ -18,5 +18,5 @@ proc-macro2 = "1.0.66" syn = { version = "2.0.27", features = ["full"] } heck = "0.4.1" itertools = "0.11.0" -sea-query = { version = "0.30.0", default-features = false } +sea-query = { version = "0.31.0-rc", default-features = false } thiserror = "1.0.44" \ No newline at end of file diff --git a/src/builder_context/filter_types_map.rs b/src/builder_context/filter_types_map.rs index bf16e6cc..7a2e30c7 100644 --- a/src/builder_context/filter_types_map.rs +++ b/src/builder_context/filter_types_map.rs @@ -212,6 +212,7 @@ impl FilterTypesMapHelper { ColumnType::VarBinary(_) => None, ColumnType::Bit(_) => None, ColumnType::VarBit(_) => None, + ColumnType::Blob => None, ColumnType::Boolean => Some(FilterType::Boolean), ColumnType::Money(_) => Some(FilterType::Text), ColumnType::Json => None, diff --git a/src/builder_context/types_map.rs b/src/builder_context/types_map.rs index 6ff6f7a7..bb6130b8 100644 --- a/src/builder_context/types_map.rs +++ b/src/builder_context/types_map.rs @@ -153,7 +153,8 @@ impl TypesMapHelper { ColumnType::Binary(_) | ColumnType::VarBinary(_) | ColumnType::Bit(_) - | ColumnType::VarBit(_) => ConvertedType::Bytes, + | ColumnType::VarBit(_) + | ColumnType::Blob => ConvertedType::Bytes, ColumnType::Boolean => ConvertedType::Bool, #[cfg(not(feature = "with-json"))] @@ -269,7 +270,8 @@ impl TypesMapHelper { ColumnType::Binary(_) | ColumnType::VarBinary(_) | ColumnType::Bit(_) - | ColumnType::VarBit(_) => Some(TypeRef::named(TypeRef::STRING)), + | ColumnType::VarBit(_) + | ColumnType::Blob => Some(TypeRef::named(TypeRef::STRING)), ColumnType::Boolean => Some(TypeRef::named(TypeRef::BOOLEAN)), // FIXME: support json type ColumnType::Json | ColumnType::JsonBinary => None, From 9716e413ef9a178def32b0ae5984fe6ba541b8cf Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Fri, 3 May 2024 14:10:39 +0800 Subject: [PATCH 02/13] Fix --- examples/mysql/Cargo.toml | 6 +++++- examples/postgres/Cargo.toml | 6 +++++- examples/sqlite/Cargo.toml | 6 +++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/examples/mysql/Cargo.toml b/examples/mysql/Cargo.toml index 4da38e95..ebc0f18e 100644 --- a/examples/mysql/Cargo.toml +++ b/examples/mysql/Cargo.toml @@ -23,4 +23,8 @@ features = ["with-decimal", "with-chrono"] serde_json = { version = "1.0.103" } [workspace] -members = [] \ No newline at end of file +members = [] + +[patch.crates-io] +sea-query = { git = "https://github.com/SeaQL/sea-query", branch = "column-type-blob" } +sea-orm = { git = "https://github.com/SeaQL/sea-orm", branch = "column-type-blob" } diff --git a/examples/postgres/Cargo.toml b/examples/postgres/Cargo.toml index 6abad24e..57fba079 100644 --- a/examples/postgres/Cargo.toml +++ b/examples/postgres/Cargo.toml @@ -23,4 +23,8 @@ features = ["with-decimal", "with-chrono", "with-postgres-array"] serde_json = { version = "1.0.103" } [workspace] -members = [] \ No newline at end of file +members = [] + +[patch.crates-io] +sea-query = { git = "https://github.com/SeaQL/sea-query", branch = "column-type-blob" } +sea-orm = { git = "https://github.com/SeaQL/sea-orm", branch = "column-type-blob" } diff --git a/examples/sqlite/Cargo.toml b/examples/sqlite/Cargo.toml index b8fc4178..89340345 100644 --- a/examples/sqlite/Cargo.toml +++ b/examples/sqlite/Cargo.toml @@ -23,4 +23,8 @@ features = ["with-decimal", "with-chrono"] serde_json = { version = "1.0.103" } [workspace] -members = [] \ No newline at end of file +members = [] + +[patch.crates-io] +sea-query = { git = "https://github.com/SeaQL/sea-query", branch = "column-type-blob" } +sea-orm = { git = "https://github.com/SeaQL/sea-orm", branch = "column-type-blob" } From 51f54b6dec4a12a44c8a3faaeff3bca3daaf3745 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Fri, 3 May 2024 14:29:10 +0800 Subject: [PATCH 03/13] Ty --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index 525eaa3e..ceb8ac8c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,3 +38,4 @@ with-postgres-array = ["sea-orm/postgres-array"] [patch.crates-io] sea-query = { git = "https://github.com/SeaQL/sea-query", branch = "column-type-blob" } sea-orm = { git = "https://github.com/SeaQL/sea-orm", branch = "column-type-blob" } + From 383a264e1e5bc3ad31099124a63868a57d7f61dd Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Fri, 3 May 2024 16:18:31 +0800 Subject: [PATCH 04/13] bump --- Cargo.toml | 5 ----- examples/mysql/Cargo.toml | 4 ---- examples/postgres/Cargo.toml | 4 ---- examples/sqlite/Cargo.toml | 4 ---- 4 files changed, 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ceb8ac8c..d959167c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,8 +34,3 @@ with-bigdecimal = ["sea-orm/with-bigdecimal", "async-graphql/bigdecimal"] with-postgres-array = ["sea-orm/postgres-array"] # with-ipnetwork = ["sea-orm/with-ipnetwork"] # with-mac_address = ["sea-orm/with-mac_address"] - -[patch.crates-io] -sea-query = { git = "https://github.com/SeaQL/sea-query", branch = "column-type-blob" } -sea-orm = { git = "https://github.com/SeaQL/sea-orm", branch = "column-type-blob" } - diff --git a/examples/mysql/Cargo.toml b/examples/mysql/Cargo.toml index ebc0f18e..2e3ad7cf 100644 --- a/examples/mysql/Cargo.toml +++ b/examples/mysql/Cargo.toml @@ -24,7 +24,3 @@ serde_json = { version = "1.0.103" } [workspace] members = [] - -[patch.crates-io] -sea-query = { git = "https://github.com/SeaQL/sea-query", branch = "column-type-blob" } -sea-orm = { git = "https://github.com/SeaQL/sea-orm", branch = "column-type-blob" } diff --git a/examples/postgres/Cargo.toml b/examples/postgres/Cargo.toml index 57fba079..db109ed5 100644 --- a/examples/postgres/Cargo.toml +++ b/examples/postgres/Cargo.toml @@ -24,7 +24,3 @@ serde_json = { version = "1.0.103" } [workspace] members = [] - -[patch.crates-io] -sea-query = { git = "https://github.com/SeaQL/sea-query", branch = "column-type-blob" } -sea-orm = { git = "https://github.com/SeaQL/sea-orm", branch = "column-type-blob" } diff --git a/examples/sqlite/Cargo.toml b/examples/sqlite/Cargo.toml index 89340345..f4bacaeb 100644 --- a/examples/sqlite/Cargo.toml +++ b/examples/sqlite/Cargo.toml @@ -24,7 +24,3 @@ serde_json = { version = "1.0.103" } [workspace] members = [] - -[patch.crates-io] -sea-query = { git = "https://github.com/SeaQL/sea-query", branch = "column-type-blob" } -sea-orm = { git = "https://github.com/SeaQL/sea-orm", branch = "column-type-blob" } From 8d9fe2166ce637c89e4fe90d07515f92e2b5af67 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Fri, 3 May 2024 16:19:14 +0800 Subject: [PATCH 05/13] CI --- .github/workflows/tests.yaml | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 0080e087..0ac261d2 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -77,8 +77,7 @@ jobs: uses: baptiste0928/cargo-install@v2 with: crate: sea-orm-cli - git: https://github.com/SeaQL/sea-orm - branch: column-type-blob + version: 1.0.0-rc.4 - name: Remove generated folder run: rm -rf ./examples/sqlite/src - name: Copy sample database @@ -118,8 +117,7 @@ jobs: uses: baptiste0928/cargo-install@v2 with: crate: sea-orm-cli - git: https://github.com/SeaQL/sea-orm - branch: column-type-blob + version: 1.0.0-rc.4 - name: Remove generated folder run: rm -rf ./examples/sqlite/src - name: Copy sample database @@ -174,8 +172,7 @@ jobs: uses: baptiste0928/cargo-install@v2 with: crate: sea-orm-cli - git: https://github.com/SeaQL/sea-orm - branch: column-type-blob + version: 1.0.0-rc.4 - name: Remove generated folder run: rm -rf ./examples/mysql/src - name: Create DB @@ -239,8 +236,7 @@ jobs: uses: baptiste0928/cargo-install@v2 with: crate: sea-orm-cli - git: https://github.com/SeaQL/sea-orm - branch: column-type-blob + version: 1.0.0-rc.4 - name: Remove generated folder run: rm -rf ./examples/postgres/src - name: Create DB From e459f6b04233ef49fc9308f44d48a8fc3393e3a8 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Fri, 3 May 2024 18:14:26 +0800 Subject: [PATCH 06/13] Actix SQLite example --- examples/sqlite/Cargo.toml | 6 ++--- examples/sqlite/src/main.rs | 47 ++++++++++++++++++++++++------------- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/examples/sqlite/Cargo.toml b/examples/sqlite/Cargo.toml index f4bacaeb..0f5d4d29 100644 --- a/examples/sqlite/Cargo.toml +++ b/examples/sqlite/Cargo.toml @@ -4,11 +4,11 @@ name = "seaography-sqlite-example" version = "1.0.0-rc.3" [dependencies] -poem = { version = "3.0" } -async-graphql-poem = { version = "7.0" } +actix-web = { version = "4.5", default-features = false, features = ["macros"] } +async-graphql-actix-web = { version = "7.0" } async-graphql = { version = "7.0", features = ["decimal", "chrono", "dataloader", "dynamic-schema"] } dotenv = "0.15.0" -sea-orm = { version = "1.0.0-rc", features = ["sqlx-sqlite", "runtime-async-std-native-tls", "seaography"] } +sea-orm = { version = "1.0.0-rc", features = ["sqlx-sqlite", "runtime-async-std-rustls", "seaography"] } tokio = { version = "1.29.1", features = ["macros", "rt-multi-thread"] } tracing = { version = "0.1.37" } tracing-subscriber = { version = "0.3.17" } diff --git a/examples/sqlite/src/main.rs b/examples/sqlite/src/main.rs index 0b8c045c..6d8532de 100644 --- a/examples/sqlite/src/main.rs +++ b/examples/sqlite/src/main.rs @@ -1,8 +1,11 @@ -use async_graphql::http::{playground_source, GraphQLPlaygroundConfig}; -use async_graphql_poem::GraphQL; +use actix_web::{guard, web, web::Data, App, HttpResponse, HttpServer, Result}; +use async_graphql::{ + dynamic::*, + http::{playground_source, GraphQLPlaygroundConfig}, +}; +use async_graphql_actix_web::{GraphQLRequest, GraphQLResponse}; use dotenv::dotenv; use lazy_static::lazy_static; -use poem::{get, handler, listener::TcpListener, web::Html, IntoResponse, Route, Server}; use sea_orm::Database; use std::env; @@ -20,13 +23,20 @@ lazy_static! { }); } -#[handler] -async fn graphql_playground() -> impl IntoResponse { - Html(playground_source(GraphQLPlaygroundConfig::new(&ENDPOINT))) +async fn index(schema: web::Data, req: GraphQLRequest) -> GraphQLResponse { + schema.execute(req.into_inner()).await.into() } -#[tokio::main] -async fn main() { +async fn graphql_playground() -> Result { + Ok(HttpResponse::Ok() + .content_type("text/html; charset=utf-8") + .body(playground_source(GraphQLPlaygroundConfig::new( + "http://localhost:8000", + )))) +} + +#[actix_web::main] +async fn main() -> std::io::Result<()> { dotenv().ok(); tracing_subscriber::fmt() .with_max_level(tracing::Level::INFO) @@ -38,13 +48,18 @@ async fn main() { let schema = seaography_sqlite_example::query_root::schema(database, *DEPTH_LIMIT, *COMPLEXITY_LIMIT) .unwrap(); - let app = Route::new().at( - &*ENDPOINT, - get(graphql_playground).post(GraphQL::new(schema)), - ); println!("Visit GraphQL Playground at http://{}", *URL); - Server::new(TcpListener::bind(&*URL)) - .run(app) - .await - .expect("Fail to start web server"); + HttpServer::new(move || { + App::new() + .app_data(Data::new(schema.clone())) + .service(web::resource("/").guard(guard::Post()).to(index)) + .service( + web::resource("/") + .guard(guard::Get()) + .to(graphql_playground), + ) + }) + .bind("127.0.0.1:8000")? + .run() + .await } From faf1e07acaeef64f504b0aaf28bb083532f52689 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Fri, 3 May 2024 18:39:59 +0800 Subject: [PATCH 07/13] Axum MySQL example --- cli/src/main.rs | 2 + examples/mysql/Cargo.toml | 4 +- examples/mysql/src/main.rs | 26 ++++---- generator/src/lib.rs | 2 + generator/src/templates/axum.rs | 79 +++++++++++++++++++++++++ generator/src/templates/axum_cargo.toml | 25 ++++++++ generator/src/templates/mod.rs | 1 + generator/src/writer.rs | 1 + 8 files changed, 125 insertions(+), 15 deletions(-) create mode 100644 generator/src/templates/axum.rs create mode 100644 generator/src/templates/axum_cargo.toml diff --git a/cli/src/main.rs b/cli/src/main.rs index ed2de028..6aa454d2 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -115,6 +115,7 @@ async fn main() { pub enum WebFrameworkEnum { Actix, Poem, + Axum, } impl From for seaography_generator::WebFrameworkEnum { @@ -122,6 +123,7 @@ impl From for seaography_generator::WebFrameworkEnum { match framework { WebFrameworkEnum::Actix => seaography_generator::WebFrameworkEnum::Actix, WebFrameworkEnum::Poem => seaography_generator::WebFrameworkEnum::Poem, + WebFrameworkEnum::Axum => seaography_generator::WebFrameworkEnum::Axum, } } } diff --git a/examples/mysql/Cargo.toml b/examples/mysql/Cargo.toml index 2e3ad7cf..51152179 100644 --- a/examples/mysql/Cargo.toml +++ b/examples/mysql/Cargo.toml @@ -4,8 +4,8 @@ name = "seaography-mysql-example" version = "1.0.0-rc.3" [dependencies] -poem = { version = "3.0" } -async-graphql-poem = { version = "7.0" } +axum = { version = "0.7" } +async-graphql-axum = { version = "7.0" } async-graphql = { version = "7.0", features = ["decimal", "chrono", "dataloader", "dynamic-schema"] } dotenv = "0.15.0" sea-orm = { version = "1.0.0-rc", features = ["sqlx-mysql", "runtime-async-std-native-tls", "seaography"] } diff --git a/examples/mysql/src/main.rs b/examples/mysql/src/main.rs index 89f931c7..9197c61d 100644 --- a/examples/mysql/src/main.rs +++ b/examples/mysql/src/main.rs @@ -1,13 +1,18 @@ use async_graphql::http::{playground_source, GraphQLPlaygroundConfig}; -use async_graphql_poem::GraphQL; +use async_graphql_axum::GraphQL; +use axum::{ + response::{self, IntoResponse}, + routing::get, + Router, +}; use dotenv::dotenv; use lazy_static::lazy_static; -use poem::{get, handler, listener::TcpListener, web::Html, IntoResponse, Route, Server}; use sea_orm::Database; use std::env; +use tokio::net::TcpListener; lazy_static! { - static ref URL: String = env::var("URL").unwrap_or("0.0.0.0:8000".into()); + static ref URL: String = env::var("URL").unwrap_or("localhost:8000".into()); static ref ENDPOINT: String = env::var("ENDPOINT").unwrap_or("/".into()); static ref DATABASE_URL: String = env::var("DATABASE_URL").expect("DATABASE_URL environment variable not set"); @@ -20,9 +25,8 @@ lazy_static! { }); } -#[handler] -async fn graphql_playground() -> impl IntoResponse { - Html(playground_source(GraphQLPlaygroundConfig::new(&ENDPOINT))) +async fn graphiql() -> impl IntoResponse { + response::Html(playground_source(GraphQLPlaygroundConfig::new(&*ENDPOINT))) } #[tokio::main] @@ -38,13 +42,9 @@ async fn main() { let schema = seaography_mysql_example::query_root::schema(database, *DEPTH_LIMIT, *COMPLEXITY_LIMIT) .unwrap(); - let app = Route::new().at( - &*ENDPOINT, - get(graphql_playground).post(GraphQL::new(schema)), - ); + let app = Router::new().route("/", get(graphiql).post_service(GraphQL::new(schema))); println!("Visit GraphQL Playground at http://{}", *URL); - Server::new(TcpListener::bind(&*URL)) - .run(app) + axum::serve(TcpListener::bind(&*URL).await.unwrap(), app) .await - .expect("Fail to start web server"); + .unwrap(); } diff --git a/generator/src/lib.rs b/generator/src/lib.rs index 141372d0..c99f1436 100644 --- a/generator/src/lib.rs +++ b/generator/src/lib.rs @@ -10,6 +10,7 @@ mod util; pub enum WebFrameworkEnum { Actix, Poem, + Axum, } #[allow(clippy::too_many_arguments)] @@ -33,6 +34,7 @@ pub async fn write_project, T: AsRef> match framework { WebFrameworkEnum::Actix => crate::templates::actix::write_main(src_path, crate_name)?, WebFrameworkEnum::Poem => crate::templates::poem::write_main(src_path, crate_name)?, + WebFrameworkEnum::Axum => crate::templates::axum::write_main(src_path, crate_name)?, } writer::write_env(&root_path.as_ref(), db_url, depth_limit, complexity_limit)?; diff --git a/generator/src/templates/axum.rs b/generator/src/templates/axum.rs new file mode 100644 index 00000000..da88e272 --- /dev/null +++ b/generator/src/templates/axum.rs @@ -0,0 +1,79 @@ +use proc_macro2::TokenStream; +use quote::quote; + +use crate::util::add_line_break; + +/// +/// Used to generate project/src/main.rs file content +/// +pub fn generate_main(crate_name: &str) -> TokenStream { + let crate_name_token: TokenStream = crate_name.replace('-', "_").parse().unwrap(); + + quote! { + use dotenv::dotenv; + use lazy_static::lazy_static; + use sea_orm::Database; + use async_graphql::{ + http::{playground_source, GraphQLPlaygroundConfig}, + dynamic::*, + }; + use async_graphql_axum::GraphQL; + use axum::{ + response::{self, IntoResponse}, + routing::get, + Router, + }; + use tokio::net::TcpListener; + use std::env; + + lazy_static! { + static ref URL: String = env::var("URL").unwrap_or("localhost:8000".into()); + static ref ENDPOINT: String = env::var("ENDPOINT").unwrap_or("/".into()); + static ref DATABASE_URL: String = + env::var("DATABASE_URL").expect("DATABASE_URL environment variable not set"); + static ref DEPTH_LIMIT: Option = env::var("DEPTH_LIMIT").map_or(None, |data| Some( + data.parse().expect("DEPTH_LIMIT is not a number") + )); + static ref COMPLEXITY_LIMIT: Option = env::var("COMPLEXITY_LIMIT") + .map_or(None, |data| { + Some(data.parse().expect("COMPLEXITY_LIMIT is not a number")) + }); + } + + async fn graphiql() -> impl IntoResponse { + response::Html(playground_source(GraphQLPlaygroundConfig::new("http://localhost:8000"))) + } + + #[tokio::main] + async fn main() { + dotenv().ok(); + tracing_subscriber::fmt() + .with_max_level(tracing::Level::INFO) + .with_test_writer() + .init(); + let database = Database::connect(&*DATABASE_URL) + .await + .expect("Fail to initialize database connection"); + + let schema = #crate_name_token::query_root::schema(database, *DEPTH_LIMIT, *COMPLEXITY_LIMIT).unwrap(); + + let app = Router::new().route("/", get(graphiql).post_service(GraphQL::new(schema))); + + println!("Visit GraphQL Playground at http://{}", *URL); + + axum::serve(TcpListener::bind(&*URL).await.unwrap(), app) + .await + .unwrap(); + } + } +} + +pub fn write_main>(path: &P, crate_name: &str) -> std::io::Result<()> { + let tokens = generate_main(crate_name); + + let file_name = path.as_ref().join("main.rs"); + + std::fs::write(file_name, add_line_break(tokens))?; + + Ok(()) +} diff --git a/generator/src/templates/axum_cargo.toml b/generator/src/templates/axum_cargo.toml new file mode 100644 index 00000000..dde1f514 --- /dev/null +++ b/generator/src/templates/axum_cargo.toml @@ -0,0 +1,25 @@ +[package] +edition = "2021" +name = "" +version = "0.1.0" + +[dependencies] +axum = { version = "0.7" } +async-graphql-axum = { version = "7.0" } +async-graphql = { version = "7.0", features = ["decimal", "chrono", "dataloader", "dynamic-schema"] } +dotenv = "0.15.0" +sea-orm = { version = "1.0.0-rc", features = ["", "runtime-async-std-native-tls", "seaography"] } +tokio = { version = "1.29.1", features = ["macros", "rt-multi-thread"] } +tracing = { version = "0.1.37" } +tracing-subscriber = { version = "0.3.17" } +lazy_static = { version = "1.4.0" } + +[dependencies.seaography] +version = "" # seaography version +features = ["with-decimal", "with-chrono"] + +[dev-dependencies] +serde_json = { version = "1.0.103" } + +[workspace] +members = [] \ No newline at end of file diff --git a/generator/src/templates/mod.rs b/generator/src/templates/mod.rs index fa0b4158..8693228a 100644 --- a/generator/src/templates/mod.rs +++ b/generator/src/templates/mod.rs @@ -1,2 +1,3 @@ pub mod actix; pub mod poem; +pub mod axum; diff --git a/generator/src/writer.rs b/generator/src/writer.rs index 48a7bc1f..68e8efc5 100644 --- a/generator/src/writer.rs +++ b/generator/src/writer.rs @@ -155,6 +155,7 @@ pub fn write_cargo_toml>( let content = match framework { WebFrameworkEnum::Actix => include_str!("./templates/actix_cargo.toml"), WebFrameworkEnum::Poem => include_str!("./templates/poem_cargo.toml"), + WebFrameworkEnum::Axum => include_str!("./templates/axum_cargo.toml"), }; let content = content From 666f77e55988f0a4a30f1672df815fc350f86661 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Fri, 3 May 2024 18:40:41 +0800 Subject: [PATCH 08/13] WIP --- .github/workflows/tests.yaml | 46 ++---------------------- examples/postgres/src/main.rs | 2 +- examples/sqlite/src/main.rs | 2 +- generator/src/templates/actix.rs | 2 +- generator/src/templates/actix_cargo.toml | 4 +-- generator/src/templates/poem.rs | 2 +- generator/src/templates/poem_cargo.toml | 2 +- 7 files changed, 10 insertions(+), 50 deletions(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 0ac261d2..a1e7109e 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -64,49 +64,9 @@ jobs: toolchain: stable components: clippy - run: cargo clippy --all -- -D warnings - integration-sqlite-poem: + integration-sqlite: name: SQLite integration tests runs-on: ubuntu-latest - needs: - - check - - test - steps: - - uses: actions/checkout@v3 - - uses: dtolnay/rust-toolchain@stable - - name: Install sea-orm-cli - uses: baptiste0928/cargo-install@v2 - with: - crate: sea-orm-cli - version: 1.0.0-rc.4 - - name: Remove generated folder - run: rm -rf ./examples/sqlite/src - - name: Copy sample database - run: cp ./examples/sqlite/sakila.db . - - name: Generate entities - run: >- - sea-orm-cli generate entity -o examples/sqlite/src/entities -u - sqlite://sakila.db --seaography - - name: Generate Seaography project - uses: actions-rs/cargo@v1 - with: - command: run - args: > - --package seaography-cli -- ./examples/sqlite - ./examples/sqlite/src/entities sqlite://sakila.db - seaography-sqlite-example -f poem - - name: Depends on local seaography - run: >- - sed -i '/^\[dependencies.seaography\]$/a \path = "..\/..\/"' - ./examples/sqlite/Cargo.toml - - name: Build example - working-directory: ./examples/sqlite - run: cargo build - - name: Integration tests - working-directory: ./examples/sqlite - run: cargo test - integration-sqlite-actix: - name: SQLite integration tests actix - runs-on: ubuntu-latest needs: - check - test @@ -202,7 +162,7 @@ jobs: - name: Depends on local seaography run: >- sed -i '/^\[dependencies.seaography\]$/a \path = "..\/..\/"' - ./examples/mysql/Cargo.toml + ./examples/mysql/Cargo.toml -f axum - name: Fix Nullable not implemented for Vec and tsvector run: 'sed -i "24,28d" ./examples/mysql/src/entities/film.rs' - name: Build example @@ -265,7 +225,7 @@ jobs: - name: Depends on local seaography run: >- sed -i '/^\[dependencies.seaography\]$/a \path = "..\/..\/"' - ./examples/postgres/Cargo.toml + ./examples/postgres/Cargo.toml -f poem - name: Fix Nullable not implemented for Vec and tsvector run: 'sed -i "26,27d" ./examples/postgres/src/entities/film.rs' - name: Build example diff --git a/examples/postgres/src/main.rs b/examples/postgres/src/main.rs index afdc96a8..4ad4d788 100644 --- a/examples/postgres/src/main.rs +++ b/examples/postgres/src/main.rs @@ -7,7 +7,7 @@ use sea_orm::Database; use std::env; lazy_static! { - static ref URL: String = env::var("URL").unwrap_or("0.0.0.0:8000".into()); + static ref URL: String = env::var("URL").unwrap_or("localhost:8000".into()); static ref ENDPOINT: String = env::var("ENDPOINT").unwrap_or("/".into()); static ref DATABASE_URL: String = env::var("DATABASE_URL").expect("DATABASE_URL environment variable not set"); diff --git a/examples/sqlite/src/main.rs b/examples/sqlite/src/main.rs index 6d8532de..ac6d89c3 100644 --- a/examples/sqlite/src/main.rs +++ b/examples/sqlite/src/main.rs @@ -10,7 +10,7 @@ use sea_orm::Database; use std::env; lazy_static! { - static ref URL: String = env::var("URL").unwrap_or("0.0.0.0:8000".into()); + static ref URL: String = env::var("URL").unwrap_or("localhost:8000".into()); static ref ENDPOINT: String = env::var("ENDPOINT").unwrap_or("/".into()); static ref DATABASE_URL: String = env::var("DATABASE_URL").expect("DATABASE_URL environment variable not set"); diff --git a/generator/src/templates/actix.rs b/generator/src/templates/actix.rs index d868a0e0..1e2d5543 100644 --- a/generator/src/templates/actix.rs +++ b/generator/src/templates/actix.rs @@ -22,7 +22,7 @@ pub fn generate_main(crate_name: &str) -> TokenStream { use std::env; lazy_static! { - static ref URL: String = env::var("URL").unwrap_or("0.0.0.0:8000".into()); + static ref URL: String = env::var("URL").unwrap_or("localhost:8000".into()); static ref ENDPOINT: String = env::var("ENDPOINT").unwrap_or("/".into()); static ref DATABASE_URL: String = env::var("DATABASE_URL").expect("DATABASE_URL environment variable not set"); diff --git a/generator/src/templates/actix_cargo.toml b/generator/src/templates/actix_cargo.toml index 5ec5d171..51e1fb0a 100644 --- a/generator/src/templates/actix_cargo.toml +++ b/generator/src/templates/actix_cargo.toml @@ -1,10 +1,10 @@ [package] edition = "2021" name = "" -version = "0.3.0" +version = "0.1.0" [dependencies] -actix-web = { version = "4.3.1", default-features = false, features = ["macros"] } +actix-web = { version = "4.5", default-features = false, features = ["macros"] } async-graphql-actix-web = { version = "7.0" } async-graphql = { version = "7.0", features = ["decimal", "chrono", "dataloader", "dynamic-schema"] } dotenv = "0.15.0" diff --git a/generator/src/templates/poem.rs b/generator/src/templates/poem.rs index f0d00bf8..b342c737 100644 --- a/generator/src/templates/poem.rs +++ b/generator/src/templates/poem.rs @@ -19,7 +19,7 @@ pub fn generate_main(crate_name: &str) -> TokenStream { use std::env; lazy_static! { - static ref URL: String = env::var("URL").unwrap_or("0.0.0.0:8000".into()); + static ref URL: String = env::var("URL").unwrap_or("localhost:8000".into()); static ref ENDPOINT: String = env::var("ENDPOINT").unwrap_or("/".into()); static ref DATABASE_URL: String = env::var("DATABASE_URL").expect("DATABASE_URL environment variable not set"); diff --git a/generator/src/templates/poem_cargo.toml b/generator/src/templates/poem_cargo.toml index 7994a6c1..77974511 100644 --- a/generator/src/templates/poem_cargo.toml +++ b/generator/src/templates/poem_cargo.toml @@ -1,7 +1,7 @@ [package] edition = "2021" name = "" -version = "0.3.0" +version = "0.1.0" [dependencies] poem = { version = "3.0" } From 3f4c71af9c17cbecae793b6beb35dbcee6860f71 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Fri, 3 May 2024 18:42:57 +0800 Subject: [PATCH 09/13] Axum --- generator/src/templates/axum.rs | 19 ++++++------------- generator/src/templates/axum_cargo.toml | 2 +- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/generator/src/templates/axum.rs b/generator/src/templates/axum.rs index da88e272..0257f4aa 100644 --- a/generator/src/templates/axum.rs +++ b/generator/src/templates/axum.rs @@ -10,21 +10,18 @@ pub fn generate_main(crate_name: &str) -> TokenStream { let crate_name_token: TokenStream = crate_name.replace('-', "_").parse().unwrap(); quote! { - use dotenv::dotenv; - use lazy_static::lazy_static; - use sea_orm::Database; - use async_graphql::{ - http::{playground_source, GraphQLPlaygroundConfig}, - dynamic::*, - }; + use async_graphql::http::{playground_source, GraphQLPlaygroundConfig}; use async_graphql_axum::GraphQL; use axum::{ response::{self, IntoResponse}, routing::get, Router, }; - use tokio::net::TcpListener; + use dotenv::dotenv; + use lazy_static::lazy_static; + use sea_orm::Database; use std::env; + use tokio::net::TcpListener; lazy_static! { static ref URL: String = env::var("URL").unwrap_or("localhost:8000".into()); @@ -41,7 +38,7 @@ pub fn generate_main(crate_name: &str) -> TokenStream { } async fn graphiql() -> impl IntoResponse { - response::Html(playground_source(GraphQLPlaygroundConfig::new("http://localhost:8000"))) + response::Html(playground_source(GraphQLPlaygroundConfig::new(&*ENDPOINT))) } #[tokio::main] @@ -54,13 +51,9 @@ pub fn generate_main(crate_name: &str) -> TokenStream { let database = Database::connect(&*DATABASE_URL) .await .expect("Fail to initialize database connection"); - let schema = #crate_name_token::query_root::schema(database, *DEPTH_LIMIT, *COMPLEXITY_LIMIT).unwrap(); - let app = Router::new().route("/", get(graphiql).post_service(GraphQL::new(schema))); - println!("Visit GraphQL Playground at http://{}", *URL); - axum::serve(TcpListener::bind(&*URL).await.unwrap(), app) .await .unwrap(); diff --git a/generator/src/templates/axum_cargo.toml b/generator/src/templates/axum_cargo.toml index dde1f514..cd338df8 100644 --- a/generator/src/templates/axum_cargo.toml +++ b/generator/src/templates/axum_cargo.toml @@ -22,4 +22,4 @@ features = ["with-decimal", "with-chrono"] serde_json = { version = "1.0.103" } [workspace] -members = [] \ No newline at end of file +members = [] From 434050a2036e54bdb6f6683243fa3f2a8cbbfc22 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Fri, 3 May 2024 18:47:03 +0800 Subject: [PATCH 10/13] Actix --- examples/sqlite/src/main.rs | 4 +--- generator/src/templates/actix.rs | 10 ++-------- generator/src/templates/actix_cargo.toml | 2 +- 3 files changed, 4 insertions(+), 12 deletions(-) diff --git a/examples/sqlite/src/main.rs b/examples/sqlite/src/main.rs index ac6d89c3..b9bf0c0f 100644 --- a/examples/sqlite/src/main.rs +++ b/examples/sqlite/src/main.rs @@ -30,9 +30,7 @@ async fn index(schema: web::Data, req: GraphQLRequest) -> GraphQLRespons async fn graphql_playground() -> Result { Ok(HttpResponse::Ok() .content_type("text/html; charset=utf-8") - .body(playground_source(GraphQLPlaygroundConfig::new( - "http://localhost:8000", - )))) + .body(playground_source(GraphQLPlaygroundConfig::new(&*ENDPOINT)))) } #[actix_web::main] diff --git a/generator/src/templates/actix.rs b/generator/src/templates/actix.rs index 1e2d5543..672c5394 100644 --- a/generator/src/templates/actix.rs +++ b/generator/src/templates/actix.rs @@ -12,8 +12,8 @@ pub fn generate_main(crate_name: &str) -> TokenStream { quote! { use actix_web::{guard, web, web::Data, App, HttpResponse, HttpServer, Result}; use async_graphql::{ - http::{playground_source, GraphQLPlaygroundConfig}, dynamic::*, + http::{playground_source, GraphQLPlaygroundConfig}, }; use async_graphql_actix_web::{GraphQLRequest, GraphQLResponse}; use dotenv::dotenv; @@ -42,9 +42,7 @@ pub fn generate_main(crate_name: &str) -> TokenStream { async fn graphql_playground() -> Result { Ok(HttpResponse::Ok() .content_type("text/html; charset=utf-8") - .body( - playground_source(GraphQLPlaygroundConfig::new("http://localhost:8000")) - )) + .body(playground_source(GraphQLPlaygroundConfig::new(&*ENDPOINT)))) } #[actix_web::main] @@ -54,15 +52,11 @@ pub fn generate_main(crate_name: &str) -> TokenStream { .with_max_level(tracing::Level::INFO) .with_test_writer() .init(); - let database = Database::connect(&*DATABASE_URL) .await .expect("Fail to initialize database connection"); - let schema = #crate_name_token::query_root::schema(database, *DEPTH_LIMIT, *COMPLEXITY_LIMIT).unwrap(); - println!("Visit GraphQL Playground at http://{}", *URL); - HttpServer::new(move || { App::new() .app_data(Data::new(schema.clone())) diff --git a/generator/src/templates/actix_cargo.toml b/generator/src/templates/actix_cargo.toml index 51e1fb0a..e4d0ce13 100644 --- a/generator/src/templates/actix_cargo.toml +++ b/generator/src/templates/actix_cargo.toml @@ -22,4 +22,4 @@ features = ["with-decimal", "with-chrono"] serde_json = { version = "1.0.103" } [workspace] -members = [] \ No newline at end of file +members = [] From 7fc1710a659652cc241930253871358eaa09c737 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Fri, 3 May 2024 18:48:44 +0800 Subject: [PATCH 11/13] Poem --- generator/src/templates/poem.rs | 2 -- generator/src/templates/poem_cargo.toml | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/generator/src/templates/poem.rs b/generator/src/templates/poem.rs index b342c737..ed4bd51d 100644 --- a/generator/src/templates/poem.rs +++ b/generator/src/templates/poem.rs @@ -47,9 +47,7 @@ pub fn generate_main(crate_name: &str) -> TokenStream { let database = Database::connect(&*DATABASE_URL) .await .expect("Fail to initialize database connection"); - let schema = #crate_name_token::query_root::schema(database, *DEPTH_LIMIT, *COMPLEXITY_LIMIT).unwrap(); - let app = Route::new().at( &*ENDPOINT, get(graphql_playground).post(GraphQL::new(schema)), diff --git a/generator/src/templates/poem_cargo.toml b/generator/src/templates/poem_cargo.toml index 77974511..e29654b6 100644 --- a/generator/src/templates/poem_cargo.toml +++ b/generator/src/templates/poem_cargo.toml @@ -22,4 +22,4 @@ features = ["with-decimal", "with-chrono"] serde_json = { version = "1.0.103" } [workspace] -members = [] \ No newline at end of file +members = [] From 4375eb99f40604e28f1321b8ec59cc313dc14c2a Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Fri, 3 May 2024 18:50:12 +0800 Subject: [PATCH 12/13] fmt --- generator/src/templates/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generator/src/templates/mod.rs b/generator/src/templates/mod.rs index 8693228a..0bdb6c7a 100644 --- a/generator/src/templates/mod.rs +++ b/generator/src/templates/mod.rs @@ -1,3 +1,3 @@ pub mod actix; -pub mod poem; pub mod axum; +pub mod poem; From 2eed68060bee4c2ceea3be5a54a86291527df75d Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Fri, 3 May 2024 21:43:43 +0800 Subject: [PATCH 13/13] Fix --- .github/workflows/tests.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index a1e7109e..154b0772 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -158,11 +158,11 @@ jobs: args: > --package seaography-cli -- ./examples/mysql ./examples/mysql/src/entities mysql://sea:sea@127.0.0.1/sakila - seaography-mysql-example + seaography-mysql-example -f axum - name: Depends on local seaography run: >- sed -i '/^\[dependencies.seaography\]$/a \path = "..\/..\/"' - ./examples/mysql/Cargo.toml -f axum + ./examples/mysql/Cargo.toml - name: Fix Nullable not implemented for Vec and tsvector run: 'sed -i "24,28d" ./examples/mysql/src/entities/film.rs' - name: Build example @@ -221,11 +221,11 @@ jobs: --package seaography-cli -- ./examples/postgres ./examples/postgres/src/entities postgres://sea:sea@127.0.0.1/sakila?currentSchema=public - seaography-postgres-example + seaography-postgres-example -f poem - name: Depends on local seaography run: >- sed -i '/^\[dependencies.seaography\]$/a \path = "..\/..\/"' - ./examples/postgres/Cargo.toml -f poem + ./examples/postgres/Cargo.toml - name: Fix Nullable not implemented for Vec and tsvector run: 'sed -i "26,27d" ./examples/postgres/src/entities/film.rs' - name: Build example