Skip to content

Commit

Permalink
feat(json): Add feature to expose JSON as raw scalar
Browse files Browse the repository at this point in the history
Signed-off-by: MATILLAT Quentin <[email protected]>
  • Loading branch information
tinou98 committed Dec 14, 2024
1 parent 66053f7 commit 28a6435
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ fnv = { version = "1.0.7" }
[features]
default = ["field-camel-case"]
with-json = ["sea-orm/with-json"]
with-json-as-scalar = ["with-json"]
with-chrono = ["sea-orm/with-chrono", "async-graphql/chrono"]
with-time = ["sea-orm/with-time", "async-graphql/time"]
with-uuid = ["sea-orm/with-uuid"]
Expand Down
9 changes: 9 additions & 0 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,15 @@ impl Builder {
.into_iter()
.fold(schema, |schema, cur| schema.register(cur));

#[cfg(feature = "with-json-as-scalar")]
let schema = schema.register(
async_graphql::dynamic::Scalar::new(&self.context.types.json_name)
.description("The `JSON` scalar type represents raw JSON values")
.specified_by_url(
"http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf",
),
);

schema
.register(
OrderByEnumBuilder {
Expand Down
21 changes: 13 additions & 8 deletions src/builder_context/types_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ pub struct TypesMapConfig {
pub time_library: TimeLibrary,
/// used to configure default decimal library
pub decimal_library: DecimalLibrary,

#[cfg(feature = "with-json-as-scalar")]
/// used to expose Json as a Scalar
pub json_name: String,
}

impl std::default::Default for TypesMapConfig {
Expand All @@ -45,6 +49,9 @@ impl std::default::Default for TypesMapConfig {
decimal_library: DecimalLibrary::Decimal,
#[cfg(all(not(feature = "with-decimal"), feature = "with-bigdecimal"))]
decimal_library: DecimalLibrary::BigDecimal,

#[cfg(feature = "with-json-as-scalar")]
json_name: "Json".to_owned(),
}
}
}
Expand Down Expand Up @@ -159,14 +166,9 @@ impl TypesMapHelper {
ColumnType::Boolean => ConvertedType::Bool,

#[cfg(not(feature = "with-json"))]
ColumnType::Json => ConvertedType::String,
ColumnType::Json | ColumnType::JsonBinary => ConvertedType::String,
#[cfg(feature = "with-json")]
ColumnType::Json => ConvertedType::Json,

// FIXME: how should we map them JsonBinary type ?
// #[cfg(feature = "with-json")]
// ColumnType::JsonBinary => ConvertedType::Json,
ColumnType::JsonBinary => ConvertedType::String,
ColumnType::Json | ColumnType::JsonBinary => ConvertedType::Json,

#[cfg(not(feature = "with-uuid"))]
ColumnType::Uuid => ConvertedType::String,
Expand Down Expand Up @@ -280,7 +282,10 @@ impl TypesMapHelper {
| ColumnType::Blob => Some(TypeRef::named(TypeRef::STRING)),
ColumnType::Boolean => Some(TypeRef::named(TypeRef::BOOLEAN)),
// FIXME: support json type
#[cfg(not(feature = "with-json-as-scalar"))]
ColumnType::Json | ColumnType::JsonBinary => None,
#[cfg(feature = "with-json-as-scalar")]
ColumnType::Json | ColumnType::JsonBinary => Some(TypeRef::named(&self.context.types.json_name)),
ColumnType::Uuid => Some(TypeRef::named(TypeRef::STRING)),
ColumnType::Enum {
name: enum_name,
Expand Down Expand Up @@ -850,4 +855,4 @@ pub fn decode_hex(s: &str) -> Result<Vec<u8>, ParseIntError> {
.step_by(2)
.map(|i| u8::from_str_radix(&s[i..i + 2], 16))
.collect()
}
}
8 changes: 7 additions & 1 deletion src/outputs/entity_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,13 @@ fn sea_query_value_to_graphql_value(

#[cfg(feature = "with-json")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-json")))]
sea_orm::sea_query::Value::Json(value) => value.map(|it| Value::from(it.to_string())),
sea_orm::sea_query::Value::Json(value) => value.map(|it| {
if cfg!(feature = "with-json-as-scalar") {
Value::from_json((*it).clone()).expect("Unable to serialize")
} else {
Value::from(it.to_string())
}
}),

#[cfg(feature = "with-chrono")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-chrono")))]
Expand Down

0 comments on commit 28a6435

Please sign in to comment.