Hashtag Web3 Logo

Your First Subgraph: Indexing Blockchain Data with The Graph

Learn how to build a subgraph using The Graph Protocol. This guide provides a step-by-step tutorial on how to index smart contract data and serve it via a.

Your First Subgraph: Indexing Blockchain Data with The Graph - Hashtag Web3 article cover

Building a decentralized application (dApp) involves two main components: writing smart contracts (the on-chain backend) and building a user interface (the off-chain frontend). A major challenge for dApp developers is getting data from the blockchain to display on the frontend. Reading data directly from a smart contract can be slow, inefficient, and limited.

This is the problem that The Graph solves. The Graph is a decentralized protocol for indexing and querying data from blockchains. It allows you to define which data you care about from a smart contract, how to process it, and then serves that data to your frontend via a fast and efficient GraphQL API.

Learning to build a "subgraph" is an essential skill for any full-stack Web3 developer. This guide will walk you through the process.

Why Do We Need Subgraphs?

Imagine you want to display a list of all the NFTs a user owns from your collection. To do this by reading directly from the blockchain, you would have to:

  1. Look through every Transfer event ever emitted by your NFT contract.
  2. Filter them to find the ones where the user was the recipient.
  3. Keep track of which ones they later sent away.
  4. Finally, compile a list of the tokens they currently own.

This is incredibly slow and complex to do on the client-side. A subgraph does all this work on a server and then provides you with a simple API to ask, "Hey, give me all the NFTs owned by this address."

The Core Components of a Subgraph

A subgraph project consists of three main files:

  1. The Subgraph Manifest (subgraph.yaml): This is the configuration file. It tells The Graph which smart contracts to listen to (the dataSource), which events on those contracts to pay attention to, and which functions (called "handlers") to run when those events occur.

  2. The Schema (schema.graphql): This is a GraphQL schema file. You use it to define the shape of the data you want to store and query. For example, you might define an NFT entity with properties like id, owner, and tokenURI.

  3. The Mappings (src/mapping.ts): This is the code that transforms the blockchain data into the entities you defined in your schema. It's written in AssemblyScript (a subset of TypeScript). You write a "handler" function for each event you're interested in. When The Graph sees that event on the blockchain, it runs your handler function, which takes the event data, creates or updates your entities, and saves them to the database.

Step-by-Step Guide to Building a Simple Subgraph

Let's imagine we want to build a subgraph for a simple ERC-721 NFT contract.

Prerequisites: You'll need to have Node.js and npm installed. Then, install The Graph CLI globally: npm install -g @graphprotocol/graph-cli

Step 1: Initialize the Subgraph

The Graph CLI can initialize a new subgraph from an existing contract address. Find an NFT contract on a testnet like Sepolia Etherscan and use its address.

graph init --from-contract <CONTRACT_ADDRESS> --network sepolia my-nft-subgraph

The CLI will introspect the contract and generate a basic template for all three core files based on the contract's events.

Step 2: Define Your Schema (schema.graphql)

Let's define what data we want to save. We want to track each NFT and who currently owns it.

type Token @entity {
  id: ID! # The token ID
  tokenURI: String!
  owner: User!
}

type User @entity {
  id: ID! # The user's address
  tokens: [Token!]! @derivedFrom(field: "owner")
}
  • @entity: This directive tells The Graph to save Token and User as top-level data entities.
  • @derivedFrom: This creates a relationship. The tokens field on the User entity is derived from the owner field on the Token entity.

Step 3: Write the Mapping Logic (src/mapping.ts)

The CLI will have created a boilerplate handler function for the Transfer event. We need to fill it in.

import { Transfer as TransferEvent } from "../generated/MyNFT/MyNFT";
import { Token, User } from "../generated/schema";

export function handleTransfer(event: TransferEvent): void {
  // Find or create the User (the recipient of the NFT)
  let to = User.load(event.params.to.toHex());
  if (!to) {
    to = new User(event.params.to.toHex());
    to.save();
  }
  
  // Find or create the Token
  let tokenId = event.params.tokenId.toString();
  let token = Token.load(tokenId);
  if (!token) {
    token = new Token(tokenId);
    // You might need to call the contract to get the tokenURI
    // let contract = MyNFT.bind(event.address)
    // token.tokenURI = contract.tokenURI(event.params.tokenId)
    token.tokenURI = "/placeholder.json"; // Placeholder for simplicity
  }
  
  // Update the owner of the token
  token.owner = to.id;
  token.save();
}

This function is triggered every time a Transfer event happens. It creates a User entity for the new owner if one doesn't exist, creates a Token entity if it's the first time we've seen this token, and then updates the token's owner field to point to the new user.

Step 4: Deploy the Subgraph

  1. Authenticate: graph auth --studio <YOUR_DEPLOY_KEY>
  2. Codegen & Build: graph codegen && graph build
  3. Deploy: graph deploy --studio <SUBGRAPH_NAME>

After deploying to The Graph's hosted service (the "Subgraph Studio"), it will start indexing the data from your chosen contract. Once it's synced, you'll have a GraphQL endpoint that your dApp can use to instantly query for NFT and owner data.

Learning to build subgraphs is a fundamental step in becoming a full-stack Web3 developer. It's the bridge that connects your on-chain logic to your off-chain user experience, enabling you to build fast, rich, and data-intensive decentralized applications.

The Web3 Opportunity

The Web3 sector is experiencing explosive growth, with demand far outpacing supply for qualified talent. Unlike traditional tech, Web3 offers unique advantages: higher compensation, equity opportunities, fully remote roles, and the chance to work on transformative technology.

Market Context

The Web3 job market has fundamentally different dynamics than Web2:

Compensation: Web3 roles typically pay 20-40% higher than equivalent Web2 positions, with significant bonus and equity components.

Remote-First Culture: Most Web3 organizations operate fully or primarily remote, offering flexibility that's rare in traditional tech.

Growth Trajectory: Career progression happens faster in Web3 due to rapid company scaling and talent shortage.

Equity Upside: Token and equity packages are standard, offering significant wealth-building potential.

Step-by-Step Transition Strategy

Step 1: Build Web3 Knowledge Foundation

Spend 4-8 weeks learning blockchain fundamentals. Understand:

  • How blockchain technology works
  • Different blockchain architectures
  • Smart contracts and their use cases
  • DeFi, NFTs, and DAOs
  • Current Web3 ecosystem and key players

Step 2: Learn Relevant Skills

Depending on your target role:

  • Engineers: Solidity, JavaScript/TypeScript, Web3 libraries (ethers.js, web3.js)
  • Product Managers: Token economics, protocol governance, user growth in Web3
  • Business Development: Market analysis, partnership strategy, regulatory landscape
  • Community/Operations: Community building, Discord management, governance

Step 3: Build Your Portfolio

Create tangible proof of your Web3 expertise:

  • Complete open-source contributions to Web3 projects
  • Build a small DApp or smart contract
  • Write about Web3 topics on Medium or Twitter
  • Contribute to DAOs or community projects
  • Participate in hackathons

Step 4: Network in Web3

The Web3 community is incredibly accessible:

  • Join Discord communities of projects you're interested in
  • Attend Web3 conferences (Consensus, Devcon, ETHDenver)
  • Engage on Twitter/X with Web3 builders and thought leaders
  • Participate in governance forums
  • Join local Web3 meetups

Step 5: Apply Strategically

Target roles that leverage your existing expertise plus new Web3 knowledge:

  • If you're a backend engineer, look for blockchain infrastructure roles
  • If you're a PM, look for protocol product roles
  • If you're in sales/business, look for Web3 business development

Real-World Success Stories

Developer to Smart Contract Engineer

Alex, a 5-year backend engineer at a FAANG company, spent 3 months learning Solidity while maintaining his day job. He contributed to an open-source protocol, caught the attention of a major DeFi project, and transitioned with a 50% salary increase and significant equity.

Product Manager in Web3

Jessica, a PM from traditional finance, leveraged her domain expertise in DeFi. Her understanding of financial products combined with Web3 technology made her incredibly valuable. She found a role at a leading DeFi protocol within 4 weeks.

Career Changer Success

Marcus left his corporate job to focus on Web3 for 6 months. Through consistent learning, networking, and portfolio building, he landed a role leading Developer Relations at a major blockchain platform, with compensation far exceeding his previous role.

Web3-Specific Challenges

Volatility Risk: The sector's volatility can impact job stability. Diversify and build emergency funds.

Regulatory Uncertainty: Regulations are still evolving. Choose projects with strong legal teams.

Due Diligence: Not all projects are legitimate. Research thoroughly before joining.

Learning Curve: The learning curve is steep, but the community is incredibly supportive.

FAQ

Q: Do I need to be a blockchain expert to work in Web3? A: No. Companies need diverse skills-marketing, design, operations, business development. Your existing expertise is valuable; you just need to learn the Web3 context.

Q: How much can I earn in Web3? A: Significantly more than Web2 equivalents. Base salaries are higher, plus signing bonuses, equity, and token packages. Realistic expectation: 30-60% increase from Web2 roles.

Q: Is it risky to transition to Web3? A: Like any emerging industry, there's risk. Mitigate by joining established, well-funded projects with strong teams and track records. Avoid speculation; focus on building.

Q: How long does the transition take? A: 2-6 months depending on your background and effort level. Engineers and product managers transition faster due to transferable skills.

Q: What if the crypto market crashes? A: The fundamental technology and use cases remain valid. Bear markets often create better opportunities-teams can focus on building rather than hype-driven growth.

Key Takeaways

  • Web3 offers significant compensation, growth, and impact opportunities
  • Transition takes 2-6 months with dedicated effort
  • Your existing skills are valuable; focus on learning Web3 context
  • Networking and portfolio building matter more than certifications
  • Join established projects to mitigate risk
  • The community is incredibly supportive and accessible