From 1e496a25ad659fe5bb8676820a363818a3b63883 Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Fri, 10 Jan 2025 18:06:08 +0000 Subject: [PATCH] Update seaography_example --- examples/seaography_example/README.md | 2 +- .../seaography_example/graphql/Cargo.toml | 18 +++------ .../graphql/src/entities/baker.rs | 2 +- .../graphql/src/entities/bakery.rs | 2 +- .../graphql/src/entities/cake.rs | 4 +- .../graphql/src/entities/cake_baker.rs | 2 +- .../graphql/src/entities/mod.rs | 4 +- .../graphql/src/entities/prelude.rs | 2 +- .../seaography_example/graphql/src/lib.rs | 6 --- .../seaography_example/graphql/src/main.rs | 38 +++---------------- .../graphql/src/query_root.rs | 29 ++++++-------- .../seaography_example/migration/README.md | 5 +++ 12 files changed, 37 insertions(+), 77 deletions(-) diff --git a/examples/seaography_example/README.md b/examples/seaography_example/README.md index 4d61714f..5c0c79d6 100644 --- a/examples/seaography_example/README.md +++ b/examples/seaography_example/README.md @@ -25,7 +25,7 @@ cargo run ## Install Seaography ```sh -cargo install seaography-cli@^1.0.0 +cargo install seaography-cli@^1.1.3 ``` ## Generate GraphQL project diff --git a/examples/seaography_example/graphql/Cargo.toml b/examples/seaography_example/graphql/Cargo.toml index 6121ca3a..5c125270 100644 --- a/examples/seaography_example/graphql/Cargo.toml +++ b/examples/seaography_example/graphql/Cargo.toml @@ -1,29 +1,23 @@ [package] edition = "2021" name = "sea-orm-seaography-example" -version = "0.3.0" -publish = false +version = "0.1.0" [dependencies] poem = { version = "3.0" } async-graphql-poem = { version = "7.0" } -async-graphql = { version = "7.0", features = ["decimal", "chrono", "dataloader", "dynamic-schema"] } -async-trait = { version = "0.1.72" } dotenv = "0.15.0" -sea-orm = { path = "../../../", features = ["sqlx-mysql", "runtime-async-std-native-tls", "seaography"] } +sea-orm = { version = "~1.1.4", features = ["sqlx-mysql", "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" } -seaography = { version = "1.1.0", features = ["with-decimal", "with-chrono"] } + +[dependencies.seaography] +version = "~1.1.3" # seaography version +features = ["with-decimal", "with-chrono"] [dev-dependencies] serde_json = { version = "1.0.103" } [workspace] members = [] - -# This allows us to develop using a local version of sea-orm -# remove this section in your own project -[patch.crates-io] -sea-orm = { path = "../../../" } diff --git a/examples/seaography_example/graphql/src/entities/baker.rs b/examples/seaography_example/graphql/src/entities/baker.rs index ba2e6ac2..55b798b8 100644 --- a/examples/seaography_example/graphql/src/entities/baker.rs +++ b/examples/seaography_example/graphql/src/entities/baker.rs @@ -1,4 +1,4 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1 +//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.4 use sea_orm::entity::prelude::*; diff --git a/examples/seaography_example/graphql/src/entities/bakery.rs b/examples/seaography_example/graphql/src/entities/bakery.rs index a168d51e..edaab9a2 100644 --- a/examples/seaography_example/graphql/src/entities/bakery.rs +++ b/examples/seaography_example/graphql/src/entities/bakery.rs @@ -1,4 +1,4 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1 +//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.4 use sea_orm::entity::prelude::*; diff --git a/examples/seaography_example/graphql/src/entities/cake.rs b/examples/seaography_example/graphql/src/entities/cake.rs index 30e1ee20..5a09c853 100644 --- a/examples/seaography_example/graphql/src/entities/cake.rs +++ b/examples/seaography_example/graphql/src/entities/cake.rs @@ -1,4 +1,4 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1 +//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.4 use sea_orm::entity::prelude::*; @@ -10,7 +10,7 @@ pub struct Model { pub name: String, #[sea_orm(column_type = "Decimal(Some((16, 4)))")] pub price: Decimal, - pub bakery_id: Option, + pub bakery_id: i32, pub gluten_free: i8, } diff --git a/examples/seaography_example/graphql/src/entities/cake_baker.rs b/examples/seaography_example/graphql/src/entities/cake_baker.rs index 7a629588..a0ecfa94 100644 --- a/examples/seaography_example/graphql/src/entities/cake_baker.rs +++ b/examples/seaography_example/graphql/src/entities/cake_baker.rs @@ -1,4 +1,4 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1 +//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.4 use sea_orm::entity::prelude::*; diff --git a/examples/seaography_example/graphql/src/entities/mod.rs b/examples/seaography_example/graphql/src/entities/mod.rs index fe886c6f..c02f7cb6 100644 --- a/examples/seaography_example/graphql/src/entities/mod.rs +++ b/examples/seaography_example/graphql/src/entities/mod.rs @@ -1,4 +1,4 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1 +//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.4 pub mod prelude; @@ -7,4 +7,4 @@ pub mod bakery; pub mod cake; pub mod cake_baker; -seaography::register_entity_modules!([baker, bakery, cake, cake_baker]); +seaography::register_entity_modules!([baker, bakery, cake, cake_baker,]); diff --git a/examples/seaography_example/graphql/src/entities/prelude.rs b/examples/seaography_example/graphql/src/entities/prelude.rs index 9433bfe6..e8d734c6 100644 --- a/examples/seaography_example/graphql/src/entities/prelude.rs +++ b/examples/seaography_example/graphql/src/entities/prelude.rs @@ -1,4 +1,4 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1 +//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.4 pub use super::baker::Entity as Baker; pub use super::bakery::Entity as Bakery; diff --git a/examples/seaography_example/graphql/src/lib.rs b/examples/seaography_example/graphql/src/lib.rs index 770aa614..a292add5 100644 --- a/examples/seaography_example/graphql/src/lib.rs +++ b/examples/seaography_example/graphql/src/lib.rs @@ -1,8 +1,2 @@ -use sea_orm::prelude::*; - pub mod entities; pub mod query_root; - -pub struct OrmDataloader { - pub db: DatabaseConnection, -} diff --git a/examples/seaography_example/graphql/src/main.rs b/examples/seaography_example/graphql/src/main.rs index 6ddab689..61178bc8 100644 --- a/examples/seaography_example/graphql/src/main.rs +++ b/examples/seaography_example/graphql/src/main.rs @@ -1,28 +1,12 @@ -use async_graphql::{ - dataloader::DataLoader, - http::{playground_source, GraphQLPlaygroundConfig}, -}; +use async_graphql::http::{playground_source, GraphQLPlaygroundConfig}; use async_graphql_poem::GraphQL; use dotenv::dotenv; -use lazy_static::lazy_static; use poem::{get, handler, listener::TcpListener, web::Html, IntoResponse, Route, Server}; use sea_orm::Database; -use sea_orm_seaography_example::*; +use seaography::{async_graphql, lazy_static}; use std::env; -lazy_static! { - static ref URL: String = env::var("URL").unwrap_or("0.0.0.0: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")) - }); -} +lazy_static::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 < usize > = env :: var ("DEPTH_LIMIT") . map_or (None , | data | Some (data . parse () . expect ("DEPTH_LIMIT is not a number"))) ; static ref COMPLEXITY_LIMIT : Option < usize > = env :: var ("COMPLEXITY_LIMIT") . map_or (None , | data | { Some (data . parse () . expect ("COMPLEXITY_LIMIT is not a number")) }) ; } #[handler] async fn graphql_playground() -> impl IntoResponse { @@ -39,19 +23,9 @@ async fn main() { let database = Database::connect(&*DATABASE_URL) .await .expect("Fail to initialize database connection"); - let orm_dataloader: DataLoader = DataLoader::new( - OrmDataloader { - db: database.clone(), - }, - tokio::spawn, - ); - let schema = sea_orm_seaography_example::query_root::schema( - database, - orm_dataloader, - *DEPTH_LIMIT, - *COMPLEXITY_LIMIT, - ) - .unwrap(); + let schema = + sea_orm_seaography_example::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/examples/seaography_example/graphql/src/query_root.rs b/examples/seaography_example/graphql/src/query_root.rs index 76972459..4126f8ec 100644 --- a/examples/seaography_example/graphql/src/query_root.rs +++ b/examples/seaography_example/graphql/src/query_root.rs @@ -1,28 +1,21 @@ -use crate::{entities::*, OrmDataloader}; -use async_graphql::{dataloader::DataLoader, dynamic::*}; +use crate::entities::*; +use async_graphql::dynamic::*; use sea_orm::DatabaseConnection; -use seaography::{Builder, BuilderContext}; +use seaography::{async_graphql, lazy_static, Builder, BuilderContext}; lazy_static::lazy_static! { static ref CONTEXT : BuilderContext = BuilderContext :: default () ; } pub fn schema( database: DatabaseConnection, - orm_dataloader: DataLoader, depth: Option, complexity: Option, ) -> Result { - let builder = Builder::new(&CONTEXT, database.clone()); - let builder = crate::entities::register_entity_modules(builder); - let schema = builder.schema_builder(); - let schema = if let Some(depth) = depth { - schema.limit_depth(depth) - } else { - schema - }; - let schema = if let Some(complexity) = complexity { - schema.limit_complexity(complexity) - } else { - schema - }; - schema.data(database).data(orm_dataloader).finish() + let mut builder = Builder::new(&CONTEXT, database.clone()); + seaography::register_entities!(builder, [baker, bakery, cake, cake_baker,]); + builder + .set_depth_limit(depth) + .set_complexity_limit(complexity) + .schema_builder() + .data(database) + .finish() } diff --git a/examples/seaography_example/migration/README.md b/examples/seaography_example/migration/README.md index cf79bde0..49d0e70f 100644 --- a/examples/seaography_example/migration/README.md +++ b/examples/seaography_example/migration/README.md @@ -1,5 +1,10 @@ # Bakery Schema +```sql +CREATE DATABASE bakery; +GRANT ALL PRIVILEGES ON bakery.* TO sea; +``` + Assume the database is named `bakery`: ```sh