Update seaography_example

This commit is contained in:
Chris Tsang 2025-01-10 18:06:08 +00:00
parent 216479c6f6
commit 1e496a25ad
12 changed files with 37 additions and 77 deletions

View File

@ -25,7 +25,7 @@ cargo run
## Install Seaography ## Install Seaography
```sh ```sh
cargo install seaography-cli@^1.0.0 cargo install seaography-cli@^1.1.3
``` ```
## Generate GraphQL project ## Generate GraphQL project

View File

@ -1,29 +1,23 @@
[package] [package]
edition = "2021" edition = "2021"
name = "sea-orm-seaography-example" name = "sea-orm-seaography-example"
version = "0.3.0" version = "0.1.0"
publish = false
[dependencies] [dependencies]
poem = { version = "3.0" } poem = { version = "3.0" }
async-graphql-poem = { version = "7.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" 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"] } tokio = { version = "1.29.1", features = ["macros", "rt-multi-thread"] }
tracing = { version = "0.1.37" } tracing = { version = "0.1.37" }
tracing-subscriber = { version = "0.3.17" } 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] [dev-dependencies]
serde_json = { version = "1.0.103" } serde_json = { version = "1.0.103" }
[workspace] [workspace]
members = [] 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 = "../../../" }

View File

@ -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::*; use sea_orm::entity::prelude::*;

View File

@ -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::*; use sea_orm::entity::prelude::*;

View File

@ -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::*; use sea_orm::entity::prelude::*;
@ -10,7 +10,7 @@ pub struct Model {
pub name: String, pub name: String,
#[sea_orm(column_type = "Decimal(Some((16, 4)))")] #[sea_orm(column_type = "Decimal(Some((16, 4)))")]
pub price: Decimal, pub price: Decimal,
pub bakery_id: Option<i32>, pub bakery_id: i32,
pub gluten_free: i8, pub gluten_free: i8,
} }

View File

@ -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::*; use sea_orm::entity::prelude::*;

View File

@ -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; pub mod prelude;
@ -7,4 +7,4 @@ pub mod bakery;
pub mod cake; pub mod cake;
pub mod cake_baker; pub mod cake_baker;
seaography::register_entity_modules!([baker, bakery, cake, cake_baker]); seaography::register_entity_modules!([baker, bakery, cake, cake_baker,]);

View File

@ -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::baker::Entity as Baker;
pub use super::bakery::Entity as Bakery; pub use super::bakery::Entity as Bakery;

View File

@ -1,8 +1,2 @@
use sea_orm::prelude::*;
pub mod entities; pub mod entities;
pub mod query_root; pub mod query_root;
pub struct OrmDataloader {
pub db: DatabaseConnection,
}

View File

@ -1,28 +1,12 @@
use async_graphql::{ use async_graphql::http::{playground_source, GraphQLPlaygroundConfig};
dataloader::DataLoader,
http::{playground_source, GraphQLPlaygroundConfig},
};
use async_graphql_poem::GraphQL; use async_graphql_poem::GraphQL;
use dotenv::dotenv; use dotenv::dotenv;
use lazy_static::lazy_static;
use poem::{get, handler, listener::TcpListener, web::Html, IntoResponse, Route, Server}; use poem::{get, handler, listener::TcpListener, web::Html, IntoResponse, Route, Server};
use sea_orm::Database; use sea_orm::Database;
use sea_orm_seaography_example::*; use seaography::{async_graphql, lazy_static};
use std::env; use std::env;
lazy_static! { 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")) }) ; }
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<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] #[handler]
async fn graphql_playground() -> impl IntoResponse { async fn graphql_playground() -> impl IntoResponse {
@ -39,19 +23,9 @@ async fn main() {
let database = Database::connect(&*DATABASE_URL) let database = Database::connect(&*DATABASE_URL)
.await .await
.expect("Fail to initialize database connection"); .expect("Fail to initialize database connection");
let orm_dataloader: DataLoader<OrmDataloader> = DataLoader::new( let schema =
OrmDataloader { sea_orm_seaography_example::query_root::schema(database, *DEPTH_LIMIT, *COMPLEXITY_LIMIT)
db: database.clone(), .unwrap();
},
tokio::spawn,
);
let schema = sea_orm_seaography_example::query_root::schema(
database,
orm_dataloader,
*DEPTH_LIMIT,
*COMPLEXITY_LIMIT,
)
.unwrap();
let app = Route::new().at( let app = Route::new().at(
&*ENDPOINT, &*ENDPOINT,
get(graphql_playground).post(GraphQL::new(schema)), get(graphql_playground).post(GraphQL::new(schema)),

View File

@ -1,28 +1,21 @@
use crate::{entities::*, OrmDataloader}; use crate::entities::*;
use async_graphql::{dataloader::DataLoader, dynamic::*}; use async_graphql::dynamic::*;
use sea_orm::DatabaseConnection; 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 () ; } lazy_static::lazy_static! { static ref CONTEXT : BuilderContext = BuilderContext :: default () ; }
pub fn schema( pub fn schema(
database: DatabaseConnection, database: DatabaseConnection,
orm_dataloader: DataLoader<OrmDataloader>,
depth: Option<usize>, depth: Option<usize>,
complexity: Option<usize>, complexity: Option<usize>,
) -> Result<Schema, SchemaError> { ) -> Result<Schema, SchemaError> {
let builder = Builder::new(&CONTEXT, database.clone()); let mut builder = Builder::new(&CONTEXT, database.clone());
let builder = crate::entities::register_entity_modules(builder); seaography::register_entities!(builder, [baker, bakery, cake, cake_baker,]);
let schema = builder.schema_builder(); builder
let schema = if let Some(depth) = depth { .set_depth_limit(depth)
schema.limit_depth(depth) .set_complexity_limit(complexity)
} else { .schema_builder()
schema .data(database)
}; .finish()
let schema = if let Some(complexity) = complexity {
schema.limit_complexity(complexity)
} else {
schema
};
schema.data(database).data(orm_dataloader).finish()
} }

View File

@ -1,5 +1,10 @@
# Bakery Schema # Bakery Schema
```sql
CREATE DATABASE bakery;
GRANT ALL PRIVILEGES ON bakery.* TO sea;
```
Assume the database is named `bakery`: Assume the database is named `bakery`:
```sh ```sh