A Subgraph is an open-source data indexing protocol, most commonly associated with The Graph Network, that extracts, processes, and stores blockchain event data into a queryable GraphQL API. It functions by defining a manifest (subgraph.yaml) that specifies the smart contracts to index, the events to listen for, and how to map that data into predefined entity types. This process transforms raw, sequential blockchain logs into a structured database, allowing applications to retrieve specific, aggregated data with complex queries in milliseconds instead of scanning entire blockchains.
Subgraph
What is a Subgraph?
A Subgraph is a specialized data indexing protocol that transforms raw blockchain data into queryable APIs, enabling efficient access to on-chain information for decentralized applications.
The architecture of a Subgraph centers on three core components: the subgraph manifest, the GraphQL schema, and the mapping logic. The GraphQL schema defines the shape of the queryable data through entities and their relationships. The mapping logic, written in AssemblyScript, is the executable code that interprets blockchain events and transactions, converting them into operations that create, update, or delete entities in the store. This decouples application logic from complex data retrieval, providing a performant and reliable data layer for dApps.
Developers deploy Subgraphs to a decentralized network of Indexers, who operate nodes that index the data and serve queries for a fee in GRT tokens. This creates a marketplace for data where Delegators and Curators signal on high-quality Subgraphs. Primary use cases include powering DeFi dashboards that track liquidity pool statistics, NFT marketplaces displaying collection traits and ownership history, and DAO tools that analyze governance proposal data. By abstracting the complexities of direct blockchain node interaction, Subgraphs have become a foundational Web3 infrastructure component for building scalable applications.
How a Subgraph Works
A technical breakdown of the indexing and querying process that powers decentralized data access for blockchain applications.
A subgraph is a set of instructions that defines how to index and transform blockchain data from a specific smart contract into a queryable GraphQL API. It operates by processing events emitted by smart contracts, applying a manifest (subgraph.yaml) to map these events to data entities, and using a Graph Node to continuously sync this structured data to a database. This process transforms raw, sequential blockchain logs into an efficiently organized data layer that applications can query in real-time without interacting directly with a node.
The workflow begins with a developer defining a subgraph manifest, which specifies the smart contract address, the blockchain network, and the events of interest. For each event, a corresponding handler function written in AssemblyScript is created to map the event data to predefined entities (data models). As new blocks are added to the chain, the Graph Node monitors for the specified events, executes the handler functions, and stores the resulting entity data. This creates a perpetually updating index that mirrors the on-chain state in a structured format.
Once deployed and synced, the subgraph exposes its data through a GraphQL endpoint. Applications send GraphQL queries to this endpoint to fetch specific, aggregated, or filtered data—such as a user's transaction history or total protocol TVL—with millisecond latency. This abstraction is critical for performance; querying a subgraph is exponentially faster and more efficient than filtering through raw blockchain data via RPC calls, enabling complex dApp interfaces and analytics dashboards.
The indexing process is secured and decentralized by Indexers, node operators in The Graph network who stake GRT tokens to provide indexing and query processing services. They run Graph Nodes, choose which subgraphs to index based on curation signals, and earn query fees and indexing rewards. Delegators and Curators participate in the ecosystem by staking GRT to signal on high-quality subgraphs, ensuring the network's data is reliable and economically sustainable.
Key Features of a Subgraph
A Subgraph is a serverless indexing protocol that transforms raw blockchain data into a queryable GraphQL API. Its core features define how it ingests, processes, and serves data to applications.
Manifest & Data Sources
The manifest (subgraph.yaml) is the configuration blueprint. It defines the smart contracts to index, the specific blockchain events to listen for, and the handlers that process them. This declarative setup allows developers to specify their data sources without writing complex indexing logic from scratch.
Mappings & Handlers
Mappings are the core transformation logic, written in AssemblyScript or TypeScript. They contain event handlers (e.g., handleTransfer) that are automatically triggered when a specified on-chain event is detected. These handlers decode event data and save structured entities to the store.
Entity Schema
The GraphQL schema (schema.graphql) defines the shape of the indexed data. Developers declare entity types (like User, Transaction) with their fields and relationships. The subgraph runtime automatically generates corresponding database tables and a complete GraphQL API from this schema.
Deterministic Indexing
A subgraph processes blockchain data deterministically, meaning that given the same chain data and manifest, it will always produce an identical database. This ensures data integrity and allows for trustless verification of the indexed results by network nodes.
Querying with GraphQL
The primary output of a subgraph is a GraphQL endpoint. Applications query this API using GraphQL's flexible syntax to fetch precisely the data they need—filtering, sorting, and paginating entities—without requiring complex backend infrastructure or direct RPC calls.
Ecosystem Usage
A Subgraph is a serverless GraphQL API that indexes and organizes blockchain data, enabling efficient querying of events, transactions, and smart contract states. It is a core component of The Graph protocol's decentralized data layer.
Decentralized Data Indexing
A Subgraph defines the smart contracts of interest, the events to index, and how to map event data into queryable entities. Indexers on The Graph network run this code, processing blockchain data in real-time to create a structured, high-performance database. This eliminates the need for developers to run their own complex indexing infrastructure.
GraphQL API Endpoint
Once deployed, a Subgraph exposes a GraphQL endpoint. Developers can query this API using GraphQL's declarative syntax to fetch precisely the data they need—such as user balances, transaction histories, or protocol metrics—without writing complex database queries or parsing raw logs. This is the primary interface for dApps to consume indexed data.
Core Components: Manifest, Schema & Mappings
A Subgraph is built from three core files:
- Subgraph Manifest (
subgraph.yaml): Defines the smart contracts, events, and handlers. - GraphQL Schema (
schema.graphql): Defines the data entities and relationships. - Mapping Scripts (AssemblyScript): Contains the logic to translate blockchain events into the entities defined in the schema.
Use Cases in DeFi & NFTs
Subgraphs are foundational for data-rich applications:
- DeFi Dashboards: Querying real-time Total Value Locked (TVL), liquidity pool stats, and user positions from protocols like Uniswap or Aave.
- NFT Marketplaces: Indexing collections, tracking ownership history, and aggregating traits for discovery.
- Analytics Platforms: Providing historical data on transactions, fees, and user growth for on-chain analysis.
The Graph Network & Decentralization
Subgraphs can be deployed to The Graph Network, a decentralized marketplace for data. Here, Indexers stake GRT to provide indexing and query processing services, Curators signal on high-quality subgraphs, and Delegators secure the network by delegating GRT. Users pay for queries with GRT tokens, creating a sustainable ecosystem.
Hosted Service vs. Decentralized Network
The Graph offers two main deployment environments:
- Hosted Service: A free, managed service run by The Graph team, ideal for development and testing. It is being sunset in favor of the decentralized network.
- Decentralized Network (Mainnet): The production-ready, permissionless network where subgraphs are indexed by a decentralized set of node operators, ensuring reliability and censorship resistance.
Subgraph
A technical deep dive into subgraphs, the core data indexing protocol for querying blockchain data.
A subgraph is a specialized data indexing protocol, most commonly associated with The Graph Network, that extracts, processes, and stores blockchain event data into a queryable database using GraphQL. It defines a schema for the data of interest, a manifest (subgraph.yaml) that maps smart contract events to handlers, and the handler logic (written in AssemblyScript or Rust) that transforms raw on-chain data into the entities defined in the schema. This architecture enables efficient, serverless queries of historical and real-time blockchain data without requiring direct interaction with an RPC node.
The operational lifecycle of a subgraph involves several key components. The subgraph manifest specifies the smart contracts to monitor, the events to index, and the handlers to execute. An indexer node, which is part of The Graph's decentralized network, runs the subgraph code, continuously scanning the blockchain for the specified events. When a matching event is found, the corresponding handler executes, creating or updating entities (typed data objects) in the indexer's database. This process transforms raw, low-level log data into structured, high-level data that can be efficiently queried via a GraphQL API endpoint.
Developers interact with a deployed subgraph primarily through its GraphQL API. Unlike REST APIs, GraphQL allows clients to request precisely the data they need in a single query, specifying the entities, fields, and relationships. A typical query might fetch specific Swap events from a DEX, filtered by pool, token, or time range. This abstraction is fundamental to the Web3 stack, powering the data layers for decentralized applications (dApps), analytics dashboards, and blockchain explorers by providing fast, reliable access to complex, aggregated on-chain information that is otherwise cumbersome to compute directly from a node.
Examples of Subgraphs
Subgraphs power a vast ecosystem of decentralized applications by indexing and querying blockchain data. Here are prominent examples across DeFi, NFTs, and infrastructure.
Comparison: Subgraph vs. Centralized Indexer
A technical comparison of decentralized Subgraph indexing versus traditional centralized indexer services.
| Feature / Metric | The Graph Subgraph | Centralized Indexer |
|---|---|---|
Architecture | Decentralized network | Centralized server(s) |
Data Provenance | On-chain verified | Off-chain, self-reported |
Query Language | GraphQL | Varies (often REST/SQL) |
Uptime Guarantee | Network slashing for downtime | SLA dependent on provider |
Censorship Resistance | ||
Development Model | Open schema (subgraph manifest) | Proprietary, closed schema |
Cost Model | Query fee market (GRT) | Fixed monthly/annual fee |
Upgrade Path | Versioned, immutable deployments | Managed by provider, can break clients |
Frequently Asked Questions (FAQ)
Common questions about Subgraphs, the data indexing protocol for querying blockchain data from networks like Ethereum.
A Subgraph is a data indexing protocol, originally developed for The Graph network, that extracts, processes, and stores blockchain data to make it efficiently queryable via GraphQL. It works by defining a manifest (subgraph.yaml) that specifies the smart contracts to index, the events to listen for, and how to map event data into entities stored in a database. An indexer node then scans the blockchain, executes the mapping logic, and serves the structured data through a GraphQL API endpoint. This process transforms raw, sequential blockchain logs into a queryable graph of related data.
Further Reading
Explore the core components, tools, and real-world applications that define the Subgraph ecosystem for blockchain data indexing.
Subgraph Manifest & Mapping
The core configuration files that define a subgraph's behavior.
- Manifest (
subgraph.yaml): Specifies the smart contract to index, the events to listen for, and the mapping file. - Mapping (
mapping.ts): Contains the AssemblyScript code that translates blockchain events into the entities defined in the schema. This is where the logic for processing and storing data lives.
Hosted Service vs. Decentralized Network
The Graph offers two main deployment options.
- Hosted Service: A free, managed service run by The Graph Foundation. It's easier for prototyping but is being phased out in favor of the decentralized network.
- Decentralized Network (Mainnet): The production-ready, permissionless network where subgraphs are published, indexed, and queried in a decentralized manner, with costs paid in GRT.
Real-World Use Cases
Subgraphs power data for major DeFi and NFT applications.
- Uniswap: Tracks pools, swaps, liquidity, and token data.
- Decentraland: Indexes LAND parcels, wearables, and marketplace sales.
- Aave: Monitors lending pools, user positions, and interest rates. These dApps query their subgraphs to populate dashboards, calculate metrics, and drive user interfaces.
Querying a Subgraph
To fetch data, applications send a POST request to a subgraph's GraphQL endpoint. A typical query might request the last 10 transactions for a specific pool, including amounts, users, and timestamps. Queries can be filtered, sorted, and paginated. Tools like the Graph Explorer provide an interactive playground for testing queries before integrating them into code.
Get In Touch
today.
Our experts will offer a free quote and a 30min call to discuss your project.