Skip to content

Commit

Permalink
Refactor torii graphql object traits (#1446)
Browse files Browse the repository at this point in the history
* Refactor torii graphql object traits

* more refactor
  • Loading branch information
broody authored Jan 25, 2024
1 parent 09783a4 commit 795ac75
Show file tree
Hide file tree
Showing 14 changed files with 329 additions and 455 deletions.
2 changes: 2 additions & 0 deletions crates/torii/graphql/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub const ID_COLUMN: &str = "id";
pub const EVENT_ID_COLUMN: &str = "event_id";
pub const ENTITY_ID_COLUMN: &str = "entity_id";
pub const JSON_COLUMN: &str = "json";
pub const TRANSACTION_HASH_COLUMN: &str = "transaction_hash";

pub const INTERNAL_ENTITY_ID_KEY: &str = "$entity_id$";

Expand All @@ -36,6 +37,7 @@ pub const SOCIAL_NAMES: (&str, &str) = ("social", "socials");
pub const CONTENT_NAMES: (&str, &str) = ("content", "contents");
pub const METADATA_NAMES: (&str, &str) = ("metadata", "metadatas");
pub const TRANSACTION_NAMES: (&str, &str) = ("transaction", "transactions");
pub const PAGE_INFO_NAMES: (&str, &str) = ("pageInfo", "");

// misc
pub const ORDER_DIR_TYPE_NAME: &str = "OrderDirection";
Expand Down
14 changes: 3 additions & 11 deletions crates/torii/graphql/src/object/connection/edge.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use async_graphql::dynamic::{Field, TypeRef};
use async_graphql::dynamic::TypeRef;
use async_graphql::Name;

use crate::object::ObjectTrait;
use crate::object::BasicObject;
use crate::types::{GraphqlType, TypeData, TypeMapping};

pub struct EdgeObject {
Expand All @@ -28,7 +28,7 @@ impl EdgeObject {
}
}

impl ObjectTrait for EdgeObject {
impl BasicObject for EdgeObject {
fn name(&self) -> (&str, &str) {
(&self.name, "")
}
Expand All @@ -40,12 +40,4 @@ impl ObjectTrait for EdgeObject {
fn type_mapping(&self) -> &TypeMapping {
&self.type_mapping
}

fn resolve_one(&self) -> Option<Field> {
None
}

fn resolve_many(&self) -> Option<Field> {
None
}
}
4 changes: 2 additions & 2 deletions crates/torii/graphql/src/object/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use sqlx::sqlite::SqliteRow;
use sqlx::Row;

use self::page_info::PageInfoObject;
use super::ObjectTrait;
use super::BasicObject;
use crate::constants::PAGE_INFO_TYPE_NAME;
use crate::query::order::Order;
use crate::query::value_mapping_from_row;
Expand Down Expand Up @@ -55,7 +55,7 @@ impl ConnectionObject {
}
}

impl ObjectTrait for ConnectionObject {
impl BasicObject for ConnectionObject {
fn name(&self) -> (&str, &str) {
(&self.name, "")
}
Expand Down
18 changes: 5 additions & 13 deletions crates/torii/graphql/src/object/connection/page_info.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
use async_graphql::connection::PageInfo;
use async_graphql::dynamic::indexmap::IndexMap;
use async_graphql::dynamic::Field;
use async_graphql::{Name, Value};

use crate::constants::{PAGE_INFO_NAMES, PAGE_INFO_TYPE_NAME};
use crate::mapping::PAGE_INFO_TYPE_MAPPING;
use crate::object::{ObjectTrait, TypeMapping};
use crate::object::{BasicObject, TypeMapping};

pub struct PageInfoObject;

impl ObjectTrait for PageInfoObject {
impl BasicObject for PageInfoObject {
fn name(&self) -> (&str, &str) {
("pageInfo", "")
PAGE_INFO_NAMES
}

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

fn type_mapping(&self) -> &TypeMapping {
&PAGE_INFO_TYPE_MAPPING
}

fn resolve_one(&self) -> Option<Field> {
None
}

fn resolve_many(&self) -> Option<Field> {
None
}
}

impl PageInfoObject {
Expand Down
70 changes: 23 additions & 47 deletions crates/torii/graphql/src/object/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@ use tokio_stream::StreamExt;
use torii_core::simple_broker::SimpleBroker;
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 super::inputs::keys_input::keys_argument;
use super::{BasicObject, ResolvableObject, TypeMapping, ValueMapping};
use crate::constants::{ENTITY_NAMES, ENTITY_TABLE, ENTITY_TYPE_NAME, EVENT_ID_COLUMN, ID_COLUMN};
use crate::mapping::ENTITY_TYPE_MAPPING;
use crate::query::data::{count_rows, fetch_multiple_rows};
use crate::object::{resolve_many, resolve_one};
use crate::query::{type_mapping_query, value_mapping_from_row};
use crate::types::TypeData;
use crate::utils::extract;
pub struct EntityObject;

impl ObjectTrait for EntityObject {
impl BasicObject for EntityObject {
fn name(&self) -> (&str, &str) {
ENTITY_NAMES
}
Expand All @@ -34,54 +33,31 @@ impl ObjectTrait for EntityObject {
&ENTITY_TYPE_MAPPING
}

fn table_name(&self) -> Option<&str> {
Some(ENTITY_TABLE)
}

fn related_fields(&self) -> Option<Vec<Field>> {
Some(vec![model_union_field()])
}
}

fn resolve_many(&self) -> Option<Field> {
let mut field = Field::new(
self.name().1,
TypeRef::named(format!("{}Connection", self.type_name())),
|ctx| {
FieldFuture::new(async move {
let mut conn = ctx.data::<Pool<Sqlite>>()?.acquire().await?;
let connection = parse_connection_arguments(&ctx)?;
let keys = parse_keys_argument(&ctx)?;
let total_count = count_rows(&mut conn, ENTITY_TABLE, &keys, &None).await?;
let (data, page_info) = fetch_multiple_rows(
&mut conn,
ENTITY_TABLE,
EVENT_ID_COLUMN,
&keys,
&None,
&None,
&connection,
total_count,
)
.await?;
let results = connection_output(
&data,
&ENTITY_TYPE_MAPPING,
&None,
EVENT_ID_COLUMN,
total_count,
false,
page_info,
)?;

Ok(Some(Value::Object(results)))
})
},
impl ResolvableObject for EntityObject {
fn resolvers(&self) -> Vec<Field> {
let resolve_one = resolve_one(
ENTITY_TABLE,
ID_COLUMN,
self.name().0,
self.type_name(),
self.type_mapping(),
);

field = connection_arguments(field);
field = keys_argument(field);
let mut resolve_many = resolve_many(
ENTITY_TABLE,
EVENT_ID_COLUMN,
self.name().1,
self.type_name(),
self.type_mapping(),
);
resolve_many = keys_argument(resolve_many);

Some(field)
vec![resolve_one, resolve_many]
}

fn subscriptions(&self) -> Option<Vec<SubscriptionField>> {
Expand Down
64 changes: 13 additions & 51 deletions crates/torii/graphql/src/object/event.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
use async_graphql::dynamic::{
Field, FieldFuture, InputValue, SubscriptionField, SubscriptionFieldFuture, TypeRef,
Field, InputValue, SubscriptionField, SubscriptionFieldFuture, TypeRef,
};
use async_graphql::{Name, Result, Value};
use sqlx::{Pool, Sqlite};
use tokio_stream::{Stream, StreamExt};
use torii_core::simple_broker::SimpleBroker;
use torii_core::sql::FELT_DELIMITER;
use torii_core::types::Event;

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 super::{resolve_many, BasicObject, ResolvableObject, TypeMapping};
use crate::constants::{EVENT_NAMES, EVENT_TABLE, EVENT_TYPE_NAME, ID_COLUMN};
use crate::mapping::EVENT_TYPE_MAPPING;
use crate::query::data::{count_rows, fetch_multiple_rows};
use crate::types::ValueMapping;

pub struct EventObject;

impl ObjectTrait for EventObject {
impl BasicObject for EventObject {
fn name(&self) -> (&str, &str) {
EVENT_NAMES
}
Expand All @@ -30,55 +27,20 @@ impl ObjectTrait for EventObject {
fn type_mapping(&self) -> &TypeMapping {
&EVENT_TYPE_MAPPING
}
}

fn table_name(&self) -> Option<&str> {
Some(EVENT_TABLE)
}

fn resolve_one(&self) -> Option<Field> {
None
}

fn resolve_many(&self) -> Option<Field> {
let mut field = Field::new(
impl ResolvableObject for EventObject {
fn resolvers(&self) -> Vec<Field> {
let mut resolve_many = resolve_many(
EVENT_TABLE,
ID_COLUMN,
self.name().1,
TypeRef::named(format!("{}Connection", self.type_name())),
|ctx| {
FieldFuture::new(async move {
let mut conn = ctx.data::<Pool<Sqlite>>()?.acquire().await?;
let connection = parse_connection_arguments(&ctx)?;
let keys = parse_keys_argument(&ctx)?;
let total_count = count_rows(&mut conn, EVENT_TABLE, &keys, &None).await?;
let (data, page_info) = fetch_multiple_rows(
&mut conn,
EVENT_TABLE,
ID_COLUMN,
&keys,
&None,
&None,
&connection,
total_count,
)
.await?;
let results = connection_output(
&data,
&EVENT_TYPE_MAPPING,
&None,
ID_COLUMN,
total_count,
false,
page_info,
)?;

Ok(Some(Value::Object(results)))
})
},
self.type_name(),
self.type_mapping(),
);
resolve_many = keys_argument(resolve_many);

field = connection_arguments(field);
field = keys_argument(field);

Some(field)
vec![resolve_many]
}

fn subscriptions(&self) -> Option<Vec<SubscriptionField>> {
Expand Down
15 changes: 3 additions & 12 deletions crates/torii/graphql/src/object/metadata/content.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use async_graphql::dynamic::Field;

use super::{ObjectTrait, TypeMapping};
use super::TypeMapping;
use crate::constants::{CONTENT_NAMES, CONTENT_TYPE_NAME};
use crate::mapping::CONTENT_TYPE_MAPPING;
use crate::object::BasicObject;

pub struct ContentObject;

impl ObjectTrait for ContentObject {
impl BasicObject for ContentObject {
fn name(&self) -> (&str, &str) {
CONTENT_NAMES
}
Expand All @@ -18,12 +17,4 @@ impl ObjectTrait for ContentObject {
fn type_mapping(&self) -> &TypeMapping {
&CONTENT_TYPE_MAPPING
}

fn resolve_one(&self) -> Option<Field> {
None
}

fn resolve_many(&self) -> Option<Field> {
None
}
}
24 changes: 8 additions & 16 deletions crates/torii/graphql/src/object/metadata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use sqlx::{Pool, Row, Sqlite};

use super::connection::page_info::PageInfoObject;
use super::connection::{connection_arguments, cursor, parse_connection_arguments};
use super::ObjectTrait;
use super::{BasicObject, ResolvableObject};
use crate::constants::{
ID_COLUMN, JSON_COLUMN, METADATA_NAMES, METADATA_TABLE, METADATA_TYPE_NAME,
};
Expand All @@ -29,7 +29,7 @@ impl MetadataObject {
}
}

impl ObjectTrait for MetadataObject {
impl BasicObject for MetadataObject {
fn name(&self) -> (&str, &str) {
METADATA_NAMES
}
Expand All @@ -41,33 +41,25 @@ impl ObjectTrait for MetadataObject {
fn type_mapping(&self) -> &TypeMapping {
&METADATA_TYPE_MAPPING
}
}

fn table_name(&self) -> Option<&str> {
Some(METADATA_TABLE)
}

fn resolve_one(&self) -> Option<Field> {
None
}

fn resolve_many(&self) -> Option<Field> {
let table_name = self.table_name().unwrap().to_string();
impl ResolvableObject for MetadataObject {
fn resolvers(&self) -> Vec<Field> {
let row_types = self.row_types();

let mut field = Field::new(
self.name().1,
TypeRef::named(format!("{}Connection", self.type_name())),
move |ctx| {
let row_types = row_types.clone();
let table_name = table_name.to_string();

FieldFuture::new(async move {
let mut conn = ctx.data::<Pool<Sqlite>>()?.acquire().await?;
let connection = parse_connection_arguments(&ctx)?;
let total_count = count_rows(&mut conn, &table_name, &None, &None).await?;
let total_count = count_rows(&mut conn, METADATA_TABLE, &None, &None).await?;
let (data, page_info) = fetch_multiple_rows(
&mut conn,
&table_name,
METADATA_TABLE,
ID_COLUMN,
&None,
&None,
Expand All @@ -94,7 +86,7 @@ impl ObjectTrait for MetadataObject {

field = connection_arguments(field);

Some(field)
vec![field]
}
}

Expand Down
Loading

0 comments on commit 795ac75

Please sign in to comment.