Skip to content

Commit

Permalink
Handle Null GraphQL values
Browse files Browse the repository at this point in the history
  • Loading branch information
billy1624 committed Oct 22, 2024
1 parent 51954fc commit 0037a87
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/builder_context/types_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,52 +483,68 @@ pub fn converted_value_to_sea_orm_value(
entity_name: &str,
column_name: &str,
) -> SeaResult<sea_orm::Value> {
let is_null = value.is_null();
Ok(match column_type {
ConvertedType::Bool if is_null => sea_orm::Value::Bool(None),
ConvertedType::Bool => value.boolean().map(|v| v.into())?,
ConvertedType::TinyInteger if is_null => sea_orm::Value::TinyInt(None),
ConvertedType::TinyInteger => {
let value: i8 = value.i64()?.try_into()?;
sea_orm::Value::TinyInt(Some(value))
}
ConvertedType::SmallInteger if is_null => sea_orm::Value::SmallInt(None),
ConvertedType::SmallInteger => {
let value: i16 = value.i64()?.try_into()?;
sea_orm::Value::SmallInt(Some(value))
}
ConvertedType::Integer if is_null => sea_orm::Value::Int(None),
ConvertedType::Integer => {
let value: i32 = value.i64()?.try_into()?;
sea_orm::Value::Int(Some(value))
}
ConvertedType::BigInteger if is_null => sea_orm::Value::BigInt(None),
ConvertedType::BigInteger => {
let value = value.i64()?;
sea_orm::Value::BigInt(Some(value))
}
ConvertedType::TinyUnsigned if is_null => sea_orm::Value::TinyUnsigned(None),
ConvertedType::TinyUnsigned => {
let value: u8 = value.u64()?.try_into()?;
sea_orm::Value::TinyUnsigned(Some(value))
}
ConvertedType::SmallUnsigned if is_null => sea_orm::Value::SmallUnsigned(None),
ConvertedType::SmallUnsigned => {
let value: u16 = value.u64()?.try_into()?;
sea_orm::Value::SmallUnsigned(Some(value))
}
ConvertedType::Unsigned if is_null => sea_orm::Value::Unsigned(None),
ConvertedType::Unsigned => {
let value: u32 = value.u64()?.try_into()?;
sea_orm::Value::Unsigned(Some(value))
}
ConvertedType::BigUnsigned if is_null => sea_orm::Value::BigUnsigned(None),
ConvertedType::BigUnsigned => {
let value = value.u64()?;
sea_orm::Value::BigUnsigned(Some(value))
}
ConvertedType::Float if is_null => sea_orm::Value::Float(None),
ConvertedType::Float => {
let value = value.f32()?;
sea_orm::Value::Float(Some(value))
}
ConvertedType::Double if is_null => sea_orm::Value::Double(None),
ConvertedType::Double => {
let value = value.f64()?;
sea_orm::Value::Double(Some(value))
}
ConvertedType::String | ConvertedType::Enum(_) | ConvertedType::Custom(_) if is_null => {
sea_orm::Value::String(None)
}
ConvertedType::String | ConvertedType::Enum(_) | ConvertedType::Custom(_) => {
let value = value.string()?;
sea_orm::Value::String(Some(Box::new(value.to_string())))
}
ConvertedType::Char if is_null => sea_orm::Value::Char(None),
ConvertedType::Char => {
let value = value.string()?;
let value: char = match value.chars().next() {
Expand All @@ -537,11 +553,14 @@ pub fn converted_value_to_sea_orm_value(
};
sea_orm::Value::Char(Some(value))
}
ConvertedType::Bytes if is_null => sea_orm::Value::Bytes(None),
ConvertedType::Bytes => {
let value = decode_hex(value.string()?)?;
sea_orm::Value::Bytes(Some(Box::new(value)))
}
#[cfg(feature = "with-json")]
ConvertedType::Json if is_null => sea_orm::Value::Json(None),
#[cfg(feature = "with-json")]
ConvertedType::Json => {
use std::str::FromStr;

Expand All @@ -555,6 +574,8 @@ pub fn converted_value_to_sea_orm_value(
sea_orm::Value::Json(Some(Box::new(value)))
}
#[cfg(feature = "with-chrono")]
ConvertedType::ChronoDate if is_null => sea_orm::Value::ChronoDate(None),
#[cfg(feature = "with-chrono")]
ConvertedType::ChronoDate => {
let value =
sea_orm::entity::prelude::ChronoDate::parse_from_str(value.string()?, "%Y-%m-%d")
Expand All @@ -568,6 +589,8 @@ pub fn converted_value_to_sea_orm_value(
sea_orm::Value::ChronoDate(Some(Box::new(value)))
}
#[cfg(feature = "with-chrono")]
ConvertedType::ChronoTime if is_null => sea_orm::Value::ChronoTime(None),
#[cfg(feature = "with-chrono")]
ConvertedType::ChronoTime => {
let value =
sea_orm::entity::prelude::ChronoTime::parse_from_str(value.string()?, "%H:%M:%S")
Expand All @@ -581,6 +604,8 @@ pub fn converted_value_to_sea_orm_value(
sea_orm::Value::ChronoTime(Some(Box::new(value)))
}
#[cfg(feature = "with-chrono")]
ConvertedType::ChronoDateTime if is_null => sea_orm::Value::ChronoDateTime(None),
#[cfg(feature = "with-chrono")]
ConvertedType::ChronoDateTime => {
let value = sea_orm::entity::prelude::ChronoDateTime::parse_from_str(
value.string()?,
Expand All @@ -596,6 +621,8 @@ pub fn converted_value_to_sea_orm_value(
sea_orm::Value::ChronoDateTime(Some(Box::new(value)))
}
#[cfg(feature = "with-chrono")]
ConvertedType::ChronoDateTimeUtc if is_null => sea_orm::Value::ChronoDateTimeUtc(None),
#[cfg(feature = "with-chrono")]
ConvertedType::ChronoDateTimeUtc => {
use std::str::FromStr;

Expand All @@ -610,6 +637,8 @@ pub fn converted_value_to_sea_orm_value(
sea_orm::Value::ChronoDateTimeUtc(Some(Box::new(value)))
}
#[cfg(feature = "with-chrono")]
ConvertedType::ChronoDateTimeLocal if is_null => sea_orm::Value::ChronoDateTimeLocal(None),
#[cfg(feature = "with-chrono")]
ConvertedType::ChronoDateTimeLocal => {
use std::str::FromStr;

Expand All @@ -624,6 +653,10 @@ pub fn converted_value_to_sea_orm_value(
sea_orm::Value::ChronoDateTimeLocal(Some(Box::new(value)))
}
#[cfg(feature = "with-chrono")]
ConvertedType::ChronoDateTimeWithTimeZone if is_null => {
sea_orm::Value::ChronoDateTimeWithTimeZone(None)
}
#[cfg(feature = "with-chrono")]
ConvertedType::ChronoDateTimeWithTimeZone => {
let value = sea_orm::entity::prelude::ChronoDateTimeWithTimeZone::parse_from_str(
value.string()?,
Expand All @@ -642,6 +675,8 @@ pub fn converted_value_to_sea_orm_value(
sea_orm::Value::ChronoDateTimeWithTimeZone(Some(Box::new(value)))
}
#[cfg(feature = "with-time")]
ConvertedType::TimeDate if is_null => sea_orm::Value::TimeDate(None),
#[cfg(feature = "with-time")]
ConvertedType::TimeDate => {
use std::str::FromStr;

Expand All @@ -659,6 +694,8 @@ pub fn converted_value_to_sea_orm_value(
sea_orm::Value::TimeDate(Some(Box::new(value)))
}
#[cfg(feature = "with-time")]
ConvertedType::TimeTime if is_null => sea_orm::Value::TimeTime(None),
#[cfg(feature = "with-time")]
ConvertedType::TimeTime => {
use std::str::FromStr;

Expand All @@ -676,6 +713,8 @@ pub fn converted_value_to_sea_orm_value(
sea_orm::Value::TimeTime(Some(Box::new(value)))
}
#[cfg(feature = "with-time")]
ConvertedType::TimeDateTime if is_null => sea_orm::Value::TimeDateTime(None),
#[cfg(feature = "with-time")]
ConvertedType::TimeDateTime => {
use std::str::FromStr;

Expand All @@ -693,6 +732,10 @@ pub fn converted_value_to_sea_orm_value(
sea_orm::Value::TimeDateTime(Some(Box::new(value)))
}
#[cfg(feature = "with-time")]
ConvertedType::TimeDateTimeWithTimeZone if is_null => {
sea_orm::Value::TimeDateTimeWithTimeZone(None)
}
#[cfg(feature = "with-time")]
ConvertedType::TimeDateTimeWithTimeZone => {
use std::str::FromStr;
let value = sea_orm::entity::prelude::TimeDateTimeWithTimeZone::parse(
Expand All @@ -709,6 +752,8 @@ pub fn converted_value_to_sea_orm_value(
sea_orm::Value::TimeDateTimeWithTimeZone(Some(Box::new(value)))
}
#[cfg(feature = "with-uuid")]
ConvertedType::Uuid if is_null => sea_orm::Value::Uuid(None),
#[cfg(feature = "with-uuid")]
ConvertedType::Uuid => {
use std::str::FromStr;

Expand All @@ -722,6 +767,8 @@ pub fn converted_value_to_sea_orm_value(
sea_orm::Value::Uuid(Some(Box::new(value)))
}
#[cfg(feature = "with-decimal")]
ConvertedType::Decimal if is_null => sea_orm::Value::Decimal(None),
#[cfg(feature = "with-decimal")]
ConvertedType::Decimal => {
use std::str::FromStr;

Expand All @@ -736,6 +783,8 @@ pub fn converted_value_to_sea_orm_value(
sea_orm::Value::Decimal(Some(Box::new(value)))
}
#[cfg(feature = "with-bigdecimal")]
ConvertedType::BigDecimal if is_null => sea_orm::Value::BigDecimal(None),
#[cfg(feature = "with-bigdecimal")]
ConvertedType::BigDecimal => {
use std::str::FromStr;

Expand All @@ -750,6 +799,10 @@ pub fn converted_value_to_sea_orm_value(
sea_orm::Value::BigDecimal(Some(Box::new(value)))
}
#[cfg(feature = "with-postgres-array")]
ConvertedType::Array(ty) if is_null => {
sea_orm::Value::Array(converted_type_to_sea_orm_array_type(&ty)?, None)
}
#[cfg(feature = "with-postgres-array")]
ConvertedType::Array(ty) => {
let list_value = value
.list()?
Expand Down

0 comments on commit 0037a87

Please sign in to comment.