Skip to content

Commit

Permalink
Update seaography_example
Browse files Browse the repository at this point in the history
  • Loading branch information
tyt2y3 committed Jan 10, 2025
1 parent 216479c commit 1e496a2
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 77 deletions.
2 changes: 1 addition & 1 deletion examples/seaography_example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 6 additions & 12 deletions examples/seaography_example/graphql/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 = "../../../" }
2 changes: 1 addition & 1 deletion examples/seaography_example/graphql/src/entities/baker.rs
Original file line number Diff line number Diff line change
@@ -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::*;

Expand Down
2 changes: 1 addition & 1 deletion examples/seaography_example/graphql/src/entities/bakery.rs
Original file line number Diff line number Diff line change
@@ -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::*;

Expand Down
4 changes: 2 additions & 2 deletions examples/seaography_example/graphql/src/entities/cake.rs
Original file line number Diff line number Diff line change
@@ -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::*;

Expand All @@ -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,
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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::*;

Expand Down
4 changes: 2 additions & 2 deletions examples/seaography_example/graphql/src/entities/mod.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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,]);
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
6 changes: 0 additions & 6 deletions examples/seaography_example/graphql/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
use sea_orm::prelude::*;

pub mod entities;
pub mod query_root;

pub struct OrmDataloader {
pub db: DatabaseConnection,
}
38 changes: 6 additions & 32 deletions examples/seaography_example/graphql/src/main.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -39,19 +23,9 @@ 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,
)
.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)),
Expand Down
29 changes: 11 additions & 18 deletions examples/seaography_example/graphql/src/query_root.rs
Original file line number Diff line number Diff line change
@@ -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()
}
5 changes: 5 additions & 0 deletions examples/seaography_example/migration/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Bakery Schema

```sql
CREATE DATABASE bakery;
GRANT ALL PRIVILEGES ON bakery.* TO sea;
```

Assume the database is named `bakery`:

```sh
Expand Down

0 comments on commit 1e496a2

Please sign in to comment.