Indexer

Overview

  • The Aptos SDK is primarily used for reading on-chain data, but it does have limitations in terms of efficiency and query complexity. Another service that offers more advanced capabilities for reading on-chain data is the Indexer.
  • Aptos Indexer takes raw blockchain data, processes it, and stores it in a more queryable format using traditional databases. You can easily query the data through GraphQL API.

Aptogotchi Example

1. Writing Your Indexer GraphQL Queries.

  • If you're new to GraphQL, you can get started with this video tutorial.
  • Access Aptos Indexer effortlessly via the GraphQL API.
  • For this case, we'll focus on querying the number of Aptogotchi NFTs within the Aptogotchi collection and identifying their respective owners.
  • It's recommended to experiment with the GraphQL API before coding. You can try out this query on the testnet here. The collection_id to use is 0x0b79e60c009db29711615a479074b68c2e51be549d90da022d3186002e5772c6.

Indexer GraphQL Screenshot


export const queryAptogotchiCollection = `
query AptogotchiCollectionQuery($collection_id: String) {
current_collections_v2(
where: { collection_id: { _eq: $collection_id } }
) {
collection_id
collection_name
current_supply
}
current_collection_ownership_v2_view(
where: { collection_id: { _eq: $collection_id } }
order_by: {last_transaction_version: desc}
) {
owner_address
}
}
`;

2. Next, call the GraphQL query in your frontend code.


const collectionResponse: CollectionResponse = await aptosClient.queryIndexer({
query: {
query: queryAptogotchiCollection,
variables: {
collection_id: collectionAddress,
},
},
});

3. Lastly, display the data queried from Indexer.


const firstFewAptogotchi = await Promise.all(
collectionResponse.current_collection_ownership_v2_view
.filter((holder) => holder.owner_address !== account.address)
// TODO: change to limit 3 in gql after indexer fix limit
.slice(0, 3)
.map((holder) =>
aptosClient.view({
payload: {
function: `${NEXT_PUBLIC_CONTRACT_ADDRESS}::main::get_aptogotchi`,
functionArguments: [holder.owner_address],
},
}),
),
);
setCollection(collectionResponse.current_collections_v2[0]);
setFirstFewAptogotchiName(firstFewAptogotchi.map((x) => x[0] as string));



More Resources

Read more about Indexer.

Link