Skip to content

Commit

Permalink
Try constructing async-graphql root for "related entity" and "entity"…
Browse files Browse the repository at this point in the history
… without relation
  • Loading branch information
billy1624 committed May 17, 2023
1 parent 0833478 commit c84294e
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 34 deletions.
24 changes: 1 addition & 23 deletions issues/1599/Cargo.toml
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"]
17 changes: 17 additions & 0 deletions issues/1599/entity/Cargo.toml
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.
4 changes: 4 additions & 0 deletions issues/1599/entity/src/lib.rs
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;
20 changes: 20 additions & 0 deletions issues/1599/graphql/Cargo.toml
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" }
64 changes: 64 additions & 0 deletions issues/1599/graphql/src/main.rs
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");
}
34 changes: 34 additions & 0 deletions issues/1599/graphql/src/query_root.rs
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()
}
11 changes: 0 additions & 11 deletions issues/1599/src/main.rs

This file was deleted.

0 comments on commit c84294e

Please sign in to comment.