From 7f90d34a0145efd5b68a53f9a53bf870aa2fe7ef Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Thu, 7 Nov 2024 11:55:58 +0800 Subject: [PATCH 1/7] enum_type_name --- src/builder_context/types_map.rs | 155 +++++++++++++------------ src/enumerations/active_enum.rs | 8 +- src/inputs/active_enum_filter_input.rs | 7 +- src/inputs/entity_input.rs | 2 + src/outputs/entity_object.rs | 6 +- 5 files changed, 88 insertions(+), 90 deletions(-) diff --git a/src/builder_context/types_map.rs b/src/builder_context/types_map.rs index 4812473f..d3ee2be8 100644 --- a/src/builder_context/types_map.rs +++ b/src/builder_context/types_map.rs @@ -1,5 +1,6 @@ use std::{collections::BTreeMap, num::ParseIntError}; +use heck::ToUpperCamelCase; use async_graphql::dynamic::{TypeRef, ValueAccessor}; use sea_orm::{ColumnTrait, ColumnType, EntityTrait}; @@ -238,87 +239,91 @@ impl TypesMapHelper { &self, ty: &ColumnType, not_null: bool, + enum_type_name: Option, ) -> Option { let active_enum_builder = ActiveEnumBuilder { context: self.context, }; - match ty { - ColumnType::Char(_) | ColumnType::String(_) | ColumnType::Text => { - Some(TypeRef::named(TypeRef::STRING)) - } - ColumnType::TinyInteger - | ColumnType::SmallInteger - | ColumnType::Integer - | ColumnType::BigInteger - | ColumnType::TinyUnsigned - | ColumnType::SmallUnsigned - | ColumnType::Unsigned - | ColumnType::BigUnsigned => Some(TypeRef::named(TypeRef::INT)), - ColumnType::Float | ColumnType::Double => Some(TypeRef::named(TypeRef::FLOAT)), - ColumnType::Decimal(_) | ColumnType::Money(_) => Some(TypeRef::named(TypeRef::STRING)), - ColumnType::DateTime - | ColumnType::Timestamp - | ColumnType::TimestampWithTimeZone - | ColumnType::Time - | ColumnType::Date => Some(TypeRef::named(TypeRef::STRING)), - ColumnType::Year => Some(TypeRef::named(TypeRef::INT)), - ColumnType::Interval(_, _) => Some(TypeRef::named(TypeRef::STRING)), - ColumnType::Binary(_) - | ColumnType::VarBinary(_) - | ColumnType::Bit(_) - | ColumnType::VarBit(_) - | ColumnType::Blob => Some(TypeRef::named(TypeRef::STRING)), - ColumnType::Boolean => Some(TypeRef::named(TypeRef::BOOLEAN)), - // FIXME: support json type - ColumnType::Json | ColumnType::JsonBinary => None, - ColumnType::Uuid => Some(TypeRef::named(TypeRef::STRING)), - ColumnType::Enum { - name: enum_name, - variants: _, - } => Some(TypeRef::named( - active_enum_builder.type_name_from_iden(enum_name), - )), - ColumnType::Cidr | ColumnType::Inet | ColumnType::MacAddr => { - Some(TypeRef::named(TypeRef::STRING)) - } - #[cfg(not(feature = "with-postgres-array"))] - ColumnType::Array(_) => None, - #[cfg(feature = "with-postgres-array")] - ColumnType::Array(iden) => { - // FIXME: Propagating the not_null flag here is probably incorrect. The following - // types are all logically valid: - // - [T] - // - [T!] - // - [T]! - // - [T!]! - // - [[T]] - // - [[T!]] - // - [[T]!] - // - [[T!]!] - // - [[T!]!]! - // - [[T!]]! (etc, recursively) - // - // This is true for both GraphQL itself but also for the equivalent types in some - // backends, like Postgres. - // - // However, the not_null flag lives on the column definition in sea_query, not on - // the type itself. That means we lose the ability to represent nullability - // reliably on any inner type. We have three options: - // - pass down the flag (what we're doing here): - // pros: likely the most common intent from those who care about nullability - // cons: can be incorrect in both inserts and queries - // - always pass true: - // pros: none? maybe inserts are easier to reason about? - // cons: just as likely to be wrong as flag passing - // - always pass false: - // pros: always technically workable for queries (annoying for non-null data) - // conts: bad for inserts - let iden_type = self.sea_orm_column_type_to_graphql_type(iden.as_ref(), true); - iden_type.map(|it| TypeRef::List(Box::new(it))) + match enum_type_name { + Some(enum_type_name) => Some(TypeRef::named(format!("{}Enum", enum_type_name.to_upper_camel_case()))), + None => match ty { + ColumnType::Char(_) | ColumnType::String(_) | ColumnType::Text => { + Some(TypeRef::named(TypeRef::STRING)) + } + ColumnType::TinyInteger + | ColumnType::SmallInteger + | ColumnType::Integer + | ColumnType::BigInteger + | ColumnType::TinyUnsigned + | ColumnType::SmallUnsigned + | ColumnType::Unsigned + | ColumnType::BigUnsigned => Some(TypeRef::named(TypeRef::INT)), + ColumnType::Float | ColumnType::Double => Some(TypeRef::named(TypeRef::FLOAT)), + ColumnType::Decimal(_) | ColumnType::Money(_) => Some(TypeRef::named(TypeRef::STRING)), + ColumnType::DateTime + | ColumnType::Timestamp + | ColumnType::TimestampWithTimeZone + | ColumnType::Time + | ColumnType::Date => Some(TypeRef::named(TypeRef::STRING)), + ColumnType::Year => Some(TypeRef::named(TypeRef::INT)), + ColumnType::Interval(_, _) => Some(TypeRef::named(TypeRef::STRING)), + ColumnType::Binary(_) + | ColumnType::VarBinary(_) + | ColumnType::Bit(_) + | ColumnType::VarBit(_) + | ColumnType::Blob => Some(TypeRef::named(TypeRef::STRING)), + ColumnType::Boolean => Some(TypeRef::named(TypeRef::BOOLEAN)), + // FIXME: support json type + ColumnType::Json | ColumnType::JsonBinary => None, + ColumnType::Uuid => Some(TypeRef::named(TypeRef::STRING)), + ColumnType::Enum { + name: enum_name, + variants: _, + } => Some(TypeRef::named( + active_enum_builder.type_name_from_iden(enum_name), + )), + ColumnType::Cidr | ColumnType::Inet | ColumnType::MacAddr => { + Some(TypeRef::named(TypeRef::STRING)) + } + #[cfg(not(feature = "with-postgres-array"))] + ColumnType::Array(_) => None, + #[cfg(feature = "with-postgres-array")] + ColumnType::Array(iden) => { + // FIXME: Propagating the not_null flag here is probably incorrect. The following + // types are all logically valid: + // - [T] + // - [T!] + // - [T]! + // - [T!]! + // - [[T]] + // - [[T!]] + // - [[T]!] + // - [[T!]!] + // - [[T!]!]! + // - [[T!]]! (etc, recursively) + // + // This is true for both GraphQL itself but also for the equivalent types in some + // backends, like Postgres. + // + // However, the not_null flag lives on the column definition in sea_query, not on + // the type itself. That means we lose the ability to represent nullability + // reliably on any inner type. We have three options: + // - pass down the flag (what we're doing here): + // pros: likely the most common intent from those who care about nullability + // cons: can be incorrect in both inserts and queries + // - always pass true: + // pros: none? maybe inserts are easier to reason about? + // cons: just as likely to be wrong as flag passing + // - always pass false: + // pros: always technically workable for queries (annoying for non-null data) + // conts: bad for inserts + let iden_type = self.sea_orm_column_type_to_graphql_type(iden.as_ref(), true, enum_type_name); + iden_type.map(|it| TypeRef::List(Box::new(it))) + } + ColumnType::Custom(_iden) => Some(TypeRef::named(TypeRef::STRING)), + _ => None, } - ColumnType::Custom(_iden) => Some(TypeRef::named(TypeRef::STRING)), - _ => None, } .map(|ty| { if not_null { diff --git a/src/enumerations/active_enum.rs b/src/enumerations/active_enum.rs index e156afa4..d4c47f88 100644 --- a/src/enumerations/active_enum.rs +++ b/src/enumerations/active_enum.rs @@ -1,5 +1,5 @@ use async_graphql::dynamic::Enum; -use heck::{ToSnakeCase, ToUpperCamelCase}; +use heck::ToUpperCamelCase; use sea_orm::{ActiveEnum, DynIden, Value}; use crate::BuilderContext; @@ -19,11 +19,7 @@ impl std::default::Default for ActiveEnumConfig { format!("{}Enum", name.to_upper_camel_case()) }), variant_name: Box::new(|_enum_name: &str, variant: &str| -> String { - if cfg!(feature = "field-snake-case") { - variant.to_snake_case() - } else { - variant.to_upper_camel_case().to_ascii_uppercase() - } + variant.trim_matches('\'').to_string() }), } } diff --git a/src/inputs/active_enum_filter_input.rs b/src/inputs/active_enum_filter_input.rs index c8060889..4bd7cf52 100644 --- a/src/inputs/active_enum_filter_input.rs +++ b/src/inputs/active_enum_filter_input.rs @@ -1,7 +1,7 @@ use std::collections::BTreeSet; use async_graphql::dynamic::ObjectAccessor; -use heck::{ToSnakeCase, ToUpperCamelCase}; +use heck::ToUpperCamelCase; use sea_orm::{ActiveEnum, ColumnTrait, ColumnType, Condition, DynIden, EntityTrait}; use crate::{ActiveEnumBuilder, BuilderContext, FilterInfo, FilterOperation, SeaResult}; @@ -89,11 +89,6 @@ where let extract_variant = move |input: &str| -> String { let variant = variants.iter().find(|variant| { let variant = variant.to_string(); - let variant = if cfg!(feature = "field-snake-case") { - variant.to_snake_case() - } else { - variant.to_upper_camel_case().to_ascii_uppercase() - }; variant.eq(input) }); variant.unwrap().to_string() diff --git a/src/inputs/entity_input.rs b/src/inputs/entity_input.rs index 123dec7a..2c54af4e 100644 --- a/src/inputs/entity_input.rs +++ b/src/inputs/entity_input.rs @@ -95,6 +95,7 @@ impl EntityInputBuilder { } let column_def = column.def(); + let enum_type_name = column.enum_type_name(); let auto_increment = match ::from_column(column) { Some(_) => T::PrimaryKey::auto_increment(), @@ -107,6 +108,7 @@ impl EntityInputBuilder { let graphql_type = match types_map_helper.sea_orm_column_type_to_graphql_type( column_def.get_column_type(), is_insert_not_nullable, + enum_type_name, ) { Some(type_name) => type_name, None => return object, diff --git a/src/outputs/entity_object.rs b/src/outputs/entity_object.rs index f4c83f86..eb9291ee 100644 --- a/src/outputs/entity_object.rs +++ b/src/outputs/entity_object.rs @@ -112,10 +112,12 @@ impl EntityObjectBuilder { let column_name = self.column_name::(&column); let column_def = column.def(); + let enum_type_name = column.enum_type_name(); let graphql_type = match types_map_helper.sea_orm_column_type_to_graphql_type( column_def.get_column_type(), !column_def.is_null(), + enum_type_name, ) { Some(type_name) => type_name, None => return object, @@ -212,9 +214,7 @@ fn sea_query_value_to_graphql_value( sea_orm::Value::BigUnsigned(value) => value.map(Value::from), sea_orm::Value::Float(value) => value.map(Value::from), sea_orm::Value::Double(value) => value.map(Value::from), - sea_orm::Value::String(value) if is_enum => { - value.map(|it| Value::from(it.as_str().to_upper_camel_case().to_ascii_uppercase())) - } + sea_orm::Value::String(value) if is_enum => value.map(|it| Value::from(it.as_str())), sea_orm::Value::String(value) => value.map(|it| Value::from(it.as_str())), sea_orm::Value::Char(value) => value.map(|it| Value::from(it.to_string())), From cf135e2319cdd5f74ce99fa962e9e289a3d51a04 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Fri, 8 Nov 2024 15:35:59 +0800 Subject: [PATCH 2/7] enum_type_name returns Option<&'static str> --- src/builder_context/types_map.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/builder_context/types_map.rs b/src/builder_context/types_map.rs index d3ee2be8..e7201d30 100644 --- a/src/builder_context/types_map.rs +++ b/src/builder_context/types_map.rs @@ -239,7 +239,7 @@ impl TypesMapHelper { &self, ty: &ColumnType, not_null: bool, - enum_type_name: Option, + enum_type_name: Option<&'static str>, ) -> Option { let active_enum_builder = ActiveEnumBuilder { context: self.context, From 9aa49eccf9fe9bfec860186945554b6303dc9623 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Fri, 8 Nov 2024 15:46:50 +0800 Subject: [PATCH 3/7] Added `register_active_enums!()` macro --- src/builder.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/builder.rs b/src/builder.rs index 592f5041..031c5f1f 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -376,3 +376,13 @@ macro_rules! register_entity_modules { } }; } + +#[macro_export] +macro_rules! register_active_enums { + ([$($enum_paths:path),+ $(,)?]) => { + pub fn register_active_enums(mut builder: seaography::builder::Builder) -> seaography::builder::Builder { + $(builder.register_enumeration::<$enum_paths>();)* + builder + } + }; +} From d22c1b603202855f802dff55233e02dbbab7d20d Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Tue, 12 Nov 2024 17:31:48 +0800 Subject: [PATCH 4/7] dep --- Cargo.toml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 0861f8f2..9cf85ad4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,3 +36,7 @@ with-postgres-array = ["sea-orm/postgres-array"] # with-mac_address = ["sea-orm/with-mac_address"] field-snake-case = [] field-camel-case = [] + +[patch.crates-io] +sea-orm = { git = "https://github.com/SeaQL/sea-orm", branch = "pro" } +sea-orm-migration = { git = "https://github.com/SeaQL/sea-orm", branch = "pro" } From e5132ef8aaeb080e9e2f015a9df14d411275b979 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Mon, 2 Dec 2024 14:58:49 +0800 Subject: [PATCH 5/7] fmt --- src/builder_context/types_map.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/builder_context/types_map.rs b/src/builder_context/types_map.rs index e7201d30..f2f00194 100644 --- a/src/builder_context/types_map.rs +++ b/src/builder_context/types_map.rs @@ -1,7 +1,7 @@ use std::{collections::BTreeMap, num::ParseIntError}; -use heck::ToUpperCamelCase; use async_graphql::dynamic::{TypeRef, ValueAccessor}; +use heck::ToUpperCamelCase; use sea_orm::{ColumnTrait, ColumnType, EntityTrait}; use crate::{ActiveEnumBuilder, BuilderContext, EntityObjectBuilder, SeaResult}; @@ -246,7 +246,10 @@ impl TypesMapHelper { }; match enum_type_name { - Some(enum_type_name) => Some(TypeRef::named(format!("{}Enum", enum_type_name.to_upper_camel_case()))), + Some(enum_type_name) => Some(TypeRef::named(format!( + "{}Enum", + enum_type_name.to_upper_camel_case() + ))), None => match ty { ColumnType::Char(_) | ColumnType::String(_) | ColumnType::Text => { Some(TypeRef::named(TypeRef::STRING)) @@ -260,7 +263,9 @@ impl TypesMapHelper { | ColumnType::Unsigned | ColumnType::BigUnsigned => Some(TypeRef::named(TypeRef::INT)), ColumnType::Float | ColumnType::Double => Some(TypeRef::named(TypeRef::FLOAT)), - ColumnType::Decimal(_) | ColumnType::Money(_) => Some(TypeRef::named(TypeRef::STRING)), + ColumnType::Decimal(_) | ColumnType::Money(_) => { + Some(TypeRef::named(TypeRef::STRING)) + } ColumnType::DateTime | ColumnType::Timestamp | ColumnType::TimestampWithTimeZone @@ -318,12 +323,16 @@ impl TypesMapHelper { // - always pass false: // pros: always technically workable for queries (annoying for non-null data) // conts: bad for inserts - let iden_type = self.sea_orm_column_type_to_graphql_type(iden.as_ref(), true, enum_type_name); + let iden_type = self.sea_orm_column_type_to_graphql_type( + iden.as_ref(), + true, + enum_type_name, + ); iden_type.map(|it| TypeRef::List(Box::new(it))) } ColumnType::Custom(_iden) => Some(TypeRef::named(TypeRef::STRING)), _ => None, - } + }, } .map(|ty| { if not_null { From e4f2d1d31e7580ebd62929a1b535854a73f22bea Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Mon, 2 Dec 2024 15:05:40 +0800 Subject: [PATCH 6/7] bump sea-orm --- Cargo.toml | 8 ++++---- examples/mysql/Cargo.toml | 2 +- examples/postgres/Cargo.toml | 2 +- examples/sqlite/Cargo.toml | 2 +- generator/src/templates/actix_cargo.toml | 2 +- generator/src/templates/axum_cargo.toml | 2 +- generator/src/templates/poem_cargo.toml | 2 +- 7 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9cf85ad4..ba7fff15 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ categories = ["database"] [dependencies] async-graphql = { version = "7.0", features = ["decimal", "chrono", "dataloader", "dynamic-schema"] } -sea-orm = { version = "~1.1.0", default-features = false, features = ["seaography"] } +sea-orm = { version = "~1.1.2", default-features = false, features = ["seaography"] } itertools = { version = "0.12.0" } heck = { version = "0.4.1" } thiserror = { version = "1.0.44" } @@ -37,6 +37,6 @@ with-postgres-array = ["sea-orm/postgres-array"] field-snake-case = [] field-camel-case = [] -[patch.crates-io] -sea-orm = { git = "https://github.com/SeaQL/sea-orm", branch = "pro" } -sea-orm-migration = { git = "https://github.com/SeaQL/sea-orm", branch = "pro" } +# [patch.crates-io] +# sea-orm = { git = "https://github.com/SeaQL/sea-orm" } +# sea-orm-migration = { git = "https://github.com/SeaQL/sea-orm" } diff --git a/examples/mysql/Cargo.toml b/examples/mysql/Cargo.toml index 1b423ec8..7a4f05b1 100644 --- a/examples/mysql/Cargo.toml +++ b/examples/mysql/Cargo.toml @@ -8,7 +8,7 @@ axum = { version = "0.7" } async-graphql-axum = { version = "7.0" } async-graphql = { version = "7.0", features = ["decimal", "chrono", "dataloader", "dynamic-schema"] } dotenv = "0.15.0" -sea-orm = { version = "~1.1.0", features = ["sqlx-mysql", "runtime-async-std-native-tls", "seaography"] } +sea-orm = { version = "~1.1.2", 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" } diff --git a/examples/postgres/Cargo.toml b/examples/postgres/Cargo.toml index c6539f5e..96ff48ab 100644 --- a/examples/postgres/Cargo.toml +++ b/examples/postgres/Cargo.toml @@ -8,7 +8,7 @@ poem = { version = "3.0" } async-graphql-poem = { version = "7.0" } async-graphql = { version = "7.0", features = ["decimal", "chrono", "dataloader", "dynamic-schema"] } dotenv = "0.15.0" -sea-orm = { version = "~1.1.0", features = ["sqlx-postgres", "runtime-async-std-native-tls", "seaography"] } +sea-orm = { version = "~1.1.2", features = ["sqlx-postgres", "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" } diff --git a/examples/sqlite/Cargo.toml b/examples/sqlite/Cargo.toml index 39338e1c..ae26fcb1 100644 --- a/examples/sqlite/Cargo.toml +++ b/examples/sqlite/Cargo.toml @@ -8,7 +8,7 @@ actix-web = { version = "4.5", default-features = false, features = ["macros"] } async-graphql-actix-web = { version = "7.0" } async-graphql = { version = "7.0", features = ["decimal", "chrono", "dataloader", "dynamic-schema"] } dotenv = "0.15.0" -sea-orm = { version = "~1.1.0", features = ["sqlx-sqlite", "runtime-async-std-rustls", "seaography"] } +sea-orm = { version = "~1.1.2", features = ["sqlx-sqlite", "runtime-async-std-rustls", "seaography"] } tokio = { version = "1.29.1", features = ["macros", "rt-multi-thread"] } tracing = { version = "0.1.37" } tracing-subscriber = { version = "0.3.17" } diff --git a/generator/src/templates/actix_cargo.toml b/generator/src/templates/actix_cargo.toml index c0fc0236..0a9a94c8 100644 --- a/generator/src/templates/actix_cargo.toml +++ b/generator/src/templates/actix_cargo.toml @@ -8,7 +8,7 @@ actix-web = { version = "4.5", default-features = false, features = ["macros"] } async-graphql-actix-web = { version = "7.0" } async-graphql = { version = "7.0", features = ["decimal", "chrono", "dataloader", "dynamic-schema"] } dotenv = "0.15.0" -sea-orm = { version = "~1.1.0", features = ["", "runtime-async-std-native-tls", "seaography"] } +sea-orm = { version = "~1.1.2", features = ["", "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" } diff --git a/generator/src/templates/axum_cargo.toml b/generator/src/templates/axum_cargo.toml index 37f2d00e..f2378719 100644 --- a/generator/src/templates/axum_cargo.toml +++ b/generator/src/templates/axum_cargo.toml @@ -8,7 +8,7 @@ axum = { version = "0.7" } async-graphql-axum = { version = "7.0" } async-graphql = { version = "7.0", features = ["decimal", "chrono", "dataloader", "dynamic-schema"] } dotenv = "0.15.0" -sea-orm = { version = "~1.1.0", features = ["", "runtime-async-std-native-tls", "seaography"] } +sea-orm = { version = "~1.1.2", features = ["", "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" } diff --git a/generator/src/templates/poem_cargo.toml b/generator/src/templates/poem_cargo.toml index 3313908c..16df43af 100644 --- a/generator/src/templates/poem_cargo.toml +++ b/generator/src/templates/poem_cargo.toml @@ -8,7 +8,7 @@ poem = { version = "3.0" } async-graphql-poem = { version = "7.0" } async-graphql = { version = "7.0", features = ["decimal", "chrono", "dataloader", "dynamic-schema"] } dotenv = "0.15.0" -sea-orm = { version = "~1.1.0", features = ["", "runtime-async-std-native-tls", "seaography"] } +sea-orm = { version = "~1.1.2", features = ["", "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" } From 713479da9ff5088ebd6bcf95e435a5ab230c58f8 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Mon, 2 Dec 2024 15:52:07 +0800 Subject: [PATCH 7/7] Fix --- src/enumerations/active_enum.rs | 9 ++++++++- src/inputs/active_enum_filter_input.rs | 6 ++++-- src/outputs/entity_object.rs | 6 ++++-- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/enumerations/active_enum.rs b/src/enumerations/active_enum.rs index d4c47f88..8956478a 100644 --- a/src/enumerations/active_enum.rs +++ b/src/enumerations/active_enum.rs @@ -19,7 +19,7 @@ impl std::default::Default for ActiveEnumConfig { format!("{}Enum", name.to_upper_camel_case()) }), variant_name: Box::new(|_enum_name: &str, variant: &str| -> String { - variant.trim_matches('\'').to_string() + format_variant(variant) }), } } @@ -61,3 +61,10 @@ impl ActiveEnumBuilder { }) } } + +pub(crate) fn format_variant(variant: &str) -> String { + variant + .chars() + .filter(|&c| c.is_ascii_alphanumeric() || c == '_') + .collect() +} diff --git a/src/inputs/active_enum_filter_input.rs b/src/inputs/active_enum_filter_input.rs index 4bd7cf52..58fb1fa8 100644 --- a/src/inputs/active_enum_filter_input.rs +++ b/src/inputs/active_enum_filter_input.rs @@ -4,7 +4,9 @@ use async_graphql::dynamic::ObjectAccessor; use heck::ToUpperCamelCase; use sea_orm::{ActiveEnum, ColumnTrait, ColumnType, Condition, DynIden, EntityTrait}; -use crate::{ActiveEnumBuilder, BuilderContext, FilterInfo, FilterOperation, SeaResult}; +use crate::{ + format_variant, ActiveEnumBuilder, BuilderContext, FilterInfo, FilterOperation, SeaResult, +}; /// The configuration structure for ActiveEnumFilterInputConfig pub struct ActiveEnumFilterInputConfig { @@ -88,7 +90,7 @@ where let extract_variant = move |input: &str| -> String { let variant = variants.iter().find(|variant| { - let variant = variant.to_string(); + let variant = format_variant(&variant.to_string()); variant.eq(input) }); variant.unwrap().to_string() diff --git a/src/outputs/entity_object.rs b/src/outputs/entity_object.rs index eb9291ee..18db75f4 100644 --- a/src/outputs/entity_object.rs +++ b/src/outputs/entity_object.rs @@ -31,7 +31,7 @@ impl std::default::Default for EntityObjectConfig { } } -use crate::{BuilderContext, GuardAction, TypesMapHelper}; +use crate::{format_variant, BuilderContext, GuardAction, TypesMapHelper}; /// This builder produces the GraphQL object of a SeaORM entity pub struct EntityObjectBuilder { @@ -214,7 +214,9 @@ fn sea_query_value_to_graphql_value( sea_orm::Value::BigUnsigned(value) => value.map(Value::from), sea_orm::Value::Float(value) => value.map(Value::from), sea_orm::Value::Double(value) => value.map(Value::from), - sea_orm::Value::String(value) if is_enum => value.map(|it| Value::from(it.as_str())), + sea_orm::Value::String(value) if is_enum => { + value.map(|it| Value::from(format_variant(it.as_str()))) + } sea_orm::Value::String(value) => value.map(|it| Value::from(it.as_str())), sea_orm::Value::Char(value) => value.map(|it| Value::from(it.to_string())),