快速开始

Reading time: 7 min

This guide will quickly take you through how to initialize, create, and deploy your subgraph to Subgraph Studio.

确保您的子图将从一个受支持的网络 中索引数据。

本指南是在假设您具备以下条件的情况下编写的:

  • 一个加密钱包
  • 您选择的网络上的智能合约地址

1. 在子图工作室中创建子图

链到本节

Go to the Subgraph Studio and connect your wallet.

Once your wallet is connected, you can begin by clicking “Create a Subgraph." It is recommended to name the subgraph in Title Case: "Subgraph Name Chain Name."

2. 安装 Graph CLI

链到本节

The Graph CLI is written in TypeScript and you will need to have node and either npm or yarn installed to use it. Check that you have the most recent CLI version installed.

在本地计算机上,运行以下命令之一:

Using npm:

npm install -g @graphprotocol/graph-cli@latest

Using yarn:

yarn global add @graphprotocol/graph-cli

3. Initialize your subgraph from existing contract

链到本节

Initialize your subgraph from an existing contract by running the initialize command:

graph init --studio <SUBGRAPH_SLUG>

您可以在Subgraph Studio的子图页面找到针对您特定子图的命令。

初始化子图时,CLI工具会要求您提供以下信息:

  • 协议:选择子图索引数据的协议
  • 子图段塞:为您的子图创建一个名称。您的子图段塞是子图的标识符。
  • 创建子图的目录:选择您的本地目录
  • 以太坊网络(可选):您可能需要指定子图将从哪个EVM兼容网络索引数据
  • 合约地址:找到要查询数据的智能合约地址
  • ABI:如果ABI不是自动填充的,则需要将其手动输入为JSON文件
  • 起始区块:建议您在子图索引区块链数据时输入起始区块以节省时间。您可以通过查找部署合约区块来定位起始区块。
  • 合约名称:输入您的合约名称
  • 将合约事件作为实体进行索引:建议您将其设置为true,因为它将自动为每个发出的事件向子图添加映射
  • 添加其他合约(可选):您可以添加其他合约

请参阅下面的屏幕截图,以获取初始化子图时所需的示例:

Subgraph command

4. Write your subgraph

链到本节

前面的命令创建了一个原始子图,可以将其用作构建子图的起点。当对子图进行更改时,将主要使用三个文件:

  • Manifest (subgraph.yaml) - The manifest defines what datasources your subgraphs will index.
  • Schema (schema.graphql) - The GraphQL schema defines what data you wish to retrieve from the subgraph.
  • AssemblyScript Mappings (mapping.ts) - This is the code that translates data from your datasources to the entities defined in the schema.

For more information on how to write your subgraph, see Creating a Subgraph.

5. Deploy to Subgraph Studio

链到本节

一旦您的子图被编写好,请运行以下命令:

$ graph codegen
$ graph build
  • 认证并部署子图。部署密钥可以在子图工作室的子图页面上找到。
$ graph auth --studio <DEPLOY_KEY>
$ graph deploy --studio <SUBGRAPH_SLUG>

您将被要求输入一个版本标签。强烈建议使用(语义化版本)semver 进行版本控制,如0.0.1。尽管如此,您可以自由选择任何字符串作为版本,比如:v1version1asdf

6. 测试子图

链到本节

In Subgraph Studio's playground environment, you can test your subgraph by making a sample query.

日志会告诉你你的子图是否有任何错误。操作子图的日志如下所示:

Subgraph logs

如果子图失败了,可以通过使用 GraphiQL Playground查询子图的健康状况。注意,你可以利用下面的查询,输入你的子图的部署 ID。在这种情况下,Qm... 是部署 ID(可以在子图页面的详细信息下找到)。下面的查询会提示,当一个子图失败时,则可进行相应调试。

{
indexingStatuses(subgraphs: ["Qm..."]) {
node
synced
health
fatalError {
message
block {
number
hash
}
handler
}
nonFatalErrors {
message
block {
number
hash
}
handler
}
chains {
network
chainHeadBlock {
number
}
earliestBlock {
number
}
latestBlock {
number
}
lastHealthyBlock {
number
}
}
entityCount
}
}

7. Publish your subgraph to The Graph’s Decentralized Network

链到本节

Once your subgraph has been deployed to Subgraph Studio, you have tested it out, and you are ready to put it into production, you can then publish it to the decentralized network.

In Subgraph Studio, you will be able to click the publish button on the top right of your subgraph's page.

选择您希望发布子图的网络。建议将子图发布到 Arbitrum One,以利用更快的交易速度和更低的Gas费用

The (upgrade Indexer)[/sunrise/#about-the-upgrade-indexer] will begin serving queries on your subgraph regardless of subgraph curation, and it will provide you with 100,000 free queries per month.

For a higher quality of service and stronger redundancy, you can curate your subgraph to attract more Indexers. At the time of writing, it is recommended that you curate your own subgraph with at least 3,000 GRT to ensure 3-5 additional Indexers begin serving queries on your subgraph.

为了节省gas成本,您可以在将子图发布到Graph的去中心化网络时选择此按钮,在发布子图的同一交易中策展子图:

Subgraph publish

8. Query your subgraph

链到本节

现在,您可以通过将GraphQL查询发送到子图的查询URL来查询子图,您可以单击查询按钮找到该查询URL。

If you don't have your API key, you can query via the free, rate-limited development query URL, which can be used for development and staging.

For more information about querying data from your subgraph, read more here.

编辑

上页
L2转移工具指南
下页
Subgraphs
编辑