Graph
GRAPH
1. WHY THE GRAPH EXISTS (Root Problem)
Ethereum RPC nodes allow only low-level reads:
-
eth_getBalance -
eth_call -
eth_getLogs -
getBlockByNumber
But if you want something like:
“Give me a list of all swaps where user=0xA”
Your only option is to:
❌ scan all blocks
❌ fetch all events
❌ filter in your backend
❌ store in your database
❌ query manually
If you need:
-
token history
-
all NFT transfers
-
Uniswap swaps
-
All LP positions
You’d need to scan millions of blocks, slow and expensive.
So The Graph solves this.
⭐ 2. HOW THE GRAPH WORKS INTERNALLY
The Graph has three big components:
✔ Graph Node
Scans blockchain → extracts events → writes to your database.
✔ Subgraph Manifest
Tells Graph Node what to index.
✔ Mappings
Code (AssemblyScript) that transforms raw blockchain data → structured entities.
⭐ 3. WHAT EXACTLY IS A SUBGRAPH?
A subgraph is:
A definition file that tells Graph Node which smart contracts to watch,
which events to index,
and how to store the extracted data.
Your subgraph contains:
| File | Purpose |
|---|---|
subgraph.yaml | What to index (contracts, events, start block) |
schema.graphql | Database schema (entities you store) |
mapping.ts | Code that processes events and stores them |
These three pieces create an indexer, like a custom database.
⭐ 4. LIFE OF A SUBGRAPH (ACTUAL INTERNAL FLOW)
Let’s say you have a UniswapPool subgraph.
Step 1: You write subgraph.yaml
Example:
This tells Graph Node:
“Watch this contract starting from block 12,345,678
and callhandleSwap()for every Swap event.”
Step 2: You define your schema in schema.graphql
This creates your database table.
Step 3: You write your mapping in AssemblyScript (WASM)
mapping.ts:
Graph Node compiles this into WebAssembly, not JS.
Step 4: Graph Node begins indexing
Graph Node does this:
This takes hours for big subgraphs (Uniswap, Aave).
Once indexing is done →
your subgraph is instantly queryable.
Step 5: You query using GraphQL
Example:
Graph Node responds instantly — because it's reading from PostgreSQL, not blockchain.
⭐ 5. WHAT IS HAPPENING BEHIND THE SCENES?
This is the deep part.
✔ The Graph runs a full Ethereum node
Gets every block, tx, log, receipt.
✔ Graph Node decodes logs using ABI
Smart contract events emit logs: topics + data.
Graph Node uses ABI to decode:
✔ Mapping handlers run inside WASM sandbox
Every event is passed into your code.
✔ Entities stored in Graph Database
Uses PostgreSQL with strong indexing.
✔ GraphQL layer sits on top of the database
Optimized for fast searchable queries.
⭐ 6. WHY SUBGRAPHS MAKE DATA FAST?
Instead of scanning chain every time:
❌ RPC nodes re-scan millions of blocks → slow
❌ Logs must be decoded repeatedly → slow
❌ You must process in backend → slow
With subgraphs:
✔ events are indexed once — forever
✔ stored in organized DB tables
✔ GraphQL queries are extremely fast
✔ filtering and pagination built-in
✔ historical data instantly accessible
This is why Uniswap, Aave, Balancer all rely on subgraphs.
⭐ 7. HOW MULTIPLE CONTRACTS ARE INDEXED TOGETHER?
You can add multiple dataSources in the subgraph:
Mappings can reference each other.
This allows:
✔ LP positions
✔ Reward calculations
✔ User portfolio
✔ Token prices
✔ Swap history
⭐ 8. IMPORTANT CONCEPT: ENTITY RELATIONSHIPS
GraphQL schema allows relations:
This creates SQL-like joins but decentralized.
⭐ 9. START BLOCK IS IMPORTANT
If you set:
Graph Node will index the entire chain → slow.
If your contract was deployed at:
Always set this:
Saves DAYS.
⭐ 10. FULL INTERNAL ARCHITECTURE OVERVIEW
⭐ 11. Why This Replaces Querying Ethereum
Network query becomes:
Instead of:
⭐ 12. Real Example: How Uniswap v3 Uses The Graph
You can query:
-
top pools
-
top tokens
-
swaps
-
LP fees
-
TVL
-
user positions
all within milliseconds — subgraph precomputed everything.
⭐ FINAL SUMMARY (SUPER SIMPLE)
“Subgraph = Custom blockchain database
built by converting contract events into structured entities
indexed by Graph Node, queryable with GraphQL.”
๐️ Who Created The Graph?
So:
✅ Initially built by a company (Edge & Node).
✅ Now an open-source, decentralized protocol governed by token holders (GRT).
✅ Step 1: What The Graph Is — Simple Summary
The Graph = Google Search Index + API Layer for Blockchain Data.
Blockchains store all data, but not optimized for searching/querying.
Example: Uniswap wants to show all swaps where WETH was traded → Ethereum can't query like that directly — you'd have to scan millions of blocks manually.
The Graph solves this:
๐ Summary: Manual vs The Graph
๐งฑ Layered Stack of a dApp (Example: Uniswap)
๐ Real Flow: How Uniswap Uses Alchemy & The Graph Together
๐ Scenario: User opens Uniswap and selects a token pair
✅ So How Does Uniswap Actually Use Both?
[ User Interface ]
|
| ① Live state (e.g. reserves, prices, balances)
↓
[ Alchemy RPC Node ] <-- (eth_call, getReserves, block number, etc.)
|
| ② Historical / aggregated data (e.g. past swaps, volume history)
↓
[ The Graph Subgraph ] <-- pre-indexed swap events turned into GraphQL API
๐ง Key Insight:
Alchemy gives you RAW ACCESS.
The Graph gives you ORGANIZED ACCESS.
You could get swap events directly from Alchemy using getLogs,
but you'd still need to decode, store, and format everything yourself.
The Graph does that work for you automatically.
๐งช Real Example — Uniswap Swap Page
Let’s say you visit info.uniswap.org/pair/WETH-USDC
Comments
Post a Comment