Skip to content

Commit

Permalink
Torii graphql namepsace for transaction, query, and subscription types
Browse files Browse the repository at this point in the history
  • Loading branch information
broody committed Oct 31, 2023
1 parent 60db389 commit eddb812
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 40 deletions.
30 changes: 30 additions & 0 deletions crates/torii/graphql/src/constants.rs
Original file line number Diff line number Diff line change
@@ -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");
1 change: 1 addition & 0 deletions crates/torii/graphql/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod object;

mod constants;
mod error;
mod mapping;
mod query;
Expand Down
7 changes: 3 additions & 4 deletions crates/torii/graphql/src/object/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions crates/torii/graphql/src/object/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions crates/torii/graphql/src/object/metadata.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/graphql/src/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
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 @@ -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;

Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/graphql/src/object/model_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions crates/torii/graphql/src/object/transaction.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
14 changes: 0 additions & 14 deletions crates/torii/graphql/src/query/constants.rs

This file was deleted.

2 changes: 1 addition & 1 deletion crates/torii/graphql/src/query/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 1 addition & 3 deletions crates/torii/graphql/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/graphql/src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 4 additions & 3 deletions crates/torii/graphql/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,12 +32,12 @@ pub async fn build_schema(pool: &SqlitePool) -> Result<Schema> {
.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);
Expand Down Expand Up @@ -86,7 +87,7 @@ pub async fn build_schema(pool: &SqlitePool) -> Result<Schema> {
}

// 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);
}
Expand Down

0 comments on commit eddb812

Please sign in to comment.