Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Larkooo committed Nov 29, 2024
1 parent 1de3f01 commit a8afbcd
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 43 deletions.
4 changes: 2 additions & 2 deletions crates/torii/core/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,8 @@ pub fn map_row_to_ty(
let mut ty = schema.clone();
ty.from_json_value(v.clone())?;
Result::<_, PrimitiveError>::Ok(ty)
})
.collect::<Result<Vec<Ty>, _>>()?;
})
.collect::<Result<Vec<Ty>, _>>()?;
}
Ty::ByteArray(bytearray) => {
let value = row.try_get::<String, &str>(&column_name)?;
Expand Down
6 changes: 5 additions & 1 deletion crates/torii/core/src/sql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,11 @@ impl Sql {
let model_table = entity.name();

self.executor.send(QueryMessage::new(
format!("DELETE FROM [{model_table}] WHERE internal_id = ?; DELETE FROM entity_model WHERE entity_id = ? AND model_id = ?").to_string(),
format!(
"DELETE FROM [{model_table}] WHERE internal_id = ?; DELETE FROM entity_model \
WHERE entity_id = ? AND model_id = ?"
)
.to_string(),
vec![Argument::String(entity_id.clone()), Argument::String(format!("{:#x}", model_id))],
QueryType::DeleteEntity(DeleteEntityQuery {
entity_id: entity_id.clone(),
Expand Down
21 changes: 8 additions & 13 deletions crates/torii/graphql/src/object/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ use async_graphql::dynamic::{
Field, FieldFuture, FieldValue, InputValue, SubscriptionField, SubscriptionFieldFuture, TypeRef,
};
use async_graphql::{Name, Value};
use async_recursion::async_recursion;
use dojo_types::naming::get_tag;
use dojo_types::schema::Ty;
use sqlx::pool::PoolConnection;
use sqlx::{Pool, Sqlite};
use tokio_stream::StreamExt;
use torii_core::simple_broker::SimpleBroker;
Expand All @@ -15,13 +13,11 @@ use torii_core::types::Entity;
use super::inputs::keys_input::keys_argument;
use super::{BasicObject, ResolvableObject, TypeMapping, ValueMapping};
use crate::constants::{
DATETIME_FORMAT, ENTITY_ID_COLUMN, ENTITY_NAMES, ENTITY_TABLE, ENTITY_TYPE_NAME,
EVENT_ID_COLUMN, ID_COLUMN,
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::{build_type_mapping, value_mapping_from_row};
use crate::types::TypeData;
use crate::utils;
#[derive(Debug)]
pub struct EntityObject;
Expand Down Expand Up @@ -67,10 +63,8 @@ impl ResolvableObject for EntityObject {
}

fn subscriptions(&self) -> Option<Vec<SubscriptionField>> {
Some(vec![SubscriptionField::new(
"entityUpdated",
TypeRef::named_nn(self.type_name()),
|ctx| {
Some(vec![
SubscriptionField::new("entityUpdated", TypeRef::named_nn(self.type_name()), |ctx| {
SubscriptionFieldFuture::new(async move {
let id = match ctx.args.get("id") {
Some(id) => Some(id.string()?.to_string()),
Expand All @@ -87,9 +81,9 @@ impl ResolvableObject for EntityObject {
}
}))
})
},
)
.argument(InputValue::new("id", TypeRef::named(TypeRef::ID)))])
})
.argument(InputValue::new("id", TypeRef::named(TypeRef::ID))),
])
}
}

Expand Down Expand Up @@ -150,7 +144,8 @@ fn model_union_field() -> Field {
let table_name = get_tag(&namespace, &name);

// Fetch the row data
let query = format!("SELECT * FROM [{}] WHERE internal_entity_id = ?", table_name);
let query =
format!("SELECT * FROM [{}] WHERE internal_entity_id = ?", table_name);
let row =
sqlx::query(&query).bind(&entity_id).fetch_one(&mut *conn).await?;

Expand Down
12 changes: 5 additions & 7 deletions crates/torii/graphql/src/object/event_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use torii_core::types::EventMessage;
use super::inputs::keys_input::keys_argument;
use super::{BasicObject, ResolvableObject, TypeMapping, ValueMapping};
use crate::constants::{
DATETIME_FORMAT, EVENT_ID_COLUMN, EVENT_MESSAGE_ID_COLUMN, EVENT_MESSAGE_NAMES,
EVENT_MESSAGE_TABLE, EVENT_MESSAGE_TYPE_NAME, ID_COLUMN,
DATETIME_FORMAT, EVENT_ID_COLUMN, EVENT_MESSAGE_NAMES, EVENT_MESSAGE_TABLE,
EVENT_MESSAGE_TYPE_NAME, ID_COLUMN,
};
use crate::mapping::ENTITY_TYPE_MAPPING;
use crate::object::{resolve_many, resolve_one};
Expand Down Expand Up @@ -148,16 +148,14 @@ fn model_union_field() -> Field {

// Get the table name
let table_name = get_tag(&namespace, &name);

// Fetch the row data
let query = format!(
"SELECT * FROM [{}] WHERE internal_event_message_id = ?",
table_name
);
let row = sqlx::query(&query)
.bind(&entity_id)
.fetch_one(&mut *conn)
.await?;
let row =
sqlx::query(&query).bind(&entity_id).fetch_one(&mut *conn).await?;

// Use value_mapping_from_row to handle nested structures
let data = value_mapping_from_row(&row, &type_mapping, true)?;
Expand Down
7 changes: 3 additions & 4 deletions crates/torii/graphql/src/object/model_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ use async_graphql::dynamic::{Enum, Field, FieldFuture, InputObject, Object, Type
use async_graphql::Value;
use dojo_types::naming::get_tag;
use dojo_types::schema::Ty;
use sqlx::Pool;
use sqlx::Sqlite;
use sqlx::{Pool, Sqlite};

use super::connection::{connection_arguments, connection_output, parse_connection_arguments};
use super::inputs::order_input::{order_argument, parse_order_argument, OrderInputObject};
use super::inputs::where_input::{parse_where_argument, where_argument, WhereInputObject};
use super::inputs::InputObjectTrait;
use super::{BasicObject, ResolvableObject, TypeMapping, ValueMapping};
use crate::constants::{
ENTITY_ID_COLUMN, ENTITY_TABLE, ENTITY_TYPE_NAME, EVENT_ID_COLUMN, EVENT_MESSAGE_TABLE,
EVENT_MESSAGE_TYPE_NAME, ID_COLUMN, INTERNAL_ENTITY_ID_KEY,
ENTITY_ID_COLUMN, ENTITY_TABLE, ENTITY_TYPE_NAME, EVENT_MESSAGE_TABLE, EVENT_MESSAGE_TYPE_NAME,
ID_COLUMN, INTERNAL_ENTITY_ID_KEY,
};
use crate::mapping::ENTITY_TYPE_MAPPING;
use crate::query::data::{count_rows, fetch_multiple_rows, fetch_single_row};
Expand Down
27 changes: 11 additions & 16 deletions crates/torii/graphql/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ 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 dojo_types::schema::Ty;
use regex::Regex;
Expand Down Expand Up @@ -130,11 +129,13 @@ pub fn value_mapping_from_row(

match type_data {
TypeData::Simple(type_ref) => {
let mut value = fetch_value(row, &column_name, &type_ref.to_string(), is_external)?;
let mut value =
fetch_value(row, &column_name, &type_ref.to_string(), is_external)?;

// handles felt arrays stored as string (ex: keys)
if let (TypeRef::List(_), Value::String(s)) = (type_ref, &value) {
let mut felts: Vec<_> = s.split(SQL_FELT_DELIMITER).map(Value::from).collect();
let mut felts: Vec<_> =
s.split(SQL_FELT_DELIMITER).map(Value::from).collect();
felts.pop(); // removes empty item
value = Value::List(felts);
}
Expand All @@ -144,18 +145,15 @@ pub fn value_mapping_from_row(
TypeData::List(inner) => {
let value = fetch_value(row, &column_name, "String", is_external)?;
if let Value::String(json_str) = value {
let array_value: Value = serde_json::from_str(&json_str)
.map_err(|e| sqlx::Error::Protocol(format!("JSON parse error: {}", e)))?;
let array_value: Value = serde_json::from_str(&json_str).map_err(|e| {
sqlx::Error::Protocol(format!("JSON parse error: {}", e))
})?;
value_mapping.insert(Name::new(field_name), array_value);
}
}
TypeData::Nested((_, nested_mapping)) => {
let nested_values = build_value_mapping(
row,
nested_mapping,
&column_name,
is_external,
)?;
let nested_values =
build_value_mapping(row, nested_mapping, &column_name, is_external)?;
value_mapping.insert(Name::new(field_name), Value::Object(nested_values));
}
}
Expand All @@ -182,11 +180,8 @@ fn fetch_value(
type_name: &str,
is_external: bool,
) -> sqlx::Result<Value> {
let mut column_name = if !is_external {
format!("internal_{}", field_name)
} else {
field_name.to_string()
};
let mut column_name =
if !is_external { format!("internal_{}", field_name) } else { field_name.to_string() };

// for enum options, remove the ".option" suffix to get the variant
// through the enum itself field name
Expand Down

0 comments on commit a8afbcd

Please sign in to comment.