Skip to content

Commit

Permalink
fix(torii): Added timezone informations to naive datetime strings (#1668
Browse files Browse the repository at this point in the history
)

* 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
  • Loading branch information
akhercha authored Mar 17, 2024
1 parent 953e5e5 commit b4f0d71
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
2 changes: 2 additions & 0 deletions crates/torii/graphql/src/constants.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
8 changes: 5 additions & 3 deletions crates/torii/graphql/src/object/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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()),
),
])
}
Expand Down
4 changes: 2 additions & 2 deletions crates/torii/graphql/src/object/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()),
),
])
}
Expand Down
6 changes: 3 additions & 3 deletions crates/torii/graphql/src/object/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()),
),
])
}
Expand Down
20 changes: 18 additions & 2 deletions crates/torii/graphql/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -163,7 +164,22 @@ fn fetch_value(
row.try_get::<String, &str>(&column_name).map(Value::from)?,
)),
},
// fetch everything else as non-formated string
_ => Ok(row.try_get::<String, &str>(&column_name).map(Value::from)?),
// fetch everything else
_ => {
let value = match type_name {
"DateTime" => {
let dt = row
.try_get::<DateTime<Utc>, &str>(&column_name)
.expect("Should be a stored as UTC Datetime")
.to_rfc3339();
Value::from(dt)
}
_ => {
let s = row.try_get::<String, &str>(&column_name)?;
Value::from(s)
}
};
Ok(value)
}
}
}

0 comments on commit b4f0d71

Please sign in to comment.