As decentralized applications continue to grow in complexity, accessing and managing onchain data efficiently has become one of the most important technical challenges. Developers often need a reliable way to serve data to their frontends without relying solely on RPC calls, which can be slow, limited, or difficult to scale.
In this week’s Metis developer workshop, we were joined by Marcus Rein, Developer Relations at The Graph, for an in-depth session on building subgraphs. The focus was on understanding how subgraphs provide structured, queryable, and performant access to blockchain data—especially useful for applications building on the Metis Layer 2 ecosystem.
This post recaps the key takeaways from the workshop, along with supporting resources and code.
Workshop Recording: Watch the full session on YouTube
Code Repository: Subgraph Dev Workshop GitHub
A subgraph is a powerful tool that enables developers to index and query blockchain data using GraphQL. It provides a structured, API-like interface over blockchain data, and allows applications to retrieve specific datasets efficiently and reliably.
While direct smart contract queries through libraries like ethers.js can be effective in small use cases, they quickly become limited when dealing with large volumes of data, cross-contract relationships, or complex filtering logic.
Subgraphs offer the following advantages:
Optional deployment to a decentralized network for fault-tolerant indexing
In the context of Metis, subgraphs unlock significant value for teams building DeFi protocols, NFT marketplaces, DAO dashboards, or analytics-driven applications.
Use cases include:
By leveraging subgraphs, developers reduce reliance on centralized infrastructure and gain more control over how data is accessed and served.
During the workshop, the following steps were demonstrated:
Although the demonstration was conducted using Ethereum’s CryptoPunks contract, the process is fully applicable to any EVM-compatible chain, including Metis.
The workshop introduced three key components of a subgraph:
This is the manifest file that defines the smart contracts, start block, and data sources to be indexed. It also specifies the relationship between contract events and their corresponding event handlers (mappings).
Defines the shape of the data stored in the subgraph. This includes entities, field types, and relationships. It serves as the schema for GraphQL queries.
These TypeScript files contain the logic for transforming event data into entities that match the defined schema. Each event is handled through a function that extracts the relevant data and stores it in the subgraph’s database.
Example transformation:
export function handleAssign(event: AssignEvent): void {
let entity = new Assign(event.transaction.hash.toHex() + "-" + event.logIndex.toString());
entity.to = event.params.to;
entity.punkIndex = event.params.punkIndex;
entity.save();
}
This logic creates an entity for each Assign event, generates a unique ID, and stores the event data in the subgraph’s database.
Once deployed, the subgraph exposes a GraphQL endpoint. Developers can then use this endpoint to query indexed data with high precision and performance.
Example query to fetch recent transfers:
{
transfers(first: 10, orderBy: blockTimestamp, orderDirection: desc) {
from
to
value
blockTimestamp
}
}
GraphQL queries are strongly typed and flexible, allowing developers to request exactly the data they need without over-fetching.
The Graph offers two main deployment options:
The workshop covered how to deploy to Studio and test queries using the built-in GraphQL playground.
Subgraph endpoints can be queried directly from modern web frameworks such as:
The Graph Studio also provides prebuilt query templates and code snippets to make integration fast and seamless.
In our next session with The Graph, we will go deeper into subgraph development, covering:
These topics will help developers unlock even more from the data available on Metis.
We host technical workshops every week focused on tooling, infrastructure, and decentralized development within the Metis ecosystem. To stay up to date:
Have a question, topic request, or project to share? We’d love to hear from you.