Skip to content

Commit

Permalink
Switch transaction query arg from id to transactionHash
Browse files Browse the repository at this point in the history
  • Loading branch information
JunichiSugiura committed Jan 15, 2024
1 parent 87599f9 commit 1ce8e47
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion crates/torii/graphql/src/object/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
use async_graphql::dynamic::{Field, FieldFuture, InputValue, TypeRef};
use async_graphql::Value;
use convert_case::{Case, Casing};
use sqlx::{Pool, Row, Sqlite};

use super::{ObjectTrait, TypeMapping};
use crate::constants::{TRANSACTION_NAMES, TRANSACTION_TABLE, TRANSACTION_TYPE_NAME};
use crate::mapping::TRANSACTION_MAPPING;

use crate::query::data::fetch_single_row;
use crate::query::value_mapping_from_row;
use crate::utils::extract;
pub struct TransactionObject;

impl ObjectTrait for TransactionObject {
Expand All @@ -20,4 +27,31 @@ impl ObjectTrait for TransactionObject {
fn table_name(&self) -> Option<&str> {
Some(TRANSACTION_TABLE)
}

fn resolve_one(&self) -> Option<Field> {
let type_mapping = self.type_mapping().clone();
let table_name = self.table_name().unwrap().to_string();

Some(
Field::new(self.name().0, TypeRef::named_nn(self.type_name()), move |ctx| {
let type_mapping = type_mapping.clone();
let table_name = table_name.to_string();

FieldFuture::new(async move {
let mut conn = ctx.data::<Pool<Sqlite>>()?.acquire().await?;
let hash =
extract::<String>(ctx.args.as_index_map(), &COLUMN.to_case(Case::Camel))?;
let data = fetch_single_row(&mut conn, &table_name, COLUMN, &hash).await?;
let model = value_mapping_from_row(&data, &type_mapping, false)?;
Ok(Some(Value::Object(model)))
})
})
.argument(InputValue::new(
COLUMN.to_case(Case::Camel),
TypeRef::named_nn(TypeRef::STRING),
)),
)
}
}

const COLUMN: &str = "transaction_hash";

0 comments on commit 1ce8e47

Please sign in to comment.