From 2727c5ada0eec20ec5e7a1411d9a2a6d83597869 Mon Sep 17 00:00:00 2001 From: broody Date: Tue, 31 Oct 2023 09:58:21 -0700 Subject: [PATCH] Torii graphql namepsace for transaction, query, and subscription types --- crates/torii/graphql/src/constants.rs | 30 +++++++++++++++++++ crates/torii/graphql/src/lib.rs | 1 + crates/torii/graphql/src/object/entity.rs | 7 ++--- crates/torii/graphql/src/object/event.rs | 6 ++-- crates/torii/graphql/src/object/metadata.rs | 6 ++-- crates/torii/graphql/src/object/mod.rs | 2 +- crates/torii/graphql/src/object/model.rs | 6 ++-- crates/torii/graphql/src/object/model_data.rs | 2 +- .../torii/graphql/src/object/transaction.rs | 6 ++-- crates/torii/graphql/src/query/constants.rs | 14 --------- crates/torii/graphql/src/query/data.rs | 2 +- crates/torii/graphql/src/query/mod.rs | 4 +-- crates/torii/graphql/src/route.rs | 2 +- crates/torii/graphql/src/schema.rs | 7 +++-- 14 files changed, 55 insertions(+), 40 deletions(-) create mode 100644 crates/torii/graphql/src/constants.rs delete mode 100644 crates/torii/graphql/src/query/constants.rs diff --git a/crates/torii/graphql/src/constants.rs b/crates/torii/graphql/src/constants.rs new file mode 100644 index 0000000000..ecb3160e70 --- /dev/null +++ b/crates/torii/graphql/src/constants.rs @@ -0,0 +1,30 @@ +pub const DEFAULT_LIMIT: u64 = 10; +pub const BOOLEAN_TRUE: i64 = 1; + +pub const ENTITY_TABLE: &str = "entities"; +pub const EVENT_TABLE: &str = "events"; +pub const MODEL_TABLE: &str = "models"; +pub const TRANSACTION_TABLE: &str = "transactions"; +pub const METADATA_TABLE: &str = "metadata"; + +pub const ID_COLUMN: &str = "id"; +pub const EVENT_ID_COLUMN: &str = "event_id"; +pub const ENTITY_ID_COLUMN: &str = "entity_id"; + +pub const INTERNAL_ENTITY_ID_KEY: &str = "$entity_id$"; + +// objects namespaced to avoid conflicts with user models +pub const ENTITY_TYPE_NAME: &str = "World__Entity"; +pub const MODEL_TYPE_NAME: &str = "World__Model"; +pub const EVENT_TYPE_NAME: &str = "World__Event"; +pub const METADATA_TYPE_NAME: &str = "World__Metadata"; +pub const TRANSACTION_TYPE_NAME: &str = "World__Transaction"; +pub const QUERY_TYPE_NAME: &str = "World__Query"; +pub const SUBSCRIPTION_TYPE_NAME: &str = "World__Subscription"; + +// objects' single and plural names +pub const ENTITY_NAMES: (&str, &str) = ("entity", "entities"); +pub const MODEL_NAMES: (&str, &str) = ("model", "models"); +pub const EVENT_NAMES: (&str, &str) = ("event", "events"); +pub const METADATA_NAMES: (&str, &str) = ("metadata", "metadatas"); +pub const TRANSACTION_NAMES: (&str, &str) = ("transaction", "transactions"); diff --git a/crates/torii/graphql/src/lib.rs b/crates/torii/graphql/src/lib.rs index af2c578f39..9a6784f9da 100644 --- a/crates/torii/graphql/src/lib.rs +++ b/crates/torii/graphql/src/lib.rs @@ -1,5 +1,6 @@ pub mod object; +mod constants; mod error; mod mapping; mod query; diff --git a/crates/torii/graphql/src/object/entity.rs b/crates/torii/graphql/src/object/entity.rs index c9127f6a1f..e7e2299134 100644 --- a/crates/torii/graphql/src/object/entity.rs +++ b/crates/torii/graphql/src/object/entity.rs @@ -13,13 +13,12 @@ use torii_core::types::Entity; use super::connection::{connection_arguments, connection_output, parse_connection_arguments}; use super::inputs::keys_input::{keys_argument, parse_keys_argument}; use super::{ObjectTrait, TypeMapping, ValueMapping}; +use crate::constants::{ENTITY_NAMES, ENTITY_TABLE, ENTITY_TYPE_NAME, EVENT_ID_COLUMN}; use crate::mapping::ENTITY_TYPE_MAPPING; -use crate::query::constants::{ENTITY_TABLE, EVENT_ID_COLUMN}; use crate::query::data::{count_rows, fetch_multiple_rows}; use crate::query::{type_mapping_query, value_mapping_from_row}; use crate::types::TypeData; use crate::utils::extract; - pub struct EntityObject; // TODO: Refactor subscription to not use this @@ -45,11 +44,11 @@ impl EntityObject { impl ObjectTrait for EntityObject { fn name(&self) -> (&str, &str) { - ("entity", "entities") + ENTITY_NAMES } fn type_name(&self) -> &str { - "World__Entity" + ENTITY_TYPE_NAME } fn type_mapping(&self) -> &TypeMapping { diff --git a/crates/torii/graphql/src/object/event.rs b/crates/torii/graphql/src/object/event.rs index 28a407d2af..1dca42e677 100644 --- a/crates/torii/graphql/src/object/event.rs +++ b/crates/torii/graphql/src/object/event.rs @@ -5,19 +5,19 @@ use sqlx::{Pool, Sqlite}; use super::connection::{connection_arguments, connection_output, parse_connection_arguments}; use super::inputs::keys_input::{keys_argument, parse_keys_argument}; use super::{ObjectTrait, TypeMapping}; +use crate::constants::{EVENT_NAMES, EVENT_TABLE, EVENT_TYPE_NAME, ID_COLUMN}; use crate::mapping::EVENT_TYPE_MAPPING; -use crate::query::constants::{EVENT_TABLE, ID_COLUMN}; use crate::query::data::{count_rows, fetch_multiple_rows}; pub struct EventObject; impl ObjectTrait for EventObject { fn name(&self) -> (&str, &str) { - ("event", "events") + EVENT_NAMES } fn type_name(&self) -> &str { - "World__Event" + EVENT_TYPE_NAME } fn type_mapping(&self) -> &TypeMapping { diff --git a/crates/torii/graphql/src/object/metadata.rs b/crates/torii/graphql/src/object/metadata.rs index 0c3b465e43..f2344dd116 100644 --- a/crates/torii/graphql/src/object/metadata.rs +++ b/crates/torii/graphql/src/object/metadata.rs @@ -1,18 +1,18 @@ use async_graphql::dynamic::Field; use super::{ObjectTrait, TypeMapping}; +use crate::constants::{METADATA_NAMES, METADATA_TABLE, METADATA_TYPE_NAME}; use crate::mapping::METADATA_TYPE_MAPPING; -use crate::query::constants::METADATA_TABLE; pub struct MetadataObject; impl ObjectTrait for MetadataObject { fn name(&self) -> (&str, &str) { - ("metadata", "metadatas") + METADATA_NAMES } fn type_name(&self) -> &str { - "World__Metadata" + METADATA_TYPE_NAME } fn type_mapping(&self) -> &TypeMapping { diff --git a/crates/torii/graphql/src/object/mod.rs b/crates/torii/graphql/src/object/mod.rs index 8af5d582b6..60167a2ecc 100644 --- a/crates/torii/graphql/src/object/mod.rs +++ b/crates/torii/graphql/src/object/mod.rs @@ -17,7 +17,7 @@ use self::connection::edge::EdgeObject; use self::connection::{ connection_arguments, connection_output, parse_connection_arguments, ConnectionObject, }; -use crate::query::constants::ID_COLUMN; +use crate::constants::ID_COLUMN; use crate::query::data::{count_rows, fetch_multiple_rows, fetch_single_row}; use crate::query::value_mapping_from_row; use crate::types::{TypeMapping, ValueMapping}; diff --git a/crates/torii/graphql/src/object/model.rs b/crates/torii/graphql/src/object/model.rs index 8f7e70e293..b41ca69b94 100644 --- a/crates/torii/graphql/src/object/model.rs +++ b/crates/torii/graphql/src/object/model.rs @@ -6,8 +6,8 @@ use torii_core::simple_broker::SimpleBroker; use torii_core::types::Model; use super::{ObjectTrait, TypeMapping, ValueMapping}; +use crate::constants::{MODEL_NAMES, MODEL_TABLE, MODEL_TYPE_NAME}; use crate::mapping::MODEL_TYPE_MAPPING; -use crate::query::constants::MODEL_TABLE; pub struct ModelObject; @@ -29,11 +29,11 @@ impl ModelObject { impl ObjectTrait for ModelObject { fn name(&self) -> (&str, &str) { - ("model", "models") + MODEL_NAMES } fn type_name(&self) -> &str { - "World__Model" + MODEL_TYPE_NAME } fn type_mapping(&self) -> &TypeMapping { diff --git a/crates/torii/graphql/src/object/model_data.rs b/crates/torii/graphql/src/object/model_data.rs index a5bc548fca..be2628d620 100644 --- a/crates/torii/graphql/src/object/model_data.rs +++ b/crates/torii/graphql/src/object/model_data.rs @@ -9,8 +9,8 @@ use super::inputs::order_input::{order_argument, parse_order_argument, OrderInpu use super::inputs::where_input::{parse_where_argument, where_argument, WhereInputObject}; use super::inputs::InputObjectTrait; use super::{ObjectTrait, TypeMapping, ValueMapping}; +use crate::constants::{ENTITY_ID_COLUMN, ENTITY_TABLE, ID_COLUMN, INTERNAL_ENTITY_ID_KEY}; use crate::mapping::ENTITY_TYPE_MAPPING; -use crate::query::constants::{ENTITY_ID_COLUMN, ENTITY_TABLE, ID_COLUMN, INTERNAL_ENTITY_ID_KEY}; use crate::query::data::{count_rows, fetch_multiple_rows, fetch_single_row}; use crate::query::value_mapping_from_row; use crate::types::TypeData; diff --git a/crates/torii/graphql/src/object/transaction.rs b/crates/torii/graphql/src/object/transaction.rs index 3ba04578d1..17cf87bc33 100644 --- a/crates/torii/graphql/src/object/transaction.rs +++ b/crates/torii/graphql/src/object/transaction.rs @@ -1,16 +1,16 @@ use super::{ObjectTrait, TypeMapping}; +use crate::constants::{TRANSACTION_NAMES, TRANSACTION_TABLE, TRANSACTION_TYPE_NAME}; use crate::mapping::TRANSACTION_MAPPING; -use crate::query::constants::TRANSACTION_TABLE; pub struct TransactionObject; impl ObjectTrait for TransactionObject { fn name(&self) -> (&str, &str) { - ("transaction", "transactions") + TRANSACTION_NAMES } fn type_name(&self) -> &str { - "Transaction" + TRANSACTION_TYPE_NAME } fn type_mapping(&self) -> &TypeMapping { diff --git a/crates/torii/graphql/src/query/constants.rs b/crates/torii/graphql/src/query/constants.rs deleted file mode 100644 index 0da5b346fc..0000000000 --- a/crates/torii/graphql/src/query/constants.rs +++ /dev/null @@ -1,14 +0,0 @@ -pub const DEFAULT_LIMIT: u64 = 10; -pub const BOOLEAN_TRUE: i64 = 1; - -pub const ENTITY_TABLE: &str = "entities"; -pub const EVENT_TABLE: &str = "events"; -pub const MODEL_TABLE: &str = "models"; -pub const TRANSACTION_TABLE: &str = "transactions"; -pub const METADATA_TABLE: &str = "metadata"; - -pub const ID_COLUMN: &str = "id"; -pub const EVENT_ID_COLUMN: &str = "event_id"; -pub const ENTITY_ID_COLUMN: &str = "entity_id"; - -pub const INTERNAL_ENTITY_ID_KEY: &str = "$entity_id$"; diff --git a/crates/torii/graphql/src/query/data.rs b/crates/torii/graphql/src/query/data.rs index 2e79171f93..58de6dab3a 100644 --- a/crates/torii/graphql/src/query/data.rs +++ b/crates/torii/graphql/src/query/data.rs @@ -2,9 +2,9 @@ use sqlx::pool::PoolConnection; use sqlx::sqlite::SqliteRow; use sqlx::{Result, Sqlite}; -use super::constants::DEFAULT_LIMIT; use super::filter::{Filter, FilterValue}; use super::order::{CursorDirection, Direction, Order}; +use crate::constants::DEFAULT_LIMIT; use crate::object::connection::{cursor, ConnectionArguments}; pub async fn count_rows( diff --git a/crates/torii/graphql/src/query/mod.rs b/crates/torii/graphql/src/query/mod.rs index d2180bbdc9..b256164632 100644 --- a/crates/torii/graphql/src/query/mod.rs +++ b/crates/torii/graphql/src/query/mod.rs @@ -2,18 +2,16 @@ use std::str::FromStr; use async_graphql::dynamic::TypeRef; use async_graphql::{Name, Value}; -use constants::BOOLEAN_TRUE; use dojo_types::primitive::{Primitive, SqlType}; use sqlx::pool::PoolConnection; use sqlx::sqlite::SqliteRow; use sqlx::{Row, Sqlite}; use torii_core::sql::FELT_DELIMITER; -use self::constants::{ENTITY_ID_COLUMN, INTERNAL_ENTITY_ID_KEY}; +use crate::constants::{BOOLEAN_TRUE, ENTITY_ID_COLUMN, INTERNAL_ENTITY_ID_KEY}; use crate::object::model_data::ModelMember; use crate::types::{TypeData, TypeMapping, ValueMapping}; -pub mod constants; pub mod data; pub mod filter; pub mod order; diff --git a/crates/torii/graphql/src/route.rs b/crates/torii/graphql/src/route.rs index 1ed98fe769..fd575fd7ec 100644 --- a/crates/torii/graphql/src/route.rs +++ b/crates/torii/graphql/src/route.rs @@ -8,7 +8,7 @@ use url::Url; use warp::{Filter, Rejection, Reply}; use super::schema::build_schema; -use crate::query::constants::MODEL_TABLE; +use crate::constants::MODEL_TABLE; use crate::query::data::count_rows; pub async fn filter( diff --git a/crates/torii/graphql/src/schema.rs b/crates/torii/graphql/src/schema.rs index b4dae3ea79..e3a912abbb 100644 --- a/crates/torii/graphql/src/schema.rs +++ b/crates/torii/graphql/src/schema.rs @@ -11,6 +11,7 @@ use super::object::event::EventObject; use super::object::model_data::ModelDataObject; use super::object::ObjectTrait; use super::types::ScalarType; +use crate::constants::{QUERY_TYPE_NAME, SUBSCRIPTION_TYPE_NAME}; use crate::object::metadata::MetadataObject; use crate::object::model::ModelObject; use crate::object::transaction::TransactionObject; @@ -31,12 +32,12 @@ pub async fn build_schema(pool: &SqlitePool) -> Result { .collect(); // add field resolvers to query root - let mut query_root = Object::new("Query"); + let mut query_root = Object::new(QUERY_TYPE_NAME); for query in queries { query_root = query_root.field(query); } - let mut schema_builder = Schema::build("Query", None, Some("Subscription")); + let mut schema_builder = Schema::build(QUERY_TYPE_NAME, None, Some(SUBSCRIPTION_TYPE_NAME)); // register model data unions schema_builder = schema_builder.register(union); @@ -86,7 +87,7 @@ pub async fn build_schema(pool: &SqlitePool) -> Result { } // add field resolvers to subscription root - let mut subscription_root = Subscription::new("Subscription"); + let mut subscription_root = Subscription::new(SUBSCRIPTION_TYPE_NAME); for field in subscription_fields { subscription_root = subscription_root.field(field); }