From 3ae23da3fc94f1fa55cb99ff1fc18aea1387a8e7 Mon Sep 17 00:00:00 2001 From: Yun Date: Fri, 2 Jun 2023 19:15:11 -0700 Subject: [PATCH] Fix sys id camel case + clean up (#411) --- crates/torii/src/graphql/base.graphql | 67 ------------------ crates/torii/src/graphql/mod.rs | 68 +------------------ .../src/graphql/{ => object}/component.rs | 6 +- .../torii/src/graphql/{ => object}/entity.rs | 4 +- .../torii/src/graphql/{ => object}/event.rs | 6 +- crates/torii/src/graphql/object/mod.rs | 64 +++++++++++++++++ .../torii/src/graphql/{ => object}/storage.rs | 3 +- .../torii/src/graphql/{ => object}/system.rs | 6 +- .../src/graphql/{ => object}/system_call.rs | 9 ++- crates/torii/src/graphql/schema.rs | 14 ++-- .../torii/src/graphql/utils/extract_value.rs | 2 +- crates/torii/src/tests/entities_test.rs | 10 ++- crates/torii/src/tests/events_test.rs | 9 ++- 13 files changed, 105 insertions(+), 163 deletions(-) delete mode 100644 crates/torii/src/graphql/base.graphql rename crates/torii/src/graphql/{ => object}/component.rs (97%) rename crates/torii/src/graphql/{ => object}/entity.rs (97%) rename crates/torii/src/graphql/{ => object}/event.rs (96%) create mode 100644 crates/torii/src/graphql/object/mod.rs rename crates/torii/src/graphql/{ => object}/storage.rs (98%) rename crates/torii/src/graphql/{ => object}/system.rs (96%) rename crates/torii/src/graphql/{ => object}/system_call.rs (93%) diff --git a/crates/torii/src/graphql/base.graphql b/crates/torii/src/graphql/base.graphql deleted file mode 100644 index 5d20d50c1f..0000000000 --- a/crates/torii/src/graphql/base.graphql +++ /dev/null @@ -1,67 +0,0 @@ -scalar U8 -scalar U16 -scalar U32 -scalar U64 -scalar U128 -scalar U250 -scalar U256 -scalar Cursor -scalar Boolean -scalar Address -scalar DateTime -scalar FieldElement - -type Entity { - id: ID! - name: String! - partitionId: FieldElement! - keys: FieldElement - transactionHash: FieldElement! - createdAt: DateTime! - components: [Component!] -} - -type Component { - id: ID! - name: String! - address: Address! - classHash: Address! - transactionHash: FieldElement! - storageDefinition: String! - storageStates: [Storage_NAME_!] - createdAt: DateTime! -} - -type Storage_NAME_ { - id: ID! - entity: Entity! - component: Component! - createdAt: DateTime! - # dynamic fields - # ... -} - -type System { - id: ID! - name: String! - address: Address! - classHash: Address! - transactionHash: FieldElement! - createdAt: DateTime! -} - -type SystemCall { - id: ID! - data: String! - transactionHash: FieldElement! - system: System! - createdAt: DateTime! -} - -type Event { - id: ID! - keys: String! - data: String! - systemCall: SystemCall! - createdAt: DateTime! -} \ No newline at end of file diff --git a/crates/torii/src/graphql/mod.rs b/crates/torii/src/graphql/mod.rs index 65893c856a..32a9e8011b 100644 --- a/crates/torii/src/graphql/mod.rs +++ b/crates/torii/src/graphql/mod.rs @@ -1,70 +1,6 @@ -pub mod component; -pub mod constants; -pub mod entity; -pub mod event; +mod constants; +mod object; pub mod schema; pub mod server; -pub mod storage; -pub mod system; -pub mod system_call; - mod types; mod utils; - -use async_graphql::dynamic::{Field, FieldFuture, Object, TypeRef, Union}; -use async_graphql::{Name, Value}; -use indexmap::IndexMap; - -// Type aliases for GraphQL fields -pub type TypeMapping = IndexMap; -pub type ValueMapping = IndexMap; - -pub trait ObjectTrait { - fn name(&self) -> &str; - fn type_name(&self) -> &str; - fn field_type_mapping(&self) -> &TypeMapping; - fn resolvers(&self) -> Vec; - fn nested_fields(&self) -> Option> { - None - } - fn unions(&self) -> Option> { - None - } - - // Create a new GraphQL object - fn object(&self) -> Object { - let mut object = Object::new(self.type_name()); - - // Add fields (ie id, createdAt, etc) - for (field_name, field_type) in self.field_type_mapping() { - let field = create_field(field_name, field_type); - object = object.field(field); - } - - // Add related fields (ie event, system) - if let Some(nested_fields) = self.nested_fields() { - for field in nested_fields { - object = object.field(field); - } - } - - object - } -} - -fn create_field(name: &str, field_type: &str) -> Field { - let outer_name = name.to_owned(); - - Field::new(name, TypeRef::named_nn(field_type), move |ctx| { - let inner_name = outer_name.to_owned(); - - FieldFuture::new(async move { - let mapping = ctx.parent_value.try_downcast_ref::()?; - - match mapping.get(inner_name.as_str()) { - Some(value) => Ok(Some(value.clone())), - _ => Err("field not found".into()), - } - }) - }) -} diff --git a/crates/torii/src/graphql/component.rs b/crates/torii/src/graphql/object/component.rs similarity index 97% rename from crates/torii/src/graphql/component.rs rename to crates/torii/src/graphql/object/component.rs index 2019bde962..bb306860ba 100644 --- a/crates/torii/src/graphql/component.rs +++ b/crates/torii/src/graphql/object/component.rs @@ -7,10 +7,10 @@ use sqlx::pool::PoolConnection; use sqlx::{FromRow, Pool, Result, Sqlite}; use super::storage::{storage_by_column, type_mapping_from_definition, ColumnName}; -use super::types::ScalarType; -use super::utils::extract_value::extract; -use super::utils::{format_name, remove_quotes}; use super::{ObjectTrait, TypeMapping, ValueMapping}; +use crate::graphql::types::ScalarType; +use crate::graphql::utils::extract_value::extract; +use crate::graphql::utils::{format_name, remove_quotes}; #[derive(FromRow, Deserialize)] #[serde(rename_all = "camelCase")] diff --git a/crates/torii/src/graphql/entity.rs b/crates/torii/src/graphql/object/entity.rs similarity index 97% rename from crates/torii/src/graphql/entity.rs rename to crates/torii/src/graphql/object/entity.rs index b404407965..fe4a13f0c7 100644 --- a/crates/torii/src/graphql/entity.rs +++ b/crates/torii/src/graphql/object/entity.rs @@ -6,9 +6,9 @@ use serde::Deserialize; use sqlx::pool::PoolConnection; use sqlx::{FromRow, Pool, Result, Sqlite}; -use super::types::ScalarType; -use super::utils::remove_quotes; use super::{ObjectTrait, TypeMapping, ValueMapping}; +use crate::graphql::types::ScalarType; +use crate::graphql::utils::remove_quotes; #[derive(FromRow, Deserialize)] #[serde(rename_all = "camelCase")] diff --git a/crates/torii/src/graphql/event.rs b/crates/torii/src/graphql/object/event.rs similarity index 96% rename from crates/torii/src/graphql/event.rs rename to crates/torii/src/graphql/object/event.rs index 980262c2fc..18afdf55a5 100644 --- a/crates/torii/src/graphql/event.rs +++ b/crates/torii/src/graphql/object/event.rs @@ -7,10 +7,10 @@ use sqlx::pool::PoolConnection; use sqlx::{FromRow, Pool, Result, Sqlite}; use super::system_call::system_call_by_id; -use super::types::ScalarType; -use super::utils::extract_value::extract; -use super::utils::remove_quotes; use super::{ObjectTrait, TypeMapping, ValueMapping}; +use crate::graphql::types::ScalarType; +use crate::graphql::utils::extract_value::extract; +use crate::graphql::utils::remove_quotes; #[derive(FromRow, Deserialize)] #[serde(rename_all = "camelCase")] diff --git a/crates/torii/src/graphql/object/mod.rs b/crates/torii/src/graphql/object/mod.rs new file mode 100644 index 0000000000..ffd35ab550 --- /dev/null +++ b/crates/torii/src/graphql/object/mod.rs @@ -0,0 +1,64 @@ +pub mod component; +pub mod entity; +pub mod event; +pub mod storage; +pub mod system; +pub mod system_call; + +use async_graphql::dynamic::{Field, FieldFuture, Object, TypeRef, Union}; +use async_graphql::{Name, Value}; +use indexmap::IndexMap; + +// Type aliases for GraphQL fields +pub type TypeMapping = IndexMap; +pub type ValueMapping = IndexMap; + +pub trait ObjectTrait { + fn name(&self) -> &str; + fn type_name(&self) -> &str; + fn field_type_mapping(&self) -> &TypeMapping; + fn resolvers(&self) -> Vec; + fn nested_fields(&self) -> Option> { + None + } + fn unions(&self) -> Option> { + None + } + + // Create a new GraphQL object + fn object(&self) -> Object { + let mut object = Object::new(self.type_name()); + + // Add fields (ie id, createdAt, etc) + for (field_name, field_type) in self.field_type_mapping() { + let field = create_field(field_name, field_type); + object = object.field(field); + } + + // Add related fields (ie event, system) + if let Some(nested_fields) = self.nested_fields() { + for field in nested_fields { + object = object.field(field); + } + } + + object + } +} + +fn create_field(name: &str, field_type: &str) -> Field { + let outer_name = name.to_owned(); + + Field::new(name, TypeRef::named_nn(field_type), move |ctx| { + let inner_name = outer_name.to_owned(); + + FieldFuture::new(async move { + let mapping = ctx.parent_value.try_downcast_ref::()?; + + match mapping.get(inner_name.as_str()) { + Some(value) => Ok(Some(value.clone())), + _ => Err("field not found".into()), + } + }) + }) +} diff --git a/crates/torii/src/graphql/storage.rs b/crates/torii/src/graphql/object/storage.rs similarity index 98% rename from crates/torii/src/graphql/storage.rs rename to crates/torii/src/graphql/object/storage.rs index 60dcc0438d..dde43252aa 100644 --- a/crates/torii/src/graphql/storage.rs +++ b/crates/torii/src/graphql/object/storage.rs @@ -114,7 +114,8 @@ fn value_mapping_from_row(row: &SqliteRow, fields: &TypeMapping) -> Result return Err(Error::TypeNotFound { type_name: field_type.clone() }), }; - value_mapping.insert(field_name.clone(), value); + + value_mapping.insert(Name::new(field_name), value); } Ok(value_mapping) diff --git a/crates/torii/src/graphql/system.rs b/crates/torii/src/graphql/object/system.rs similarity index 96% rename from crates/torii/src/graphql/system.rs rename to crates/torii/src/graphql/object/system.rs index c5dfd2f7fd..2fc9d4b0ee 100644 --- a/crates/torii/src/graphql/system.rs +++ b/crates/torii/src/graphql/object/system.rs @@ -7,10 +7,10 @@ use sqlx::pool::PoolConnection; use sqlx::{FromRow, Pool, Result, Sqlite}; use super::system_call::system_calls_by_system_id; -use super::types::ScalarType; -use super::utils::extract_value::extract; -use super::utils::remove_quotes; use super::{ObjectTrait, TypeMapping, ValueMapping}; +use crate::graphql::types::ScalarType; +use crate::graphql::utils::extract_value::extract; +use crate::graphql::utils::remove_quotes; #[derive(FromRow, Deserialize)] #[serde(rename_all = "camelCase")] diff --git a/crates/torii/src/graphql/system_call.rs b/crates/torii/src/graphql/object/system_call.rs similarity index 93% rename from crates/torii/src/graphql/system_call.rs rename to crates/torii/src/graphql/object/system_call.rs index fea486c3af..238315e877 100644 --- a/crates/torii/src/graphql/system_call.rs +++ b/crates/torii/src/graphql/object/system_call.rs @@ -6,11 +6,10 @@ use serde::Deserialize; use sqlx::pool::PoolConnection; use sqlx::{FromRow, Pool, Result, Sqlite}; -// use super::system::System; use super::system::system_by_id; -use super::types::ScalarType; -use super::utils::extract_value::extract; use super::{ObjectTrait, TypeMapping, ValueMapping}; +use crate::graphql::types::ScalarType; +use crate::graphql::utils::extract_value::extract; #[derive(FromRow, Deserialize)] #[serde(rename_all = "camelCase")] @@ -32,7 +31,7 @@ impl SystemCallObject { (Name::new("id"), TypeRef::ID.to_string()), (Name::new("transactionHash"), TypeRef::STRING.to_string()), (Name::new("data"), TypeRef::STRING.to_string()), - (Name::new("system_id"), TypeRef::ID.to_string()), + (Name::new("systemId"), TypeRef::ID.to_string()), (Name::new("createdAt"), ScalarType::DATE_TIME.to_string()), ]), } @@ -106,7 +105,7 @@ fn value_mapping(system_call: SystemCall) -> ValueMapping { (Name::new("id"), Value::from(system_call.id.to_string())), (Name::new("transactionHash"), Value::from(system_call.transaction_hash)), (Name::new("data"), Value::from(system_call.data)), - (Name::new("system_id"), Value::from(system_call.system_id)), + (Name::new("systemId"), Value::from(system_call.system_id)), ( Name::new("createdAt"), Value::from(system_call.created_at.to_rfc3339_opts(chrono::SecondsFormat::Secs, true)), diff --git a/crates/torii/src/graphql/schema.rs b/crates/torii/src/graphql/schema.rs index d42aa4db9c..7bb96dcf66 100644 --- a/crates/torii/src/graphql/schema.rs +++ b/crates/torii/src/graphql/schema.rs @@ -2,15 +2,15 @@ use anyhow::Result; use async_graphql::dynamic::{Object, Scalar, Schema}; use sqlx::SqlitePool; -use super::component::{Component, ComponentObject}; -use super::entity::EntityObject; -use super::event::EventObject; -use super::storage::{type_mapping_from_definition, StorageObject}; -use super::system::SystemObject; -use super::system_call::SystemCallObject; +use super::object::component::{Component, ComponentObject}; +use super::object::entity::EntityObject; +use super::object::event::EventObject; +use super::object::storage::{type_mapping_from_definition, StorageObject}; +use super::object::system::SystemObject; +use super::object::system_call::SystemCallObject; +use super::object::ObjectTrait; use super::types::ScalarType; use super::utils::format_name; -use super::ObjectTrait; pub async fn build_schema(pool: &SqlitePool) -> Result { let mut schema_builder = Schema::build("Query", None, None); diff --git a/crates/torii/src/graphql/utils/extract_value.rs b/crates/torii/src/graphql/utils/extract_value.rs index f1b235aed3..6387dad912 100644 --- a/crates/torii/src/graphql/utils/extract_value.rs +++ b/crates/torii/src/graphql/utils/extract_value.rs @@ -3,7 +3,7 @@ use std::borrow::Cow; use async_graphql::Result; use super::value_accessor::{ObjectAccessor, ValueAccessor}; -use crate::graphql::ValueMapping; +use crate::graphql::object::ValueMapping; pub trait ExtractValue: Sized { fn extract(value_accessor: ValueAccessor<'_>) -> Result; diff --git a/crates/torii/src/tests/entities_test.rs b/crates/torii/src/tests/entities_test.rs index c7c286ee3d..3ff3612068 100644 --- a/crates/torii/src/tests/entities_test.rs +++ b/crates/torii/src/tests/entities_test.rs @@ -1,16 +1,20 @@ #[cfg(test)] mod tests { + use serde::Deserialize; use sqlx::SqlitePool; - use crate::graphql::entity::Entity; use crate::tests::common::run_graphql_query; + #[derive(Deserialize)] + pub struct Entity { + pub id: String, + } + #[sqlx::test(migrations = "./migrations", fixtures("entities"))] async fn test_entity(pool: SqlitePool) { let _ = pool.acquire().await; - let query = - "{ entity(id: \"entity_1\") { id name partitionId keys transactionHash createdAt } }"; + let query = "{ entity(id: \"entity_1\") { id } }"; let value = run_graphql_query(&pool, query).await; let entity = value.get("entity").ok_or("no entity found").unwrap(); diff --git a/crates/torii/src/tests/events_test.rs b/crates/torii/src/tests/events_test.rs index 046d9e56a2..b594303ff6 100644 --- a/crates/torii/src/tests/events_test.rs +++ b/crates/torii/src/tests/events_test.rs @@ -1,15 +1,20 @@ #[cfg(test)] mod tests { + use serde::Deserialize; use sqlx::SqlitePool; - use crate::graphql::event::Event; use crate::tests::common::run_graphql_query; + #[derive(Deserialize)] + pub struct Event { + pub id: String, + } + #[sqlx::test(migrations = "./migrations", fixtures("systems", "system_calls", "events"))] async fn test_event(pool: SqlitePool) { let _ = pool.acquire().await; - let query = "{ event(id: \"event_1\") { id keys data systemCallId createdAt } }"; + let query = "{ event(id: \"event_1\") { id } }"; let value = run_graphql_query(&pool, query).await; let event = value.get("event").ok_or("no event found").unwrap();