Understanding and Setting Up The Graph Protocol: A Beginner's Guide (1/2)

Diluk AngeloDiluk Angelo
4 min read

What is The Graph? ๐Ÿค”

Imagine trying to find a specific piece of information in thousands of pages of documents. Pretty hard, right? That's similar to what it's like trying to get specific data from a blockchain. The Graph is like having a super-smart librarian who organizes all this information and helps you find exactly what you need, instantly!

In Simple Terms:

  • The Graph is like "Google for blockchain data"

  • It makes it easy to get information from blockchains

  • It organizes data in a way that's fast and efficient to access

Why Do We Need The Graph? ๐ŸŽฏ

Let's say you want to:

  • Show all NFTs owned by a user

  • List all trades made by a specific wallet

  • Display historical price data

Without The Graph, you'd need to:

  1. Scan through every block in the blockchain

  2. Filter out the information you need

  3. Process and organize this data

  4. Store it somewhere for quick access

This would be:

  • Extremely slow

  • Resource-intensive

  • Expensive in terms of computation

The Graph solves all these problems by doing this work for you!

What is a Graph Node? ๐Ÿ–ฅ๏ธ

A Graph Node is like your personal data processing center. It:

  • Listens to new blockchain events

  • Processes and organizes this data

  • Stores it in a way that's easy to query

  • Serves this data to your applications

Why Run Your Own Node? ๐Ÿค˜

  1. Control: You're not dependent on third-party services

  2. Speed: Faster access to data as it's on your infrastructure

  3. Cost: Can be cheaper for high-volume applications

  4. Privacy: Your queries stay on your infrastructure

  5. Customization: You can optimize for your specific needs

Setting Up Your Own Node (Sepolia Network) ๐Ÿ› ๏ธ

Step 1: Prerequisites Checklist

  • [ ] Docker installed

  • [ ] Git installed

  • [ ] Basic command line knowledge

  • [ ] Sepolia RPC URL (from Infura or Alchemy)

  • [ ] 4GB RAM minimum recommended

  • [ ] 20GB storage space

Step 2: Environment Setup

  1. Create your project folder:
mkdir my-graph-node
cd my-graph-node
  1. Create a .env file:
# Your Sepolia RPC endpoints (always good to have backup RPCs)
ETHEREUM_RPC1=https://sepolia.infura.io/v3/YOUR-PROJECT-ID
ETHEREUM_RPC2=https://eth-sepolia.g.alchemy.com/v2/YOUR-API-KEY

# Database settings
POSTGRES_USER=graph-node
POSTGRES_PASSWORD=choose-a-strong-password
POSTGRES_DB=graph-node

# Ports
GRAPH_NODE_HTTP_PORT=8000
GRAPH_NODE_ADMIN_PORT=8020
GRAPH_NODE_INDEX_PORT=8030
GRAPH_NODE_METRICS_PORT=8040
IPFS_API_PORT=5001

Step 3: Setting Up Docker

Create docker-compose.yml:

version: '3'
services:
  graph-node:
    image: graphprotocol/graph-node:v0.35.1
    platform: linux/amd64
    ports:
      - '${GRAPH_NODE_HTTP_PORT:-8000}:8000'
      - '${GRAPH_NODE_ADMIN_PORT:-8020}:8020'
      - '${GRAPH_NODE_INDEX_PORT:-8030}:8030'
      - '${GRAPH_NODE_METRICS_PORT:-8040}:8040'
    depends_on:
      - ipfs
      - postgres
    extra_hosts:
      - host.docker.internal:host-gateway
    environment:
      postgres_host: postgres
      postgres_user: ${POSTGRES_USER:-postgres}
      postgres_pass: ${POSTGRES_PASSWORD:-postgres}
      postgres_db: ${POSTGRES_DB:-graph-node}
      ipfs: 'ipfs:5001'
      ethereum: 'sepolia:${ETHEREUM_RPC1}'
      GRAPH_LOG: info
      GRAPH_ALLOW_NON_DETERMINISTIC_IPFS: 'true'
      ETHEREUM_REORG_THRESHOLD: 50
      ETHEREUM_ANCESTOR_COUNT: 500
      ETHEREUM_POLLING_INTERVAL: 1000
      GRAPH_ETHEREUM_REQUEST_RETRIES: 10
      GRAPH_ETHEREUM_MAX_PARALLEL_BLOCKS: 10
      GRAPH_ETHEREUM_BLOCK_BATCH_SIZE: 10
      GRAPH_ETHEREUM_MAX_BLOCK_RANGE_SIZE: 1000
      GRAPH_ETHEREUM_TARGET_TRIGGERS_PER_BLOCK_RANGE: 100

  ipfs:
    image: ipfs/kubo:v0.25.0
    platform: linux/amd64
    ports:
      - '${IPFS_API_PORT:-5001}:5001'
    volumes:
      - ./data/ipfs:/data/ipfs

  postgres:
    image: postgres:16
    platform: linux/amd64
    ports:
      - '${POSTGRES_PORT:-5432}:5432'
    command:
      [
        "postgres",
        "-cshared_preload_libraries=pg_stat_statements"
      ]
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-postgres}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres}
      POSTGRES_DB: ${POSTGRES_DB:-graph-node}
      POSTGRES_INITDB_ARGS: "-E UTF8 --locale=C"
    volumes:
      - ./data/postgres:/var/lib/postgresql/data

Step 4: Starting Your Node ๐Ÿš€

  1. Create data directories:
mkdir -p data/ipfs data/postgres
  1. Start the services:
docker-compose up -d
  1. Check if everything is running:
docker-compose ps

Step 5: Verifying Your Setup โœ…

  1. Check if the Graph Node is responding:
curl http://localhost:8000/graphql
  1. Watch the logs:
docker-compose logs -f graph-node

You should see your node syncing with Sepolia blockchain!

Common Issues and Solutions ๐Ÿ”ง

1. RPC Connection Issues

Problem: "Unable to connect to ethereum node"
Solution: 
- Check if your RPC URL is correct
- Ensure you have credits/allowance on your RPC provider
- Try using a backup RPC

2. Memory Issues

Problem: "Out of memory" or container stops
Solution:
- Increase Docker memory limit
- Ensure your machine has enough free RAM
- Consider reducing block range size in config

3. IPFS Issues

Problem: "Unable to connect to IPFS"
Solution:
- Reset IPFS data: rm -rf data/ipfs/*
- Restart containers: docker-compose restart

Next Steps ๐ŸŽฏ

After your node is running:

  1. Create your first subgraph

  2. Deploy it to your node

  3. Start querying data!

Monitoring Tips ๐Ÿ“Š

Keep an eye on:

  • Node logs: docker-compose logs -f graph-node

  • System resources (CPU, RAM, Disk)

  • Sync status through the admin port

  • PostgreSQL database size

Best Practices ๐ŸŒŸ

  1. Always use multiple RPC endpoints for redundancy

  2. Backup your PostgreSQL data regularly

  3. Monitor your node's performance

  4. Keep your node updated with the latest versions

  5. Use proper security measures if exposing to the internet

Remember: Running a Graph Node is like maintaining a small data center. Take care of it, and it'll serve your data needs reliably! ๐Ÿš€

Need Help? ๐Ÿ†˜

  • Visit the Graph Protocol Discord

  • Check GitHub issues

  • Read the official documentation

  • Join The Graph community forums

Happy indexing! ๐ŸŽ‰

0
Subscribe to my newsletter

Read articles from Diluk Angelo directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Diluk Angelo
Diluk Angelo

Hey there! I'm Diluk Angelo, a Tech Lead and Web3 developer passionate about bridging the gap between traditional web solutions and the decentralized future. With years of leadership experience under my belt, I've guided teams and mentored developers in their technical journey. What really drives me is the art of transformation โ€“ taking proven Web2 solutions and reimagining them for the Web3 ecosystem while ensuring they remain scalable and efficient. Through this blog, I share practical insights from my experience in architecting decentralized solutions, leading technical teams, and navigating the exciting challenges of Web3 development. Whether you're a seasoned developer looking to pivot to Web3 or a curious mind exploring the possibilities of decentralized technology, you'll find actionable knowledge and real-world perspectives here. Expect deep dives into Web3 architecture, scalability solutions, team leadership in blockchain projects, and practical guides on transitioning from Web2 to Web3. I believe in making complex concepts accessible and sharing lessons learned from the trenches. Join me as we explore the future of the web, one block at a time!