Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SeaORM schema metadata #185

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ categories = ["database"]

[dependencies]
async-graphql = { version = "7.0", features = ["decimal", "chrono", "dataloader", "dynamic-schema"] }
sea-orm = { version = "~1.1.4", default-features = false, features = ["seaography"] }
sea-orm = { version = "~1.1.4", default-features = false, features = ["seaography", "with-json"] }
itertools = { version = "0.12.0" }
heck = { version = "0.4.1" }
thiserror = { version = "1.0.44" }
fnv = { version = "1.0.7" }
lazy_static = { version = "1.5" }
serde_json = { version = "1.0" }

[features]
default = ["field-camel-case"]
Expand All @@ -41,3 +42,6 @@ field-camel-case = []
# [patch.crates-io]
# sea-orm = { git = "https://github.com/SeaQL/sea-orm" }
# sea-orm-migration = { git = "https://github.com/SeaQL/sea-orm" }

[patch.crates-io]
sea-orm = { git = "https://github.com/SeaQL/sea-orm" }
3 changes: 3 additions & 0 deletions examples/sqlite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ serde_json = { version = "1.0.103" }

[workspace]
members = []

[patch.crates-io]
sea-orm = { git = "https://github.com/SeaQL/sea-orm" }
36 changes: 35 additions & 1 deletion src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use async_graphql::{
dataloader::DataLoader,
dynamic::{Enum, Field, FieldFuture, InputObject, Object, Schema, SchemaBuilder, TypeRef},
};
use sea_orm::{ActiveEnum, ActiveModelTrait, EntityTrait, IntoActiveModel};
use sea_orm::{ActiveEnum, ActiveModelTrait, ConnectionTrait, EntityTrait, IntoActiveModel};

use crate::{
ActiveEnumBuilder, ActiveEnumFilterInputBuilder, BuilderContext, ConnectionObjectBuilder,
Expand Down Expand Up @@ -37,6 +37,9 @@ pub struct Builder {
/// holds all entities mutations
pub mutations: Vec<Field>,

/// holds all entities metadata
pub metadata: std::collections::HashMap<String, serde_json::Value>,

/// holds a copy to the database connection
pub connection: sea_orm::DatabaseConnection,

Expand Down Expand Up @@ -70,6 +73,7 @@ impl Builder {
enumerations: Vec::new(),
queries: Vec::new(),
mutations: Vec::new(),
metadata: Default::default(),
connection,
context,
depth: None,
Expand Down Expand Up @@ -119,6 +123,10 @@ impl Builder {
};
let query = entity_query_field_builder.to_field::<T>();
self.queries.push(query);

let schema = sea_orm::Schema::new(self.connection.get_database_backend());
let metadata = schema.json_schema_from_entity(T::default());
self.metadata.insert(T::default().to_string(), metadata);
}

pub fn register_entity_mutations<T, A>(&mut self)
Expand Down Expand Up @@ -249,6 +257,32 @@ impl Builder {
.into_iter()
.fold(query, |query, field| query.field(field));

const TABLE_NAME: &str = "table_name";
let field = Field::new(
"_sea_orm_entity_metadata",
TypeRef::named("String"),
move |ctx| {
let metadata_hashmap = self.metadata.clone();
FieldFuture::new(async move {
let table_name = ctx
.args
.get(TABLE_NAME)
.expect("table_name is required")
.string()?;
if let Some(metadata) = metadata_hashmap.get(table_name) {
Ok(Some(async_graphql::Value::from_json(metadata.to_owned())?))
} else {
Ok(None)
}
})
},
)
.argument(async_graphql::dynamic::InputValue::new(
TABLE_NAME,
TypeRef::named_nn(TypeRef::STRING),
));
let query = query.field(field);

// register mutations
let mutation = self
.mutations
Expand Down
Loading