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

feat(json): Add feature to expose JSON as raw scalar #182

Open
wants to merge 1 commit 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
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