-
-
Notifications
You must be signed in to change notification settings - Fork 539
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Try constructing async-graphql root for "related entity" and "entity"…
… without relation
- Loading branch information
Showing
11 changed files
with
140 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,2 @@ | ||
[workspace] | ||
# A separate workspace | ||
|
||
[package] | ||
name = "sea-orm-issues-1599" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] | ||
anyhow = "1" | ||
serde = "1" | ||
tokio = { version = "1", features = ["rt", "rt-multi-thread", "macros"] } | ||
seaography = { path = "../../../seaography", optional = true } | ||
async-graphql = { version = "5", optional = true } | ||
|
||
[dependencies.sea-orm] | ||
path = "../../" | ||
default-features = false | ||
features = ["macros"] | ||
|
||
[features] | ||
default = ["seaography"] | ||
seaography = ["dep:seaography", "async-graphql", "sea-orm/seaography"] | ||
members = ["entity", "graphql"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
name = "entity" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[lib] | ||
name = "entity" | ||
path = "src/lib.rs" | ||
|
||
[dependencies] | ||
sea-orm = { path = "../../../" } | ||
seaography = { path = "../../../../seaography", optional = true } | ||
async-graphql = { version = "5", optional = true } | ||
|
||
[features] | ||
seaography = ["dep:seaography", "async-graphql", "sea-orm/seaography"] |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pub mod cake; | ||
pub mod cake_filling; | ||
pub mod filling; | ||
pub mod fruit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[package] | ||
name = "graphql" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] | ||
poem = { version = "1.3.55" } | ||
async-graphql-poem = { version = "5.0.6" } | ||
async-graphql = { version = "5.0.6", features = ["decimal", "chrono", "dataloader", "dynamic-schema"] } | ||
async-trait = { version = "0.1.64" } | ||
dotenv = "0.15.0" | ||
tokio = { version = "1.26.0", features = ["macros", "rt-multi-thread"] } | ||
tracing = { version = "0.1.37" } | ||
tracing-subscriber = { version = "0.3.16" } | ||
lazy_static = { version = "1.4.0" } | ||
|
||
sea-orm = { path = "../../../" } | ||
entity = { path = "../entity", features = ["seaography"] } | ||
seaography = { path = "../../../../seaography" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use async_graphql::{ | ||
dataloader::DataLoader, | ||
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::{prelude::*, Database}; | ||
use std::env; | ||
|
||
pub mod query_root; | ||
|
||
pub struct OrmDataloader { | ||
pub db: DatabaseConnection, | ||
} | ||
|
||
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")) | ||
}); | ||
} | ||
|
||
#[handler] | ||
async fn graphql_playground() -> impl IntoResponse { | ||
Html(playground_source(GraphQLPlaygroundConfig::new(&ENDPOINT))) | ||
} | ||
|
||
#[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 orm_dataloader: DataLoader<OrmDataloader> = DataLoader::new( | ||
OrmDataloader { | ||
db: database.clone(), | ||
}, | ||
tokio::spawn, | ||
); | ||
let schema = | ||
query_root::schema(database, orm_dataloader, *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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use crate::OrmDataloader; | ||
use async_graphql::{dataloader::DataLoader, dynamic::*}; | ||
use entity::*; | ||
use sea_orm::DatabaseConnection; | ||
use seaography::{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 mut builder = Builder::new(&CONTEXT); | ||
|
||
// Register entity including relations | ||
seaography::register_related_entities!(builder, [cake]); | ||
// Register entity only, no relations | ||
seaography::register_entities!(builder, [cake_filling, filling, fruit]); | ||
|
||
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() | ||
} |
This file was deleted.
Oops, something went wrong.