Skip to content

Commit

Permalink
sui-graphql-client: add checkpoints query (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-mysten authored Oct 17, 2024
1 parent ad78437 commit e62ce52
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
52 changes: 51 additions & 1 deletion crates/sui-graphql-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use query_types::ChainIdentifierQuery;
use query_types::CheckpointArgs;
use query_types::CheckpointId;
use query_types::CheckpointQuery;
use query_types::CheckpointsArgs;
use query_types::CheckpointsQuery;
use query_types::CoinMetadata;
use query_types::CoinMetadataArgs;
use query_types::CoinMetadataQuery;
Expand Down Expand Up @@ -438,7 +440,7 @@ impl Client {
// Checkpoints API
// ===========================================================================

/// Get the `CheckpointSummary` for a given checkpoint digest or checkpoint id. If none is
/// Get the [`CheckpointSummary`] for a given checkpoint digest or checkpoint id. If none is
/// provided, it will use the last known checkpoint id.
pub async fn checkpoint(
&self,
Expand Down Expand Up @@ -468,6 +470,42 @@ impl Client {
.ok_or_else(|| Error::msg("No data in response"))?
}

/// Get a page of [`CheckpointSummary`] for the provided parameters.
pub async fn checkpoints(
&self,
after: Option<&str>,
before: Option<&str>,
first: Option<i32>,
last: Option<i32>,
) -> Result<Option<Page<CheckpointSummary>>, Error> {
let operation = CheckpointsQuery::build(CheckpointsArgs {
after,
before,
first,
last,
});

let response = self.run_query(&operation).await?;

if let Some(errors) = response.errors {
return Err(Error::msg(format!("{:?}", errors)));
}

if let Some(checkpoints) = response.data {
let cc = checkpoints.checkpoints;
let page_info = cc.page_info;
let nodes = cc
.nodes
.into_iter()
.map(|c| c.try_into())
.collect::<Result<Vec<CheckpointSummary>, _>>()?;

Ok(Some(Page::new(page_info, nodes)))
} else {
Ok(None)
}
}

/// Return the sequence number of the latest checkpoint that has been executed.
pub async fn latest_checkpoint_sequence_number(
&self,
Expand Down Expand Up @@ -1008,6 +1046,18 @@ mod tests {
);
}
}
#[tokio::test]
async fn test_checkpoints_query() {
for (n, _) in NETWORKS {
let client = Client::new(n).unwrap();
let c = client.checkpoints(None, None, None, Some(5)).await;
assert!(
c.is_ok(),
"Checkpoints query failed for network: {n}. Error: {}",
c.unwrap_err()
);
}
}

#[tokio::test]
async fn test_latest_checkpoint_sequence_number_query() {
Expand Down
22 changes: 22 additions & 0 deletions crates/sui-graphql-client/src/query_types/checkpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::query_types::Base64;
use crate::query_types::BigInt;
use crate::query_types::DateTime;
use crate::query_types::Epoch;
use crate::query_types::PageInfo;

// ===========================================================================
// Checkpoint Queries
Expand All @@ -26,6 +27,27 @@ pub struct CheckpointQuery {
pub checkpoint: Option<Checkpoint>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema = "rpc", graphql_type = "Query", variables = "CheckpointsArgs")]
pub struct CheckpointsQuery {
pub checkpoints: CheckpointConnection,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema = "rpc", graphql_type = "CheckpointConnection")]
pub struct CheckpointConnection {
pub nodes: Vec<Checkpoint>,
pub page_info: PageInfo,
}

#[derive(cynic::QueryVariables, Debug)]
pub struct CheckpointsArgs<'a> {
pub first: Option<i32>,
pub after: Option<&'a str>,
pub last: Option<i32>,
pub before: Option<&'a str>,
}

// ===========================================================================
// Checkpoint Query Args
// ===========================================================================
Expand Down
2 changes: 2 additions & 0 deletions crates/sui-graphql-client/src/query_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub use chain::ChainIdentifierQuery;
pub use checkpoint::CheckpointArgs;
pub use checkpoint::CheckpointId;
pub use checkpoint::CheckpointQuery;
pub use checkpoint::CheckpointsArgs;
pub use checkpoint::CheckpointsQuery;
pub use coin::CoinMetadata;
pub use coin::CoinMetadataArgs;
pub use coin::CoinMetadataQuery;
Expand Down

0 comments on commit e62ce52

Please sign in to comment.