paginate_all
Paginate through every result of a GraphQL connection using cursor-based pagination.
Signature
pub async fn paginate_all<V, N, BuildVariables, ExtractPage>(
&self,
first: Option<i64>,
last: Option<i64>,
build_variables: BuildVariables,
extract_page: ExtractPage,
) -> Result<Vec<N>, anyhow::Error>
where
V: Variables + Serialize + Debug,
<V::Query as GraphQLQuery>::ResponseData: Debug,
BuildVariables: Fn(Option<String>, Option<String>, Option<i64>, Option<i64>) -> V,
ExtractPage: Fn(<V::Query as GraphQLQuery>::ResponseData) -> (Vec<N>, PageInfo);Example
use {
anyhow::Result,
dango_sdk::{HttpClient, PageInfo, accounts},
};
#[tokio::main]
async fn main() -> Result<()> {
let client = HttpClient::new("https://api-mainnet.dango.zone")?;
let all_accounts = client
.paginate_all(
Some(100),
None,
|after, before, first, last| accounts::Variables {
after,
before,
first,
last,
..Default::default()
},
|data| {
let pi = data.accounts.page_info;
(data.accounts.nodes, PageInfo {
start_cursor: pi.start_cursor,
end_cursor: pi.end_cursor,
has_next_page: pi.has_next_page,
has_previous_page: pi.has_previous_page,
})
},
)
.await?;
println!("fetched {} accounts", all_accounts.len());
Ok(())
}Parameters
first — Option<i64>. Page size for forward pagination. Pass Some(n) and last = None.
last — Option<i64>. Page size for backward pagination. Pass Some(n) and first = None.
build_variables — Fn(after, before, first, last) -> V. Builds the query variables for a single page given the cursors.
extract_page — Fn(ResponseData) -> (Vec<N>, PageInfo). Pulls the node list and pagination metadata out of the typed response.
Returns
Vec<N> — every node in the connection, concatenated across pages. Forward pagination preserves source order; backward pagination reverses each page so the final Vec is oldest-first.
Notes
- Exactly one of
firstorlastmust beSome. Other combinations error with"paginate_all requires exactly one offirstorlastto be Some". - Each page is a fresh GraphQL call. Mind the HTTP rate limit on connections with thousands of nodes.
- For the indexer's standard connections (
accounts,transfers,transactions,blocks,events,messages), the typed wrappers —paginate_accounts, etc. — are simpler.
See also
paginate_accounts,paginate_transfers,paginate_transactions,paginate_blocks,paginate_events,paginate_messages— typed wrappers.PageInfo— pagination metadata type.