Skip to content

Commit

Permalink
Added example with custom query around the fuel-core-client (#1175)
Browse files Browse the repository at this point in the history
Makes the `FuelClient::query` public to allow custom queries. Added an
example of how to write custom queries for
FuelLabs/fuel-core#1174 but it requires access
to the `schema.sdl`
  • Loading branch information
crypto523 committed May 9, 2023
1 parent 4ba9639 commit 72d068d
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ fuel-vm-private = { version = "0.31.1", package = "fuel-vm" }
# Common dependencies
anyhow = "1.0"
async-trait = "0.1"
cynic = { version = "2.2.1", features = ["http-reqwest"] }
clap = "4.1"
hyper = { version = "0.14.26" }
rand = "0.8"
Expand Down
2 changes: 1 addition & 1 deletion crates/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ description = "Tx client and schema specification."

[dependencies]
anyhow = { workspace = true }
cynic = { version = "2.2.1", features = ["http-reqwest"] }
cynic = { workspace = true }
derive_more = { version = "0.99" }
eventsource-client = { version = "0.10.2", optional = true }
fuel-core-types = { workspace = true, path = "../types", features = ["serde"] }
Expand Down
2 changes: 1 addition & 1 deletion crates/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl FuelClient {
Self::from_str(url.as_ref())
}

async fn query<ResponseData, Vars>(
pub async fn query<ResponseData, Vars>(
&self,
q: Operation<ResponseData, Vars>,
) -> io::Result<ResponseData>
Expand Down
2 changes: 2 additions & 0 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ path = "tests/metrics.rs"
required-features = ["metrics"]

[dependencies]
async-trait = { workspace = true }
cynic = { workspace = true }
ethers = "1.0.2"
fuel-core = { path = "../crates/fuel-core", default-features = false, features = ["dap", "test-helpers"] }
fuel-core-client = { path = "../crates/client", features = ["test-helpers"] }
Expand Down
81 changes: 81 additions & 0 deletions tests/tests/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,84 @@ async fn block_connection_5(
}
};
}

mod full_block {
use super::*;
use cynic::QueryBuilder;
use fuel_core_client::client::{
schema::{
block::{
BlockByHeightArgs,
Consensus,
Header,
},
schema,
tx::OpaqueTransaction,
BlockId,
U64,
},
FuelClient,
};

#[derive(cynic::QueryFragment, Debug)]
#[cynic(
schema_path = "../crates/client/assets/schema.sdl",
graphql_type = "Query",
variables = "BlockByHeightArgs"
)]
pub struct FullBlockByHeightQuery {
#[arguments(height: $height)]
pub block: Option<FullBlock>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(
schema_path = "../crates/client/assets/schema.sdl",
graphql_type = "Block"
)]
pub struct FullBlock {
pub id: BlockId,
pub header: Header,
pub consensus: Consensus,
pub transactions: Vec<OpaqueTransaction>,
}

#[async_trait::async_trait]
pub trait ClientExt {
async fn full_block_by_height(
&self,
height: u64,
) -> std::io::Result<Option<FullBlock>>;
}

#[async_trait::async_trait]
impl ClientExt for FuelClient {
async fn full_block_by_height(
&self,
height: u64,
) -> std::io::Result<Option<FullBlock>> {
let query = FullBlockByHeightQuery::build(BlockByHeightArgs {
height: Some(U64(height)),
});

let block = self.query(query).await?.block;

Ok(block)
}
}

#[tokio::test]
async fn get_full_block_with_tx() {
let srv = FuelService::from_database(Database::default(), Config::local_node())
.await
.unwrap();

let client = FuelClient::from(srv.bound_address);
let tx = Transaction::default();
client.submit_and_await_commit(&tx).await.unwrap();

let block = client.full_block_by_height(1).await.unwrap().unwrap();
assert_eq!(block.header.height.0, 1);
assert_eq!(block.transactions.len(), 2 /* mint + our tx */);
}
}

0 comments on commit 72d068d

Please sign in to comment.