Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Torii graphql type name collision #1086

Merged
merged 18 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions crates/torii/core/src/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,9 @@ impl Sql {
) {
match entity {
Ty::Struct(s) => {
println!("recursive path: {:?}", path);
let table_id = path.join("$");
println!("recursive table_id: {}", table_id);
let mut columns = vec!["entity_id".to_string(), "event_id".to_string()];
let mut values = vec![format!("'{entity_id}', '{event_id}'")];

Expand Down Expand Up @@ -328,7 +330,9 @@ impl Sql {
}

fn build_model_query(&mut self, path: Vec<String>, model: &Ty, model_idx: usize) {
println!("query path: {:?}", path);
let table_id = path.join("$");
println!("query table_id: {}", table_id);

let mut query = format!(
"CREATE TABLE IF NOT EXISTS [{table_id}] (entity_id TEXT NOT NULL PRIMARY KEY, \
Expand Down Expand Up @@ -377,7 +381,9 @@ impl Sql {

// If this is not the Model's root table, create a reference to the parent.
if path.len() > 1 {
println!("2nd query path: {:?}", path);
let parent_table_id = path[..path.len() - 1].join("$");
println!("2nd query parent_table_id: {}", parent_table_id);
query.push_str(&format!(
"FOREIGN KEY (entity_id) REFERENCES {parent_table_id} (entity_id), "
));
Expand Down
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
12 changes: 10 additions & 2 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,7 +174,15 @@ pub async fn model_data_recursive_query(
entity_id: &str,
type_mapping: &TypeMapping,
) -> sqlx::Result<ValueMapping> {
let table_name = path_array.join("$");
println!("model_data_recursive_query: path_array: {:?}", path_array);
let mut table_name = path_array[0].clone();
// For nested types, we need to remove prefix in path array
if path_array.len() > 1 {
gianalarcon marked this conversation as resolved.
Show resolved Hide resolved
let pattern = format!("{}_", table_name);
let name = path_array.join("$");
table_name = name.replace(&pattern, "");
}
println!("model_data_recursive_query: table_name: {:?}", table_name);
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
4 changes: 2 additions & 2 deletions crates/torii/graphql/src/object/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl ObjectTrait for EventObject {
}

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

fn type_mapping(&self) -> &TypeMapping {
Expand Down Expand Up @@ -75,7 +75,7 @@ impl ObjectTrait for EventObject {
}

fn related_fields(&self) -> Option<Vec<Field>> {
Some(vec![Field::new("systemCall", TypeRef::named_nn("SystemCall"), |ctx| {
Some(vec![Field::new("systemCall", TypeRef::named_nn("World__SystemCall"), |ctx| {
FieldFuture::new(async move {
let mut conn = ctx.data::<Pool<Sqlite>>()?.acquire().await?;
let event_values = ctx.parent_value.try_downcast_ref::<ValueMapping>()?;
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 @@ -28,7 +28,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
13 changes: 10 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,15 @@ 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("$");

let mut table_name = path_array[0].clone();
gianalarcon marked this conversation as resolved.
Show resolved Hide resolved
// For nested types, we need to remove prefix in path array
if path_array.len() > 1 {
let pattern = format!("{}_", table_name);
let name = path_array.join("$");
table_name = name.replace(&pattern, "");
}
println!("object: path_array: {:?}", path_array);
println!("object: table_name: {:?}", table_name);
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 +223,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
37 changes: 23 additions & 14 deletions crates/torii/graphql/src/object/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl ObjectTrait for SystemObject {
}

fn type_name(&self) -> &str {
"System"
"World__System"
}

fn type_mapping(&self) -> &TypeMapping {
Expand All @@ -29,18 +29,27 @@ impl ObjectTrait for SystemObject {
}

fn related_fields(&self) -> Option<Vec<Field>> {
Some(vec![Field::new("systemCalls", TypeRef::named_nn_list_nn("SystemCall"), |ctx| {
FieldFuture::new(async move {
let mut conn = ctx.data::<Pool<Sqlite>>()?.acquire().await?;
let event_values = ctx.parent_value.try_downcast_ref::<ValueMapping>()?;
let syscall_id = extract::<u64>(event_values, "system_call_id")?;
let data =
fetch_single_row(&mut conn, SYSTEM_CALL_TABLE, "id", &syscall_id.to_string())
.await?;
let system_call = value_mapping_from_row(&data, &SYSTEM_CALL_TYPE_MAPPING, false)?;

Ok(Some(Value::Object(system_call)))
})
})])
Some(vec![Field::new(
"systemCalls",
TypeRef::named_nn_list_nn("World__SystemCall"),
|ctx| {
FieldFuture::new(async move {
let mut conn = ctx.data::<Pool<Sqlite>>()?.acquire().await?;
let event_values = ctx.parent_value.try_downcast_ref::<ValueMapping>()?;
let syscall_id = extract::<u64>(event_values, "system_call_id")?;
let data = fetch_single_row(
&mut conn,
SYSTEM_CALL_TABLE,
"id",
&syscall_id.to_string(),
)
.await?;
let system_call =
value_mapping_from_row(&data, &SYSTEM_CALL_TYPE_MAPPING, false)?;

Ok(Some(Value::Object(system_call)))
})
},
)])
}
}
4 changes: 2 additions & 2 deletions crates/torii/graphql/src/object/system_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl ObjectTrait for SystemCallObject {
}

fn type_name(&self) -> &str {
"SystemCall"
"World__SystemCall"
}

fn type_mapping(&self) -> &TypeMapping {
Expand All @@ -29,7 +29,7 @@ impl ObjectTrait for SystemCallObject {
}

fn related_fields(&self) -> Option<Vec<Field>> {
Some(vec![Field::new("system", TypeRef::named_nn("System"), |ctx| {
Some(vec![Field::new("system", TypeRef::named_nn("World__System"), |ctx| {
FieldFuture::new(async move {
let mut conn = ctx.data::<Pool<Sqlite>>()?.acquire().await?;
let syscall_values = ctx.parent_value.try_downcast_ref::<ValueMapping>()?;
Expand Down
6 changes: 4 additions & 2 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,7 +92,10 @@ fn parse_nested_type(
}
})
.collect();

println!("target_id: {:?}", target_id);
println!("target_type: {:?}", target_type);
println!("nested_mapping: {:?}", nested_mapping);
let target_type = format!("{}_{}", target_id, target_type);
gianalarcon marked this conversation as resolved.
Show resolved Hide resolved
TypeData::Nested((TypeRef::named(target_type), nested_mapping))
}

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
Loading