-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(iota-graphql-rpc-client): add checkpoint graphql example (#3925)
* 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
1 parent
1a2e8e4
commit 32b8a07
Showing
3 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |