Skip to content

Commit

Permalink
Fix Torii graphql type name collision (#1086)
Browse files Browse the repository at this point in the history
* Feat: Refactor target_type, fn member_to_type_data()

* Add prefix World__

* fail models_test

* Possible refactor to path.join

* fmt

* Pass models_test

* Remove the prefix when constructing the table name for nested type

* Remove commented code

* Refactor: Add more context to new code, clean prints

* cargo.lock

* Refactor: Clean and optimize code

* Refactor: Clean code

* Refactor: Optimize code

* Repair files
  • Loading branch information
gianalarcon authored Oct 30, 2023
1 parent 1c0f9ec commit f1e33f7
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 18 deletions.
2 changes: 1 addition & 1 deletion crates/torii/graphql/src/object/connection/page_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl ObjectTrait for PageInfoObject {
}

fn type_name(&self) -> &str {
"PageInfo"
"World__PageInfo"
}

fn type_mapping(&self) -> &TypeMapping {
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 @@ -49,7 +49,7 @@ impl ObjectTrait for EntityObject {
}

fn type_name(&self) -> &str {
"Entity"
"World__Entity"
}

fn type_mapping(&self) -> &TypeMapping {
Expand Down Expand Up @@ -174,8 +174,10 @@ pub async fn model_data_recursive_query(
entity_id: &str,
type_mapping: &TypeMapping,
) -> sqlx::Result<ValueMapping> {
let table_name = path_array.join("$");
let query = format!("SELECT * FROM {} WHERE entity_id = '{}'", &table_name, entity_id);
// For nested types, we need to remove prefix in path array
let namespace = format!("{}_", path_array[0]);
let table_name = &path_array.join("$").replace(&namespace, "");
let query = format!("SELECT * FROM {} WHERE entity_id = '{}'", table_name, entity_id);
let row = sqlx::query(&query).fetch_one(conn.as_mut()).await?;
let mut value_mapping = value_mapping_from_row(&row, type_mapping, true)?;

Expand Down
2 changes: 1 addition & 1 deletion crates/torii/graphql/src/object/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl ObjectTrait for EventObject {
}

fn type_name(&self) -> &str {
"Event"
"World__Event"
}

fn type_mapping(&self) -> &TypeMapping {
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/graphql/src/object/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl ObjectTrait for MetadataObject {
}

fn type_name(&self) -> &str {
"Metadata"
"World__Metadata"
}

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 @@ -27,7 +27,7 @@ pub trait ObjectTrait: Send + Sync {
// Name of the graphql object, singular and plural (eg "player" and "players")
fn name(&self) -> (&str, &str);

// Type name of the graphql object (eg "Player")
// Type name of the graphql object (eg "World__Player")
fn type_name(&self) -> &str;

// Type mapping defines the fields of the graphql object and their corresponding type
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/graphql/src/object/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl ObjectTrait for ModelObject {
}

fn type_name(&self) -> &str {
"Model"
"World__Model"
}

fn type_mapping(&self) -> &TypeMapping {
Expand Down
7 changes: 4 additions & 3 deletions crates/torii/graphql/src/object/model_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@ pub fn object(type_name: &str, type_mapping: &TypeMapping, path_array: &[String]
let mut object = Object::new(type_name);

for (field_name, type_data) in type_mapping.clone() {
let table_name = path_array.join("$");

// For nested types, we need to remove prefix in path array
let namespace = format!("{}_", path_array[0]);
let table_name = path_array.join("$").replace(&namespace, "");
let field = Field::new(field_name.to_string(), type_data.type_ref(), move |ctx| {
let field_name = field_name.clone();
let type_data = type_data.clone();
Expand Down Expand Up @@ -216,7 +217,7 @@ pub fn object(type_name: &str, type_mapping: &TypeMapping, path_array: &[String]
}

fn entity_field() -> Field {
Field::new("entity", TypeRef::named("Entity"), |ctx| {
Field::new("entity", TypeRef::named("World__Entity"), |ctx| {
FieldFuture::new(async move {
match ctx.parent_value.try_to_value()? {
Value::Object(indexmap) => {
Expand Down
5 changes: 2 additions & 3 deletions crates/torii/graphql/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ pub async fn type_mapping_query(
model_id: &str,
) -> sqlx::Result<TypeMapping> {
let model_members = fetch_model_members(conn, model_id).await?;

let (root_members, nested_members): (Vec<&ModelMember>, Vec<&ModelMember>) =
model_members.iter().partition(|member| member.model_idx == 0);

Expand Down Expand Up @@ -93,8 +92,8 @@ fn parse_nested_type(
}
})
.collect();

TypeData::Nested((TypeRef::named(target_type), nested_mapping))
let namespaced = format!("{}_{}", target_id, target_type);
TypeData::Nested((TypeRef::named(namespaced), nested_mapping))
}

fn remove_hex_leading_zeros(value: Value) -> Value {
Expand Down
4 changes: 0 additions & 4 deletions crates/torii/graphql/src/tests/subscription_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ mod tests {

use crate::tests::{model_fixtures, run_graphql_subscription};

//#[ignore]
#[sqlx::test(migrations = "../migrations")]
async fn test_entity_subscription(pool: SqlitePool) {
// Sleep in order to run this test in a single thread
Expand Down Expand Up @@ -93,7 +92,6 @@ mod tests {
rx.recv().await.unwrap();
}

//#[ignore]
#[sqlx::test(migrations = "../migrations")]
async fn test_entity_subscription_with_id(pool: SqlitePool) {
// Sleep in order to run this test in a single thread
Expand Down Expand Up @@ -168,7 +166,6 @@ mod tests {
rx.recv().await.unwrap();
}

//#[ignore]
#[sqlx::test(migrations = "../migrations")]
async fn test_model_subscription(pool: SqlitePool) {
let mut db = Sql::new(pool.clone(), FieldElement::ZERO).await.unwrap();
Expand Down Expand Up @@ -217,7 +214,6 @@ mod tests {
rx.recv().await.unwrap();
}

//#[ignore]
#[sqlx::test(migrations = "../migrations")]
async fn test_model_subscription_with_id(pool: SqlitePool) {
// Sleep in order to run this test at the end in a single thread
Expand Down

0 comments on commit f1e33f7

Please sign in to comment.