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

Fix enum variant names in query filters and outputs #165

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
14 changes: 12 additions & 2 deletions examples/postgres/src/query_root.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
use crate::entities::*;
use async_graphql::dynamic::*;
use sea_orm::DatabaseConnection;
use seaography::{Builder, BuilderContext};
use seaography::{Builder, BuilderContext, ActiveEnumConfig, heck::ToSnakeCase};

lazy_static::lazy_static! { static ref CONTEXT : BuilderContext = BuilderContext :: default () ; }
lazy_static::lazy_static! {
static ref CONTEXT: BuilderContext = BuilderContext {
active_enum: ActiveEnumConfig {
variant_name: Box::new(|_enum_name: &str, variant: &str| -> String {
variant.to_snake_case()
}),
..Default::default()
},
..Default::default()
};
}

pub fn schema(
database: DatabaseConnection,
Expand Down
12 changes: 6 additions & 6 deletions examples/postgres/tests/query_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ async fn enumeration_filter() {
r#"
{
film(
filters: { rating: { eq: NC17 } }
filters: { rating: { eq: nc_17 } }
pagination: { page: { page: 1, limit: 5 } }
) {
nodes {
Expand All @@ -861,23 +861,23 @@ async fn enumeration_filter() {
"nodes": [
{
"filmId": 27,
"rating": "NC17"
"rating": "nc_17"
},
{
"filmId": 29,
"rating": "NC17"
"rating": "nc_17"
},
{
"filmId": 31,
"rating": "NC17"
"rating": "nc_17"
},
{
"filmId": 34,
"rating": "NC17"
"rating": "nc_17"
},
{
"filmId": 38,
"rating": "NC17"
"rating": "nc_17"
}
]
}
Expand Down
10 changes: 8 additions & 2 deletions src/builder_context/filter_types_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,14 @@ impl FilterTypesMapHelper {
FilterType::Float => &self.context.filter_types.float_filter_info,
FilterType::Boolean => &self.context.filter_types.boolean_filter_info,
FilterType::Id => &self.context.filter_types.id_filter_info,
FilterType::Enumeration(_) => {
return prepare_enumeration_condition::<T>(filter, column, condition)
FilterType::Enumeration(name) => {
return prepare_enumeration_condition::<T>(
self.context,
filter,
column,
condition,
&name,
);
}
FilterType::Custom(_) => {
let entity_object_builder = EntityObjectBuilder {
Expand Down
16 changes: 10 additions & 6 deletions src/inputs/active_enum_filter_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ impl ActiveEnumFilterInputBuilder {

/// used to update the query condition with enumeration filters
pub fn prepare_enumeration_condition<T>(
context: &'static BuilderContext,
filter: &ObjectAccessor,
column: &T::Column,
condition: Condition,
type_name: &str,
) -> SeaResult<Condition>
where
T: EntityTrait,
Expand All @@ -87,12 +89,14 @@ where
};

let extract_variant = move |input: &str| -> String {
let variant = variants.iter().find(|variant| {
let variant = variant
.to_string()
.to_upper_camel_case()
.to_ascii_uppercase();
variant.eq(input)
let variant = variants.iter().find(|orm_variant| {
let orm_variant = orm_variant.to_string();

let builder = ActiveEnumBuilder { context };

let gql_variant = builder.variant_name(type_name, &orm_variant);

gql_variant.eq(input)
});
variant.unwrap().to_string()
};
Expand Down
44 changes: 29 additions & 15 deletions src/outputs/entity_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl std::default::Default for EntityObjectConfig {
}
}

use crate::{BuilderContext, GuardAction, TypesMapHelper};
use crate::{ActiveEnumBuilder, BuilderContext, GuardAction, TypesMapHelper};

/// This builder produces the GraphQL object of a SeaORM entity
pub struct EntityObjectBuilder {
Expand Down Expand Up @@ -105,6 +105,8 @@ impl EntityObjectBuilder {
};

T::Column::iter().fold(Object::new(object_name), |object, column: T::Column| {
let context = self.context;

let column_name = self.column_name::<T>(&column);

let column_def = column.def();
Expand All @@ -129,14 +131,12 @@ impl EntityObjectBuilder {
_ => false,
};

let guard = self
.context
let guard = context
.guards
.field_guards
.get(&format!("{}.{}", &object_name, &column_name));

let conversion_fn = self
.context
let conversion_fn = context
.types
.output_conversions
.get(&format!("{}.{}", entity_name, column_name));
Expand Down Expand Up @@ -179,12 +179,14 @@ impl EntityObjectBuilder {
});
}

FieldFuture::new(async move {
Ok(sea_query_value_to_graphql_value(
object.get(column),
is_enum,
))
})
let value = sea_query_value_to_graphql_value(
context,
object.get(column),
is_enum,
column_def.get_column_type(),
);

FieldFuture::new(async move { Ok(value) })
});

object.field(field)
Expand All @@ -193,8 +195,10 @@ impl EntityObjectBuilder {
}

fn sea_query_value_to_graphql_value(
context: &'static BuilderContext,
sea_query_value: sea_orm::sea_query::Value,
is_enum: bool,
column_type: &ColumnType,
) -> Option<Value> {
match sea_query_value {
sea_orm::Value::Bool(value) => value.map(Value::from),
Expand All @@ -208,9 +212,18 @@ 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| {
let builder = ActiveEnumBuilder { context };

let enum_name = match column_type {
ColumnType::Enum { name, .. } => name.to_string(),
_ => panic!("Expected enum column type"),
};

let gql_name = builder.variant_name(enum_name.as_str(), it.as_str());

Value::from(gql_name)
}),
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())),

Expand All @@ -222,7 +235,8 @@ fn sea_query_value_to_graphql_value(
Value::List(
it.into_iter()
.map(|item| {
sea_query_value_to_graphql_value(item, is_enum).unwrap_or(Value::Null)
sea_query_value_to_graphql_value(context, item, is_enum, column_type)
.unwrap_or(Value::Null)
})
.collect(),
)
Expand Down