diff --git a/bindings/go/examples/pagination/main.go b/bindings/go/examples/pagination/main.go new file mode 100644 index 000000000..bc7ef4e14 --- /dev/null +++ b/bindings/go/examples/pagination/main.go @@ -0,0 +1,44 @@ +package main + +import ( + "fmt" + "log" + + sdk "bindings/iota_sdk_ffi" +) + +func main() { + client := sdk.GraphQlClientNewDevnet() + address, err := sdk.AddressFromHex("0x611830d3641a68f94a690dcc25d1f4b0dac948325ac18f6dd32564371735f32c") + if err != nil { + log.Fatalf("Failed to parse address: %v", err) + } + + // Limit to 1 to demonstrate pagination + limit := int32(1) + var allObjects []*sdk.Object + var nextCursor *string + for { + if nextCursor != nil { + fmt.Printf("Fetching page with cursor: %s\n", *nextCursor) + } else { + fmt.Printf("Fetching page with cursor: nil\n") + } + page, err := client.Objects(&sdk.ObjectFilter{Owner: &address}, &sdk.PaginationFilter{Direction: sdk.DirectionForward, Cursor: nextCursor, Limit: &limit}) + if err.(*sdk.SdkFfiError) != nil { + log.Fatalf("Failed to get owned objects: %v", err) + } + for _, obj := range page.Data { + allObjects = append(allObjects, obj) + } + if page.PageInfo.HasNextPage { + nextCursor = page.PageInfo.EndCursor + } else { + break + } + } + fmt.Printf("%d objects fetched:\n", len(allObjects)) + for _, obj := range allObjects { + fmt.Println(obj.ObjectId().ToHex()) + } +} diff --git a/bindings/kotlin/examples/Pagination.kt b/bindings/kotlin/examples/Pagination.kt new file mode 100644 index 000000000..69cb5a14e --- /dev/null +++ b/bindings/kotlin/examples/Pagination.kt @@ -0,0 +1,47 @@ +// Copyright (c) 2025 IOTA Stiftung +// SPDX-License-Identifier: Apache-2.0 + +import iota_sdk.Address +import iota_sdk.Direction +import iota_sdk.GraphQlClient +import iota_sdk.Object +import iota_sdk.ObjectFilter +import iota_sdk.PaginationFilter +import kotlinx.coroutines.runBlocking + +fun main() = runBlocking { + try { + val client = GraphQlClient.newDevnet() + val address = + Address.fromHex( + "0x611830d3641a68f94a690dcc25d1f4b0dac948325ac18f6dd32564371735f32c" + ) + val allObjects = mutableListOf() + var nextCursor: String? = null + while (true) { + println("Fetching page with cursor: $nextCursor") + val page = + client.objects( + ObjectFilter(owner = address), + PaginationFilter( + direction = Direction.FORWARD, + cursor = nextCursor, + // Limit to 1 to demonstrate pagination + limit = 1 + ) + ) + allObjects.addAll(page.data) + if (page.pageInfo.hasNextPage) { + nextCursor = page.pageInfo.endCursor + } else { + break + } + } + println("${allObjects.size} objects fetched:") + for (obj in allObjects) { + println(obj.objectId().toHex()) + } + } catch (e: Exception) { + e.printStackTrace() + } +} diff --git a/bindings/python/examples/pagination.py b/bindings/python/examples/pagination.py new file mode 100644 index 000000000..bed6c45f6 --- /dev/null +++ b/bindings/python/examples/pagination.py @@ -0,0 +1,30 @@ +# Copyright (c) 2025 IOTA Stiftung +# SPDX-License-Identifier: Apache-2.0 + +import asyncio +from lib.iota_sdk_ffi import Address, Direction, GraphQlClient, ObjectFilter, PaginationFilter + +async def main(): + client = GraphQlClient.new_devnet() + address = Address.from_hex("0x611830d3641a68f94a690dcc25d1f4b0dac948325ac18f6dd32564371735f32c") + + all_objects = [] + next_cursor = None + while True: + print(f"Fetching page with cursor: {next_cursor}") + page = await client.objects( + ObjectFilter(owner=address), + # Limit to 1 to demonstrate pagination + PaginationFilter(direction=Direction.FORWARD, cursor=next_cursor, limit=1) + ) + all_objects.extend(page.data) + if page.page_info.has_next_page: + next_cursor = page.page_info.end_cursor + else: + break + print(f"{len(all_objects)} objects fetched:") + for obj_id in all_objects: + print(obj_id.object_id().to_hex()) + +if __name__ == "__main__": + asyncio.run(main()) diff --git a/crates/iota-graphql-client/examples/pagination.rs b/crates/iota-graphql-client/examples/pagination.rs new file mode 100644 index 000000000..be544532c --- /dev/null +++ b/crates/iota-graphql-client/examples/pagination.rs @@ -0,0 +1,47 @@ +// Copyright (c) 2025 IOTA Stiftung +// SPDX-License-Identifier: Apache-2.0 + +use anyhow::Result; +use iota_graphql_client::{Client, pagination::PaginationFilter, query_types::ObjectFilter}; +use iota_types::Address; + +#[tokio::main] +async fn main() -> Result<()> { + let client = Client::new_devnet(); + + let address = + Address::from_hex("0x611830d3641a68f94a690dcc25d1f4b0dac948325ac18f6dd32564371735f32c")?; + + let mut all_objects = Vec::new(); + let mut next_cursor = None; + while let Some(cursor) = Some(next_cursor.clone()) { + println!("Fetching page with cursor: {cursor:?}"); + let owned_objects_page = client + .objects( + Some(ObjectFilter { + owner: Some(address), + ..Default::default() + }), + PaginationFilter { + cursor, + // Limit to 1 to demonstrate pagination + limit: Some(1), + ..Default::default() + }, + ) + .await?; + let (page_info, data) = owned_objects_page.into_parts(); + all_objects.extend(data); + if page_info.has_next_page { + next_cursor = page_info.end_cursor.clone(); + } else { + break; + } + } + println!("{} objects fetched:", all_objects.len()); + for obj in &all_objects { + println!("{}", obj.object_id()); + } + + Ok(()) +}