QueryAPI Indexing Example
With QueryAPI you can quickly create your own indexer by:
- Writing your own custom indexer function;
- Specifying the schema for your own custom hosted database and write to it with your indexer function;
- Retrieving that data through a GraphQL API.
提示
You can watch a complete video walkthrough of Query API following this link.
How it Works
QueryAPI works by:
- Writing the indexer name to the blockchain, registering its creation;
- Creating the tables as specified in the schema to the GraphQL database, exposing a GraphQL endpoint to query the data;
- Spinning up a cloud process that runs your indexer function, writing to the GraphQL database;
You can access the NEAR QueryAPI by following this link.
This should take you to a dashboard that looks like this:
Video Walkthrough
Tip: Watch the QueryAPI widget introduction.
Creating an Indexer
Clicking on "Create New Indexer" will redirect you in-browser to a Component code editor that looks like this:
This is the interface through which you can create a new Indexer. On here you can specify:
- the filtering, transforming logic in
indexingLogic.js
- the database schema in
schema.sql
- the GraphQL queries in
GraphiQL
- the indexer name on Indexer Name
- from which block to start indexing
Design Workflow
To design and create your indexer, you can follow this recommended workflow:
- Using nearblocks.io, find transactions to smart contracts that you want to index
- Take the block
height
and put it into the Debug Mode filter, open your browser's Developer Console, and hit Play - Inspect the block and write JavaScript code using NEAR Lake Primitives to extract data from a
block
object. (This JS code will be yourIndexingLogic.js
)Tip: Use
context.log
for debugging to ensure you are getting the right results - Add more blocks for debugging, or start following the blockchain to see how new blocks are handled
- Create tables that you need to store the data using Postgres CREATE table syntax. (This SQL code will be your
schema.sql
)
Video Walkthrough
Tip: Watch the introduction to indexing functions.