On-Chain Data Indexing with The Graph
Blockchain queries are slow. The Graph fixes this by indexing on-chain data into queryable subgraphs. Learn how to build fast GraphQL APIs over slow chains.
On-Chain Data Indexing with The Graph: Building Fast Queries Over Slow Chains
Blockchain networks excel at decentralization and immutability, but they're terrible at answering questions. Want to find all token transfers for a specific address in the last week? Run a full node and scan millions of blocks yourself. Want to aggregate DEX trades across time periods? Prepare for timeout errors and rate limits.
This is where The Graph enters. It transforms slow, inefficient blockchain data into fast, queryable APIs using a decentralized indexing protocol. Instead of querying the chain directly, applications query indexed subgraphs—GraphQL endpoints that serve data in milliseconds rather than seconds or failures.
Why On-Chain Queries Are Broken
Traditional blockchain RPC calls are synchronous, block-by-block operations. Ethereum nodes can process roughly 15 transactions per second, and retrieving historical data means replaying state changes sequentially. Complex queries—filtering events, aggregating values, joining data across contracts—require multiple round trips or custom indexing infrastructure.
Projects like LavaPi work with protocols and dApps that hit these scaling bottlenecks early. The Graph solves this systematically by moving query logic off-chain while keeping data integrity on-chain.
The Performance Gap
bash# Direct RPC query: No filtering, no aggregation curl https://eth-mainnet.g.alchemy.com/v2/YOUR-API-KEY \ -X POST \ -H "Content-Type: application/json" \ -d '{"jsonrpc":"2.0","method":"eth_getLogs","params":[{"address":"0xA0b..."}],"id":1}' # Returns: Raw logs, you parse locally. Slow. Expensive. Limited to 10k results.
Compare this to a Graph subgraph query:
graphql{ transfers(first: 100, where: { from: "0xA0b..." }) { id from to value timestamp } }
Same data, instant response, built-in filtering, no client-side processing.
Building a Subgraph
A subgraph definition tells The Graph's indexers which contracts to watch, which events to capture, and how to transform raw blockchain data into queryable entities.
Core Components
Manifest (subgraph.yaml): Specifies the data source—contract address, ABI, event handlers.
Schema (schema.graphql): Defines entity structure and relationships. This becomes your API.
Mapping (mapping.ts): Event handlers that transform blockchain logs into entities.
Example: Indexing an ERC-20 Token
yamlspecVersion: 0.0.5 description: Simple ERC-20 token indexing schema: file: ./schema.graphql dataSources: - kind: ethereum name: Token network: mainnet source: address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48" abi: ERC20 startBlock: 6082465 mapping: kind: ethereum/events apiVersion: 0.0.7 language: wasm/assemblyscript entities: - Transfer eventHandlers: - event: Transfer(indexed address,indexed address,uint256) handler: handleTransfer file: ./src/mapping.ts
typescript// src/mapping.ts import { Transfer } from '../generated/Token/Token' import { Transfer as TransferEntity } from '../generated/schema' export function handleTransfer(event: Transfer): void { let transfer = new TransferEntity(event.transaction.hash.toHex() + '-' + event.logIndex.toString()) transfer.from = event.params.from transfer.to = event.params.to transfer.value = event.params.value transfer.timestamp = event.block.timestamp transfer.save() }
graphql# schema.graphql type Transfer @entity { id: ID! from: Bytes! to: Bytes! value: BigInt! timestamp: BigInt! }
Deploy this to The Graph's hosted service or decentralized network, and within minutes you have a production GraphQL API.
Real-World Trade-Offs
The Graph isn't free. Indexers stake GRT tokens and earn query fees. Subgraphs can lag behind the chain head by blocks. Custom logic beyond event filtering requires off-chain computation. For simple use cases—standard token transfers, basic NFT metadata—it's overkill. For complex dApps tracking hundreds of contracts and millions of events, it's essential.
When LavaPi helps teams build robust blockchain infrastructure, The Graph often becomes a core dependency. It separates concern: let the chain handle consensus; let The Graph handle queries.
Takeaway
Indexing solves blockchain's query problem without sacrificing decentralization or data integrity. The Graph does this at scale, cheaply, and with minimal setup. If your dApp is building custom indexing logic, you're probably reinventing something The Graph already does better.
LavaPi Team
Digital Engineering Company