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
```sh
cargo install seaography-cli@^1.0.0
cargo install seaography-cli@^1.1.3
```
## Generate GraphQL project

View File

@ -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 = "../../../" }

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::*;

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::*;

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::*;
@ -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<i32>,
pub bakery_id: i32,
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::*;

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;
@ -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,]);

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::bakery::Entity as Bakery;

View File

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

View File

@ -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<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"))
});
}
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,18 +23,8 @@ async fn main() {
let database = Database::connect(&*DATABASE_URL)
.await
.expect("Fail to initialize database connection");
let orm_dataloader: DataLoader<OrmDataloader> = DataLoader::new(
OrmDataloader {
db: database.clone(),
},
tokio::spawn,
);
let schema = sea_orm_seaography_example::query_root::schema(
database,
orm_dataloader,
*DEPTH_LIMIT,
*COMPLEXITY_LIMIT,
)
let schema =
sea_orm_seaography_example::query_root::schema(database, *DEPTH_LIMIT, *COMPLEXITY_LIMIT)
.unwrap();
let app = Route::new().at(
&*ENDPOINT,

View File

@ -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<OrmDataloader>,
depth: Option<usize>,
complexity: Option<usize>,
) -> Result<Schema, SchemaError> {
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()
}

View File

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