返回知识库
Day 26

Day 26:The Graph 与 Subgraph 原理

理解 The Graph 索引层、Subgraph 架构与 GraphQL 查询,实操查询 Uniswap Subgraph 并产出 GraphQL 查询笔记

2025-02-05
The GraphSubgraphGraphQL索引UniswapWeek4

Day 26: The Graph - Subgraph 原理

Week 4 学习路径

Week 4: NFT与新范式
├── Day 22–25: NFT / 账户抽象 ✅
├── Day 26: The Graph Subgraph 原理 ✅ ← 今天
├── Day 27: 集成 Subgraph 到项目
└── Day 28: Uniswap 产品分析文章

核心概念

为什么需要 The Graph?

链上数据是「事件流」:区块、交易、日志。DApp 若直接扫链会非常慢且贵。The Graph 把链上数据索引成可按实体查询的 GraphQL API,前端/后端只关心「查什么」,不关心「怎么从链上扫」。

方式特点
直接读链 (RPC)要过滤事件、分页、聚合,逻辑重、慢
The Graph (Subgraph)按 Schema 查实体,一次请求拿聚合好的数据

一句话:Subgraph = 链上数据的「可查询视图」,用 GraphQL 按业务模型取数。

Subgraph 是什么?

  • Subgraph:针对某协议/合约的「索引定义」+ 部署后的查询端点。
  • 组成
  • Schema(GraphQL):定义实体(Entity),如 PoolSwapToken
  • Manifest:数据源(合约、网络、要索引的事件)。
  • Mapping:把链上事件/调用写成 Handler,写入 Schema 里的实体。
  • 流程:链上出块 → Graph 节点监听事件 → Mapping 更新实体 → 用户用 GraphQL 查询。
链上事件 → Indexer 执行 Mapping → 写入实体存储
                                    ↓
前端/后端 ← GraphQL 查询 ← Subgraph API

GraphQL 和 REST 的差别(PM 视角)

维度RESTGraphQL
请求一个 URL 一种资源一个端点,查询里声明要的字段
多资源多次请求或后端拼装一次请求多实体、多字段
版本常靠 v1/v2 URLSchema 演进,按需查字段

前端只需「我要 Pool 的 id、token0/token1 的 symbol、最近 10 笔 swap」→ 一条 GraphQL 查出来。


知识点:Uniswap Subgraph 怎么查

端点(需 API Key)

The Graph Studio 注册后使用 Gateway:

  • Uniswap V3 Ethereum: https://gateway.thegraph.com/api//subgraphs/id/5zvR82QoaXYFyDEKLZ9t6v9adgnptxYpKpSbxtgVENFV
  • Uniswap V2: 在 Explorer 或文档中查对应 Subgraph ID。

(也常见「未认证」的公共端点,文档会写;生产建议用 API Key 控制用量。)

示例查询(可写在笔记里)

1. 协议级统计(若有对应 Schema)

query ProtocolStats {
  uniswapFactory(id: "0x1F98431c8aD98523631AE4a59f267346ea31F984") {
    poolCount
    totalVolumeUSD
    txCount
  }
}

2. 池子列表(分页)

query Pools($first: Int!, $skip: Int!) {
  pools(first: $first, skip: $skip, orderBy: totalValueLockedUSD, orderDirection: desc) {
    id
    token0 { id symbol decimals }
    token1 { id symbol decimals }
    feeTier
    totalValueLockedUSD
    volumeUSD
  }
}

变量: { "first": 10, "skip": 0 }

3. 某池子最近 Swap

query PoolSwaps($pool: String!) {
  swaps(first: 20, orderBy: timestamp, orderDirection: desc, where: { pool: $pool }) {
    id
    timestamp
    amount0
    amount1
    amountUSD
    sender
  }
}

(实际字段名以 Uniswap 官方 Subgraph Schema 为准,以上为常见模式。)

实操建议

  • 打开 Uniswap Subgraph 文档The Graph Explorer,找到当前推荐的 Subgraph(如 V2/V3 某网络)。
  • 在 Explorer 的 Playground 里运行上述类型的查询,把「请求 + 变量 + 返回字段」记进笔记。

链上/实操 (约 1h)


项目开发 (约 2h)

  • [ ] 完成上述 GraphQL 查询笔记(可含截图或 curl/Postman 示例)。
  • [ ] 可选:在 momoweb3 里用 fetchgraphql-request 调一次 Subgraph,把结果打印或简单展示(为 Day 27 集成做准备)。

产出要求

GraphQL 查询笔记(必须)

包含至少:

1. Subgraph 是什么:用 2–3 句话 + 一张流程图(链上→索引→GraphQL)。

2. Uniswap 使用的端点:你实际用的 URL(可打码 API Key)。

3. 2 个以上查询:完整 GraphQL + 变量 + 返回字段说明或示例。

4. 对比:和直接读链或 Dune 查同一类数据时的差异(速度、复杂度、适用场景)。


学习资源

资源链接
The Graph 文档https://thegraph.com/docs/
The Graph GraphQL APIhttps://thegraph.com/docs/en/subgraphs/querying/graphql-api/
Uniswap Subgraph 概览https://docs.uniswap.org/api/subgraph/overview
Uniswap 查询示例https://docs.uniswap.org/api/subgraph/guides/examples
Uniswap V4 示例https://docs.uniswap.org/api/subgraph/guides/v4-examples
The Graph Explorerhttps://thegraph.com/explorer

面试题准备

Q: 为什么很多 DeFi 前端用 The Graph 而不是直接读链?

  • 30 秒:链上只有事件和状态,按「池子、交易、用户」聚合和分页要自己扫块和算,慢且贵。The Graph 把链上数据索引成 GraphQL,按业务实体查询,一次请求拿聚合结果,开发简单、体验好。
  • 延伸:可提 Subgraph 的 Schema、Mapping、以及和 Dune(SQL 分析)的定位差异——The Graph 偏「产品/前端取数」,Dune 偏「分析/看板」。

明日预告

Day 27:把 Subgraph 集成到 momoweb3 项目(如 ETH 价格或池子数据从 Graph 拉取),代码提交。