Skip to content

Commit

Permalink
feat(iota-graphql-rpc-client): add checkpoint graphql example (#3925)
Browse files Browse the repository at this point in the history
* feat(iota-graphql-rpc-client): add checkpoint graphql example

* feat(iota-graphql-rpc-client): add ensure to catch errors

---------

Co-authored-by: Thibault Martinez <[email protected]>
  • Loading branch information
Thoralf-M and thibault-martinez authored Nov 8, 2024
1 parent 1a2e8e4 commit 32b8a07
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 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.

4 changes: 4 additions & 0 deletions crates/iota-graphql-rpc-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ thiserror.workspace = true

# internal dependencies
iota-graphql-rpc-headers.workspace = true

[dev-dependencies]
anyhow.workspace = true
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
67 changes: 67 additions & 0 deletions crates/iota-graphql-rpc-client/examples/checkpoint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright (c) 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

//! This example showcases how to use the GraphQL API by querying data about the
//! latest checkpoint.
//!
//! cargo run --example checkpoint

use iota_graphql_rpc_client::simple_client::SimpleClient;

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
let graphql_client = SimpleClient::new("http://127.0.0.1:8000");
let query = r#"{
checkpoint {
digest
sequenceNumber
validatorSignatures
previousCheckpointDigest
networkTotalTransactions
rollingGasSummary {
computationCost
storageCost
storageRebate
nonRefundableStorageFee
}
epoch {
epochId
referenceGasPrice
startTimestamp
endTimestamp
}
transactionBlocks(first: 2) {
edges {
node {
kind {
__typename
}
digest
sender {
address
}
}
}
}
}
}"#;
let res = graphql_client
.execute_to_graphql(query.to_string(), true, vec![], vec![])
.await?;
anyhow::ensure!(res.errors().is_empty(), "{:?}", res.errors());

let resp_body = res.response_body().data.clone().into_json()?;
// Access a nested field
println!(
"Selected data for checkpoint {}:",
resp_body
.get("checkpoint")
.ok_or(anyhow::anyhow!("missing checkpoint"))?
.get("sequenceNumber")
.ok_or(anyhow::anyhow!("missing sequenceNumber"))?
);
// Full response
println!("{:#?}", resp_body);

Ok(())
}

0 comments on commit 32b8a07

Please sign in to comment.