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

Refactor model members in GraphQL to camelCase #1215

Merged
merged 16 commits into from
Dec 5, 2023
Merged
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions crates/torii/graphql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ async-recursion = "1.0.5"
async-trait.workspace = true
base64.workspace = true
chrono.workspace = true
convert_case = "0.6.0"
dojo-types = { path = "../../dojo-types" }
lazy_static.workspace = true
scarb-ui.workspace = true
Expand All @@ -28,8 +29,8 @@ thiserror.workspace = true
tokio-stream = "0.1.11"
tokio-util = "0.7.7"
tokio.workspace = true
torii-core = { path = "../core" }
toml.workspace = true
torii-core = { path = "../core" }
tracing.workspace = true
url.workspace = true
warp.workspace = true
Expand All @@ -39,7 +40,7 @@ camino.workspace = true
dojo-test-utils = { path = "../../dojo-test-utils", features = [ "build-examples" ] }
dojo-world = { path = "../../dojo-world" }
scarb.workspace = true
serial_test = "2.0.0"
sozo = { path = "../../sozo" }
starknet-crypto.workspace = true
starknet.workspace = true
serial_test = "2.0.0"
44 changes: 21 additions & 23 deletions crates/torii/graphql/src/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ lazy_static! {
pub static ref ENTITY_TYPE_MAPPING: TypeMapping = IndexMap::from([
(Name::new("id"), TypeData::Simple(TypeRef::named(TypeRef::ID))),
(Name::new("keys"), TypeData::Simple(TypeRef::named_list(TypeRef::STRING))),
(Name::new("event_id"), TypeData::Simple(TypeRef::named(TypeRef::STRING))),
(Name::new("eventId"), TypeData::Simple(TypeRef::named(TypeRef::STRING))),
(
Name::new("created_at"),
Name::new("createdAt"),
TypeData::Simple(TypeRef::named(GraphqlType::DateTime.to_string())),
),
(
Name::new("updated_at"),
Name::new("updatedAt"),
TypeData::Simple(TypeRef::named(GraphqlType::DateTime.to_string())),
),
]);
Expand All @@ -26,43 +26,43 @@ lazy_static! {
(Name::new("keys"), TypeData::Simple(TypeRef::named_list(TypeRef::STRING))),
(Name::new("data"), TypeData::Simple(TypeRef::named_list(TypeRef::STRING))),
(
Name::new("created_at"),
Name::new("createdAt"),
TypeData::Simple(TypeRef::named(GraphqlType::DateTime.to_string())),
),
(Name::new("transaction_hash"), TypeData::Simple(TypeRef::named(TypeRef::STRING))),
(Name::new("transactionHash"), TypeData::Simple(TypeRef::named(TypeRef::STRING))),
]);
pub static ref MODEL_TYPE_MAPPING: TypeMapping = IndexMap::from([
(Name::new("id"), TypeData::Simple(TypeRef::named(TypeRef::ID))),
(Name::new("name"), TypeData::Simple(TypeRef::named(TypeRef::STRING))),
(
Name::new("class_hash"),
Name::new("classHash"),
TypeData::Simple(TypeRef::named(Primitive::Felt252(None).to_string())),
),
(
Name::new("transaction_hash"),
Name::new("transactionHash"),
TypeData::Simple(TypeRef::named(Primitive::Felt252(None).to_string())),
),
(
Name::new("created_at"),
Name::new("createdAt"),
TypeData::Simple(TypeRef::named(GraphqlType::DateTime.to_string())),
),
]);
pub static ref TRANSACTION_MAPPING: TypeMapping = IndexMap::from([
(Name::new("id"), TypeData::Simple(TypeRef::named(TypeRef::ID))),
(
Name::new("transaction_hash"),
Name::new("transactionHash"),
TypeData::Simple(TypeRef::named(Primitive::Felt252(None).to_string()))
),
(
Name::new("sender_address"),
Name::new("senderAddress"),
TypeData::Simple(TypeRef::named(Primitive::Felt252(None).to_string()))
),
(
Name::new("calldata"),
TypeData::Simple(TypeRef::named_list(Primitive::Felt252(None).to_string()))
),
(
Name::new("max_fee"),
Name::new("maxFee"),
TypeData::Simple(TypeRef::named(Primitive::Felt252(None).to_string()))
),
(
Expand All @@ -74,21 +74,18 @@ lazy_static! {
TypeData::Simple(TypeRef::named(Primitive::Felt252(None).to_string()))
),
(
Name::new("created_at"),
Name::new("createdAt"),
TypeData::Simple(TypeRef::named(GraphqlType::DateTime.to_string())),
),
]);
pub static ref PAGE_INFO_TYPE_MAPPING: TypeMapping = TypeMapping::from([
(Name::new("has_previous_page"), TypeData::Simple(TypeRef::named(TypeRef::BOOLEAN))),
(Name::new("has_next_page"), TypeData::Simple(TypeRef::named(TypeRef::BOOLEAN))),
(Name::new("hasPreviousPage"), TypeData::Simple(TypeRef::named(TypeRef::BOOLEAN))),
(Name::new("hasNextPage"), TypeData::Simple(TypeRef::named(TypeRef::BOOLEAN))),
(
Name::new("start_cursor"),
TypeData::Simple(TypeRef::named(GraphqlType::Cursor.to_string())),
),
(
Name::new("end_cursor"),
Name::new("startCursor"),
TypeData::Simple(TypeRef::named(GraphqlType::Cursor.to_string())),
),
(Name::new("endCursor"), TypeData::Simple(TypeRef::named(GraphqlType::Cursor.to_string())),),
]);
pub static ref SOCIAL_TYPE_MAPPING: TypeMapping = IndexMap::from([
(Name::new("name"), TypeData::Simple(TypeRef::named(TypeRef::STRING))),
Expand All @@ -102,21 +99,22 @@ lazy_static! {
(Name::new("cover_uri"), TypeData::Simple(TypeRef::named(TypeRef::STRING))),
(Name::new("socials"), TypeData::Simple(TypeRef::named_list(SOCIAL_TYPE_NAME)))
]);
// Todo: refactor this to use the same type as the one in dojo-world
pub static ref METADATA_TYPE_MAPPING: TypeMapping = IndexMap::from([
(Name::new("id"), TypeData::Simple(TypeRef::named(TypeRef::ID))),
(Name::new("uri"), TypeData::Simple(TypeRef::named(TypeRef::STRING))),
(
Name::new("content"),
TypeData::Nested((TypeRef::named(CONTENT_TYPE_NAME), IndexMap::new()))
),
(Name::new("icon_img"), TypeData::Simple(TypeRef::named(TypeRef::STRING))),
(Name::new("cover_img"), TypeData::Simple(TypeRef::named(TypeRef::STRING))),
(Name::new("iconImg"), TypeData::Simple(TypeRef::named(TypeRef::STRING))),
(Name::new("coverImg"), TypeData::Simple(TypeRef::named(TypeRef::STRING))),
(
Name::new("created_at"),
Name::new("createdAt"),
TypeData::Simple(TypeRef::named(GraphqlType::DateTime.to_string()))
),
(
Name::new("updated_at"),
Name::new("updatedAt"),
TypeData::Simple(TypeRef::named(GraphqlType::DateTime.to_string()))
),
]);
Expand Down
8 changes: 4 additions & 4 deletions crates/torii/graphql/src/object/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ impl ConnectionObject {
Name::new("edges"),
TypeData::Simple(TypeRef::named_list(format!("{}Edge", type_name))),
),
(Name::new("total_count"), TypeData::Simple(TypeRef::named_nn(TypeRef::INT))),
(Name::new("totalCount"), TypeData::Simple(TypeRef::named_nn(TypeRef::INT))),
(
Name::new("page_info"),
Name::new("pageInfo"),
TypeData::Nested((TypeRef::named_nn(PAGE_INFO_TYPE_NAME), IndexMap::new())),
),
]);
Expand Down Expand Up @@ -141,8 +141,8 @@ pub fn connection_output(
.collect::<sqlx::Result<Vec<Value>>>();

Ok(ValueMapping::from([
(Name::new("total_count"), Value::from(total_count)),
(Name::new("totalCount"), Value::from(total_count)),
(Name::new("edges"), Value::List(model_edges?)),
(Name::new("page_info"), PageInfoObject::value(page_info)),
(Name::new("pageInfo"), PageInfoObject::value(page_info)),
]))
}
8 changes: 4 additions & 4 deletions crates/torii/graphql/src/object/connection/page_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ impl ObjectTrait for PageInfoObject {
impl PageInfoObject {
pub fn value(page_info: PageInfo) -> Value {
Value::Object(IndexMap::from([
(Name::new("has_previous_page"), Value::from(page_info.has_previous_page)),
(Name::new("has_next_page"), Value::from(page_info.has_next_page)),
(Name::new("hasPreviousPage"), Value::from(page_info.has_previous_page)),
(Name::new("hasNextPage"), Value::from(page_info.has_next_page)),
(
Name::new("start_cursor"),
Name::new("startCursor"),
match page_info.start_cursor {
Some(val) => Value::from(val),
None => Value::Null,
},
),
(
Name::new("end_cursor"),
Name::new("endCursor"),
match page_info.end_cursor {
Some(val) => Value::from(val),
None => Value::Null,
Expand Down
7 changes: 4 additions & 3 deletions crates/torii/graphql/src/object/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ impl EntityObject {
IndexMap::from([
(Name::new("id"), Value::from(entity.id)),
(Name::new("keys"), Value::from(keys)),
(Name::new("event_id"), Value::from(entity.event_id)),
(Name::new("eventId"), Value::from(entity.event_id)),
(
Name::new("created_at"),
Name::new("createdAt"),
Value::from(entity.created_at.format("%Y-%m-%d %H:%M:%S").to_string()),
),
(
Name::new("updated_at"),
Name::new("updatedAt"),
Value::from(entity.updated_at.format("%Y-%m-%d %H:%M:%S").to_string()),
),
])
Expand All @@ -134,6 +134,7 @@ fn model_union_field() -> Field {
match ctx.parent_value.try_to_value()? {
Value::Object(indexmap) => {
let mut conn = ctx.data::<Pool<Sqlite>>()?.acquire().await?;

let entity_id = extract::<String>(indexmap, "id")?;
let model_ids: Vec<(String,)> =
sqlx::query_as("SELECT model_id from entity_model WHERE entity_id = ?")
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 @@ -102,9 +102,9 @@ impl EventObject {
(Name::new("id"), Value::from(event.id)),
(Name::new("keys"), Value::from(keys)),
(Name::new("data"), Value::from(data)),
(Name::new("transaction_hash"), Value::from(event.transaction_hash)),
(Name::new("transactionHash"), Value::from(event.transaction_hash)),
(
Name::new("created_at"),
Name::new("createdAt"),
Value::from(event.created_at.format("%Y-%m-%d %H:%M:%S").to_string()),
),
])
Expand Down
4 changes: 2 additions & 2 deletions crates/torii/graphql/src/object/metadata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ fn metadata_connection_output(
.collect::<sqlx::Result<Vec<Value>>>();

Ok(ValueMapping::from([
(Name::new("total_count"), Value::from(total_count)),
(Name::new("totalCount"), Value::from(total_count)),
(Name::new("edges"), Value::List(edges?)),
(Name::new("page_info"), PageInfoObject::value(page_info)),
(Name::new("pageInfo"), PageInfoObject::value(page_info)),
]))
}

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 @@ -60,10 +60,10 @@ impl ModelObject {
IndexMap::from([
(Name::new("id"), Value::from(model.id)),
(Name::new("name"), Value::from(model.name)),
(Name::new("class_hash"), Value::from(model.class_hash)),
(Name::new("transaction_hash"), Value::from(model.transaction_hash)),
(Name::new("classHash"), Value::from(model.class_hash)),
(Name::new("transactionHash"), Value::from(model.transaction_hash)),
(
Name::new("created_at"),
Name::new("createdAt"),
Value::from(model.created_at.format("%Y-%m-%d %H:%M:%S").to_string()),
),
])
Expand Down
8 changes: 6 additions & 2 deletions crates/torii/graphql/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::str::FromStr;

use async_graphql::dynamic::TypeRef;
use async_graphql::{Name, Value};
use convert_case::{Case, Casing};
use dojo_types::primitive::{Primitive, SqlType};
use sqlx::pool::PoolConnection;
use sqlx::sqlite::SqliteRow;
Expand Down Expand Up @@ -144,8 +145,11 @@ fn fetch_value(
type_name: &str,
is_external: bool,
) -> sqlx::Result<Value> {
let column_name =
if is_external { format!("external_{}", field_name) } else { field_name.to_string() };
let column_name = if is_external {
format!("external_{}", field_name)
} else {
field_name.to_string().to_case(Case::Snake)
};

match Primitive::from_str(type_name) {
// fetch boolean
Expand Down
12 changes: 6 additions & 6 deletions crates/torii/graphql/src/tests/entities_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ mod tests {
r#"
{{
entities {} {{
total_count
totalCount
edges {{
cursor
node {{
keys
}}
}}
page_info {{
has_previous_page
has_next_page
start_cursor
end_cursor
pageInfo {{
hasPreviousPage
hasNextPage
startCursor
endCursor
}}
}}
}}
Expand Down
17 changes: 8 additions & 9 deletions crates/torii/graphql/src/tests/metadata_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ mod tests {
const QUERY: &str = r#"
{
metadatas {
total_count
totalCount
edges {
cursor
node {
uri
cover_img
icon_img
coverImg
iconImg
content {
name
description
Expand All @@ -33,11 +33,11 @@ mod tests {
}
}
}
page_info {
has_previous_page
has_next_page
start_cursor
end_cursor
pageInfo {
hasPreviousPage
hasNextPage
startCursor
endCursor
}
}
}
Expand All @@ -60,7 +60,6 @@ mod tests {
"#,
)
.unwrap();

let world_metadata = dojo_metadata.world.unwrap();
db.update_metadata(&RESOURCE, URI, &world_metadata, &None, &Some(cover_img.to_string()))
.await
Expand Down
Loading
Loading