7 分钟
使用子图合成的集成数据
利用子图构成来加快开发时间。创建基础子图,带有基本数据,然后在它上方构建附加子图。
Optimize your Subgraph by merging data from independent, source Subgraphs into a single composable Subgraph to enhance data aggregation.
介绍
Composable Subgraphs enable you to combine multiple Subgraphs’ data sources into a new Subgraph, facilitating faster and more flexible Subgraph development. Subgraph composition empowers you to create and maintain smaller, focused Subgraphs that collectively form a larger, interconnected dataset.
合成的好处
子图合成是一个强大的功能,可以缩放:
- 重新使用、混合和合并现有数据
- 简化开发和查询
- 使用多个数据源 (最多五个源子图)
- 加快子图同步速度
- 处理错误并优化重新同步
架构概述
此示例的设置涉及两个子图:
- 源子图:跟踪事件数据作为实体。
- 依赖子图:使用源子图作为数据源。
你可以在 source
和 dependent
目录中找到。
- 源子图 是一个基本的事件跟踪子图,记录相关合约发布的事件。
- 依赖子图 将源子图作为数据源,将源实体作为触发器。
源子图是标准子图,依赖子图则使用子图合成功能。
先决条件
Source Subgraphs
- All Subgraphs need to be published with a specVersion 1.3.0 or later (Use the latest graph-cli version to be able to deploy composable Subgraphs)
- See notes here: https://github.com/graphprotocol/graph-node/releases/tag/v0.37.0
- Immutable entities only: All Subgraphs must have immutable entities when the Subgraph is deployed
- Pruning can be used in the source Subgraphs, but only entities that are immutable can be composed on top of
- Source Subgraphs cannot use grafting on top of existing entities
- Aggregated entities can be used in composition, but entities that are composed from them cannot performed additional aggregations directly
Composed Subgraphs
- You can only compose up to a maximum of 5 source Subgraphs
- Composed Subgraphs can only use datasources from the same chain
- Nested composition is not yet supported: Composing on top of another composed Subgraph isn’t allowed at this time
- Aggregated entities can be used in composition, but the composed entities on them cannot also use aggregations directly
- Developers cannot compose an onchain datasource with a Subgraph datasource (i.e. you can’t do normal event handlers and call handlers and block handlers in a composed Subgraph)
Additionally, you can explore the example-composable-subgraph repository for a working implementation of composable Subgraphs
开始
The following guide provides examples for defining 3 source Subgraphs to create one powerful composed Subgraph.
详情
- 为了保持示例简单,所有源子图只使用区块处理器。 然而,在实际环境中,每个源子图将使用不同的智能合约提供的数据。
- 下面的例子显示了如何导入和扩展另一个子图的模式以增强其功能。
- 每个源子图由一个特定实体优化。
- 列出的所有命令都安装了必要的依赖,基于GraphQL 模式生成代码。 构建子图并将其部署到您的本地Graph节点实例。
第 1 步:部署源子图区块时间
第一个源子图计算每个区块的区块时间。
- 它从其它子图中导入方案并添加一个带有“时间戳”字段的
block
实体,这代表了每个区块被开采的时间。 - 它监听与时间相关的区块链事件(例如区块时间戳),并处理此数据以相应地更新子图的实体。
要在本地部署此子图,请运行以下命令:
1npm install2npm run codegen3npm run build4npm run create-local5npm run deploy-local
第 2 步:部署源子图区块时间
第二个源子图将每个区块的成本指数化。
关键函数
- 从其它子图中导入模式并添加一个含有成本相关字段的
block
实体。 - 监听与成本相关的区块链事件(例如燃气费、交易成本),并处理此数据以相应更新子图的实体。
要在本地部署此子图,请运行以下命令。
第 3 步:定义源子图中区块大小
第三个源子图索引每个块的大小。要在本地部署此子图,请运行与上面相同的命令。
关键函数
- 从其它子图中导入现有的模式并添加一个
block
实体,其中包含一个size
字段,代表每个区块的大小。 - 监听与区块大小相关的区块链事件(例如存储或体积),并处理此数据以相应地更新子图的实体。
第 4 步:合并进区块统计子图
This composed Subgraph combines and aggregates the information from the source Subgraphs above, providing a unified view of block statistics. To deploy this Subgraph locally, run the same commands as above.
注意:
- 对源子图的任何更改都可能生成一个新的部署ID。
- 请务必更新子图数据源地址中的部署 ID 来利用最新的更改。
- 所有源子图均应在部署合成子图之前部署。
关键函数
- 它提供了一个综合数据模型,其中包括所有相关的区块计量。
- It combines data from 3 source Subgraphs, and provides a comprehensive view of block statistics, enabling more complex queries and analyses.
要点前瞻
- 这个强大的工具将缩放子图的开发,并允许您合并多个子图。
- The setup includes the deployment of 3 source Subgraphs and one final deployment of the composed Subgraph.
- 这个功能解锁了可扩展性,简化了开发和维护效率。
其他资源
- Check out all the code for this example in this GitHub repo.
- 若要将高级功能添加到您的子图,请参阅子图高级功能。
- 要了解更多关于聚合的信息,请查看 Timeseries and Aggreg。