关于 Graph
Reading time: 5 min
本页将解释什么是 Graph,以及你将如何开始。
Graph 是一个去中心化的协议,用于索引和查询区块链的数据。 它使查询那些难以直接查询的数据成为可能。
像 这样具有复杂智能合约的项目,以及像 这样的 NFTs 倡议,都在以太坊区块链上存储数据,因此,除了直接从区块链上读取基本数据外,真的很难。
In the case of Bored Ape Yacht Club, we can perform basic read operations on like getting the owner of a certain Ape, getting the content URI of an Ape based on their ID, or the total supply. This can be done because these read operations are programmed directly into the smart contract. However, more advanced real-world queries and operations like aggregation, search, relationships, and non-trivial filtering are not possible. For example, if we wanted to query for Apes that are owned by a certain address and filter by one of its characteristics, we would not be able to get that information by interacting directly with the contract itself.
To get this data, you would have to process every single event ever emitted, read the metadata from IPFS using the Token ID and IPFS hash, and then aggregate it. It would take hours or even days for a decentralized application (dapp) running in a browser to get an answer to these simple questions.
你也可以建立你自己的服务器,在那里处理交易,把它们保存到数据库,并在上面建立一个 API 终端,以便查询数据。 然而,这种选择是,需要维护,会出现单点故障,并破坏了去中心化化所需的重要安全属性。
为区块链数据编制索引真的非常非常难。
Blockchain properties like finality, chain reorganizations, or uncled blocks complicate this process further. They make it time consuming and conceptually hard to retrieve correct query results from blockchain data.
The Graph provides a solution with a decentralized protocol that indexes and enables the efficient and high-performance querying of blockchain data. These APIs (indexed "subgraphs") can then be queried with a standard GraphQL API. Today, there is a hosted service as well as a decentralized protocol with the same capabilities. Both are backed by the open source implementation of .
Graph基于子图描述(称为子图清单)学习如何索引以太坊数据。子图描述定义了子图感兴趣的智能合约、要注意的合约中的事件,以及如何将事件数据映射到Graph将存储在其数据库中的数据。
一旦编写了子图清单
,就可以使用Graph CLI将定义存储在IPFS中,并告诉索引人开始为该子图的数据编制索引。
此图提供了部署子图清单后用于处理以太坊交易的数据流的更多细节
流程遵循这些步骤:
- 一个去中心化的应用程序通过智能合约上的交易向以太坊添加数据。
- 智能合约在处理交易时,会发出一个或多个事件。
- Graph 节点不断扫描以太坊的新区块和它们可能包含的子图的数据。
- Graph 节点在这些区块中为你的子图找到以太坊事件并运行你提供的映射处理程序。 映射是一个 WASM 模块,它创建或更新 Graph 节点存储的数据实体,以响应以太坊事件。
- 去中心化的应用程序使用Graph节点的,从区块链的索引中查询 Graph 节点的数据。 Graph 节点反过来将 GraphQL 查询转化为对其底层数据存储的查询,以便利用存储的索引功能来获取这些数据。 去中心化的应用程序在一个丰富的用户界面中为终端用户显示这些数据,他们用这些数据在以太坊上发行新的交易。 就这样周而复始。
The following sections provide more detail on how to define subgraphs, how to deploy them, and how to query data from the indexes that Graph Node builds.
Before you start writing your own subgraph, you might want to have a look at and explore some of the subgraphs that have already been deployed. The page for each subgraph contains a playground that lets you query that subgraph's data with GraphQL.