From b4f0d71b8d2fae591fa4e9f65972fe4dac2a760f Mon Sep 17 00:00:00 2001 From: akhercha Date: Sun, 17 Mar 2024 22:57:27 +0100 Subject: [PATCH] fix(torii): Added timezone informations to naive datetime strings (#1668) * fix(torii): Added timezone informations to naive datetimes * fix(torii_naive_datetimes): Quick parameters renaming * fix(torii_naive_datetimes): Used rust fmt script * fix(torii_naive_datetimes): Naive datetime refacto * fix(torii_naive_datetimes): Quick refacotring * fix(torii_naive_datetimes): Fixed cargo clippy error * fix(torii_naive_datetimes): Updated datetime retrieval --- crates/torii/graphql/src/constants.rs | 2 ++ crates/torii/graphql/src/object/entity.rs | 8 +++++--- crates/torii/graphql/src/object/event.rs | 4 ++-- crates/torii/graphql/src/object/model.rs | 6 +++--- crates/torii/graphql/src/query/mod.rs | 20 ++++++++++++++++++-- 5 files changed, 30 insertions(+), 10 deletions(-) diff --git a/crates/torii/graphql/src/constants.rs b/crates/torii/graphql/src/constants.rs index 85fe2e79cd..34411bdb91 100644 --- a/crates/torii/graphql/src/constants.rs +++ b/crates/torii/graphql/src/constants.rs @@ -1,3 +1,5 @@ +pub const DATETIME_FORMAT: &str = "%Y-%m-%dT%H:%M:%SZ"; + pub const DEFAULT_LIMIT: u64 = 10; pub const BOOLEAN_TRUE: i64 = 1; diff --git a/crates/torii/graphql/src/object/entity.rs b/crates/torii/graphql/src/object/entity.rs index 9a5c2d5693..8ba12fc34b 100644 --- a/crates/torii/graphql/src/object/entity.rs +++ b/crates/torii/graphql/src/object/entity.rs @@ -12,7 +12,9 @@ use torii_core::types::Entity; use super::inputs::keys_input::keys_argument; use super::{BasicObject, ResolvableObject, TypeMapping, ValueMapping}; -use crate::constants::{ENTITY_NAMES, ENTITY_TABLE, ENTITY_TYPE_NAME, EVENT_ID_COLUMN, ID_COLUMN}; +use crate::constants::{ + DATETIME_FORMAT, ENTITY_NAMES, ENTITY_TABLE, ENTITY_TYPE_NAME, EVENT_ID_COLUMN, ID_COLUMN, +}; use crate::mapping::ENTITY_TYPE_MAPPING; use crate::object::{resolve_many, resolve_one}; use crate::query::{type_mapping_query, value_mapping_from_row}; @@ -94,11 +96,11 @@ impl EntityObject { (Name::new("eventId"), Value::from(entity.event_id)), ( Name::new("createdAt"), - Value::from(entity.created_at.format("%Y-%m-%dT%H:%M:%SZ").to_string()), + Value::from(entity.created_at.format(DATETIME_FORMAT).to_string()), ), ( Name::new("updatedAt"), - Value::from(entity.updated_at.format("%Y-%m-%dT%H:%M:%SZ").to_string()), + Value::from(entity.updated_at.format(DATETIME_FORMAT).to_string()), ), ]) } diff --git a/crates/torii/graphql/src/object/event.rs b/crates/torii/graphql/src/object/event.rs index a1dd2eb282..8e37cab5ea 100644 --- a/crates/torii/graphql/src/object/event.rs +++ b/crates/torii/graphql/src/object/event.rs @@ -9,7 +9,7 @@ use torii_core::types::Event; use super::inputs::keys_input::{keys_argument, parse_keys_argument}; use super::{resolve_many, BasicObject, ResolvableObject, TypeMapping}; -use crate::constants::{EVENT_NAMES, EVENT_TABLE, EVENT_TYPE_NAME, ID_COLUMN}; +use crate::constants::{DATETIME_FORMAT, EVENT_NAMES, EVENT_TABLE, EVENT_TYPE_NAME, ID_COLUMN}; use crate::mapping::EVENT_TYPE_MAPPING; use crate::types::ValueMapping; @@ -67,7 +67,7 @@ impl EventObject { (Name::new("transactionHash"), Value::from(event.transaction_hash)), ( Name::new("createdAt"), - Value::from(event.created_at.format("%Y-%m-%dT%H:%M:%SZ").to_string()), + Value::from(event.created_at.format(DATETIME_FORMAT).to_string()), ), ]) } diff --git a/crates/torii/graphql/src/object/model.rs b/crates/torii/graphql/src/object/model.rs index 974c3f62ff..3dd84b8425 100644 --- a/crates/torii/graphql/src/object/model.rs +++ b/crates/torii/graphql/src/object/model.rs @@ -9,8 +9,8 @@ use torii_core::types::Model; use super::{resolve_many, BasicObject, ResolvableObject, TypeMapping, ValueMapping}; use crate::constants::{ - ID_COLUMN, MODEL_NAMES, MODEL_ORDER_FIELD_TYPE_NAME, MODEL_ORDER_TYPE_NAME, MODEL_TABLE, - MODEL_TYPE_NAME, ORDER_ASC, ORDER_DESC, ORDER_DIR_TYPE_NAME, + DATETIME_FORMAT, ID_COLUMN, MODEL_NAMES, MODEL_ORDER_FIELD_TYPE_NAME, MODEL_ORDER_TYPE_NAME, + MODEL_TABLE, MODEL_TYPE_NAME, ORDER_ASC, ORDER_DESC, ORDER_DIR_TYPE_NAME, }; use crate::mapping::MODEL_TYPE_MAPPING; use crate::object::resolve_one; @@ -110,7 +110,7 @@ impl ModelObject { (Name::new("transactionHash"), Value::from(model.transaction_hash)), ( Name::new("createdAt"), - Value::from(model.created_at.format("%Y-%m-%dT%H:%M:%SZ").to_string()), + Value::from(model.created_at.format(DATETIME_FORMAT).to_string()), ), ]) } diff --git a/crates/torii/graphql/src/query/mod.rs b/crates/torii/graphql/src/query/mod.rs index 95e9095c79..85ed857d9b 100644 --- a/crates/torii/graphql/src/query/mod.rs +++ b/crates/torii/graphql/src/query/mod.rs @@ -2,6 +2,7 @@ use std::str::FromStr; use async_graphql::dynamic::TypeRef; use async_graphql::{Name, Value}; +use chrono::{DateTime, Utc}; use convert_case::{Case, Casing}; use dojo_types::primitive::{Primitive, SqlType}; use sqlx::sqlite::SqliteRow; @@ -163,7 +164,22 @@ fn fetch_value( row.try_get::(&column_name).map(Value::from)?, )), }, - // fetch everything else as non-formated string - _ => Ok(row.try_get::(&column_name).map(Value::from)?), + // fetch everything else + _ => { + let value = match type_name { + "DateTime" => { + let dt = row + .try_get::, &str>(&column_name) + .expect("Should be a stored as UTC Datetime") + .to_rfc3339(); + Value::from(dt) + } + _ => { + let s = row.try_get::(&column_name)?; + Value::from(s) + } + }; + Ok(value) + } } }