diff --git a/crates/sui-graphql-client/src/lib.rs b/crates/sui-graphql-client/src/lib.rs index 10f13208..7ad09187 100644 --- a/crates/sui-graphql-client/src/lib.rs +++ b/crates/sui-graphql-client/src/lib.rs @@ -104,6 +104,14 @@ impl Page { fn new(page_info: PageInfo, data: Vec) -> Self { Self { page_info, data } } + + pub fn is_empty(&self) -> bool { + self.data.is_empty() + } + + fn new_empty() -> Self { + Self::new(PageInfo::default(), vec![]) + } } /// The GraphQL client for interacting with the Sui blockchain. @@ -254,7 +262,7 @@ impl Client { before: Option, first: Option, last: Option, - ) -> Result>, Error> { + ) -> Result, Error> { ensure!( !(first.is_some() && last.is_some()), "Cannot pass both first and last" @@ -284,9 +292,9 @@ impl Client { .nodes .into_iter() .collect::>(); - Ok(Some(Page::new(page_info, nodes))) + Ok(Page::new(page_info, nodes)) } else { - Ok(None) + Ok(Page::new_empty()) } } @@ -338,7 +346,7 @@ impl Client { first: Option, last: Option, coin_type: Option<&str>, - ) -> Result>, Error> { + ) -> Result, Error> { ensure!( !(first.is_some() && last.is_some()), "Cannot pass both first and last" @@ -359,16 +367,15 @@ impl Client { ) .await?; - Ok(response.map(|x| { - Page::new( - x.page_info, - x.data - .iter() - .flat_map(Coin::try_from_object) - .map(|c| c.into_owned()) - .collect::>(), - ) - })) + Ok(Page::new( + response.page_info, + response + .data + .iter() + .flat_map(Coin::try_from_object) + .map(|c| c.into_owned()) + .collect::>(), + )) } /// Stream of coins for the specified address and coin type. @@ -393,14 +400,14 @@ impl Client { None, ).await?; - if let Some(page) = response { - for object in page.data { + if !response.is_empty() { + for object in response.data() { if let Some(coin) = Coin::try_from_object(&object) { yield coin.into_owned(); } } - if let Some(end_cursor) = page.page_info.end_cursor { + if let Some(end_cursor) = response.page_info.end_cursor { after = Some(end_cursor); } else { break; @@ -536,7 +543,7 @@ impl Client { before: Option, first: Option, last: Option, - ) -> Result>, Error> { + ) -> Result, Error> { ensure!( !(first.is_some() && last.is_some()), "Cannot pass both first and last" @@ -571,9 +578,9 @@ impl Client { .collect::, bcs::Error>>() .map_err(|e| Error::msg(format!("Cannot decode bcs bytes into Event: {e}")))?; - Ok(Some(Page::new(page_info, nodes))) + Ok(Page::new(page_info, nodes)) } else { - Ok(None) + Ok(Page::new_empty()) } } @@ -640,7 +647,7 @@ impl Client { filter: Option>, first: Option, last: Option, - ) -> Result>, Error> { + ) -> Result, Error> { ensure!( !(first.is_some() && last.is_some()), "Cannot pass both first and last" @@ -678,9 +685,9 @@ impl Client { .collect::, bcs::Error>>() .map_err(|e| Error::msg(format!("Cannot decode bcs bytes into Object: {e}")))?; - Ok(Some(Page::new(page_info, objects))) + Ok(Page::new(page_info, objects)) } else { - Ok(None) + Ok(Page::new_empty()) } } @@ -817,7 +824,7 @@ impl Client { first: Option, last: Option, filter: Option>, - ) -> Result>, Error> { + ) -> Result, Error> { ensure!( !(first.is_some() && last.is_some()), "Cannot pass both first and last" @@ -843,9 +850,9 @@ impl Client { .map(|n| n.try_into()) .collect::>>()?; let page = Page::new(page_info, transactions); - Ok(Some(page)) + Ok(page) } else { - Ok(None) + Ok(Page::new_empty()) } } @@ -981,7 +988,7 @@ mod tests { ); assert!( - av.unwrap().is_some(), + !av.unwrap().is_empty(), "Active validators query returned None for network: {n}" ); } @@ -1073,7 +1080,7 @@ mod tests { events.unwrap_err() ); assert!( - events.unwrap().is_some(), + !events.unwrap().is_empty(), "Events query returned no data for network: {n}" ); } diff --git a/crates/sui-graphql-client/src/query_types/mod.rs b/crates/sui-graphql-client/src/query_types/mod.rs index 824b6d65..8a78a29f 100644 --- a/crates/sui-graphql-client/src/query_types/mod.rs +++ b/crates/sui-graphql-client/src/query_types/mod.rs @@ -113,7 +113,7 @@ pub struct MoveObject { // Utility Types // =========================================================================== -#[derive(cynic::QueryFragment, Debug)] +#[derive(Default, cynic::QueryFragment, Debug)] #[cynic(schema = "rpc", graphql_type = "PageInfo")] /// Information about pagination in a connection. pub struct PageInfo {