Skip to content

Commit 5826287

Browse files
committed
Add new GraphQL field to query tx directly
1 parent cec2480 commit 5826287

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

graphql-engine/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ impl QueryRoot {
4848
async fn event(&self, tx_hash: GqlH256) -> async_graphql::Result<Vec<Vec<u8>>> {
4949
Ok(self.client.events_by_tx_hash(&ctypes::TxHash::from(tx_hash.0)).into_iter().map(|x| x.value).collect())
5050
}
51+
52+
/// FIXME: Design a general query scheme to handle both block to tx and tx to block.
53+
async fn transaction(&self, tx_hash: GqlH256) -> Option<u64> {
54+
self.client.transaction(&ctypes::TxHash::from(tx_hash.0).into()).map(|tx| tx.block_number)
55+
}
5156
}
5257

5358
#[derive(Clone)]

integration-test/src/lib.rs

+13
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,19 @@ pub async fn get_event(port: u16, tx_hash: primitives::H256) -> Vec<Vec<u8>> {
168168
list.iter().map(|event| event.as_array().unwrap().iter().map(|x| x.as_i64().unwrap() as u8).collect()).collect()
169169
}
170170

171+
/// Returns the number of block including it, if there is.
172+
pub async fn get_tx(port: u16, tx_hash: primitives::H256) -> Option<u64> {
173+
let query = "query Test($txHash: String!) {
174+
transaction(txHash: $txHash)
175+
}";
176+
let mut variables = Value::Object(Default::default());
177+
variables["txHash"] = Value::String(hex::encode(tx_hash.as_ref()));
178+
179+
let query_result = request_query(port, "engine", query, &variables.to_string()).await;
180+
let value: Value = serde_json::from_str(&query_result).unwrap();
181+
serde_json::from_value(value["data"]["transaction"].clone()).unwrap()
182+
}
183+
171184
/// This is a copy from `foundry-timestamp`.
172185
#[derive(serde::Serialize, serde::Deserialize, Debug)]
173186
pub struct SignedTransaction {

integration-test/tests/integration_test.rs

+2
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,6 @@ async fn events() {
176176

177177
let result = get_event(GRAPHQL_PORT, *tx2.hash()).await;
178178
assert!(result.is_empty());
179+
180+
assert!(get_tx(GRAPHQL_PORT, *tx1.hash()).await.unwrap() <= get_latest_block(GRAPHQL_PORT).await);
179181
}

0 commit comments

Comments
 (0)