Skip to content

Commit f66613a

Browse files
Publish composable subgraphs (#919)
* Init * Edits * Fix * Update website/src/pages/en/subgraphs/guides/subgraph-composition.mdx Co-authored-by: Idalith <[email protected]> * Feedback --------- Co-authored-by: Idalith <[email protected]>
1 parent 0c37875 commit f66613a

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

website/src/pages/en/subgraphs/guides/_meta.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export default {
2+
'subgraph-composition': '',
23
'subgraph-debug-forking': '',
34
near: '',
45
arweave: '',
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
---
2+
title: Aggregate Data Using Subgraph Composition
3+
sidebarTitle: 'Build a Composable Subgraph with Multiple Subgraphs'
4+
---
5+
6+
Leverage Subgraph composition to speed up development time. Create a base Subgraph with essential data, then build additional Subgraphs on top of it.
7+
8+
Optimize your Subgraph by merging data from independent, source Subgraphs into a single composable Subgraph to enhance data aggregation.
9+
10+
## Introduction
11+
12+
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.
13+
14+
### Benefits of Composition
15+
16+
Subgraph composition is a powerful feature for scaling, allowing you to:
17+
18+
- Reuse, mix, and combine existing data
19+
- Streamline development and queries
20+
- Use multiple data sources (up to five source Subgraphs)
21+
- Speed up your Subgraph's syncing speed
22+
- Handle errors and optimize the resync
23+
24+
## Architecture Overview
25+
26+
The setup for this example involves two Subgraphs:
27+
28+
1. **Source Subgraph**: Tracks event data as entities.
29+
2. **Dependent Subgraph**: Uses the source Subgraph as a data source.
30+
31+
You can find these in the `source` and `dependent` directories.
32+
33+
- The **source Subgraph** is a basic event-tracking Subgraph that records events emitted by relevant contracts.
34+
- The **dependent Subgraph** references the source Subgraph as a data source, using the entities from the source as triggers.
35+
36+
While the source Subgraph is a standard Subgraph, the dependent Subgraph uses the Subgraph composition feature.
37+
38+
## Prerequisites
39+
40+
### Source Subgraphs
41+
42+
- 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)
43+
- See notes here: https://github.com/graphprotocol/graph-node/releases/tag/v0.37.0
44+
- Immutable entities only: All Subgraphs must have [immutable entities](https://thegraph.com/docs/en/subgraphs/best-practices/immutable-entities-bytes-as-ids/#immutable-entities) when the Subgraph is deployed
45+
- Pruning can be used in the source Subgraphs, but only entities that are immutable can be composed on top of
46+
- Source Subgraphs cannot use grafting on top of existing entities
47+
- Aggregated entities can be used in composition, but entities that are composed from them cannot performed additional aggregations directly
48+
49+
### Composed Subgraphs
50+
51+
- You can only compose up to a **maximum of 5 source Subgraphs**
52+
- Composed Subgraphs can only use **datasources from the same chain**
53+
- **Nested composition is not yet supported**: Composing on top of another composed Subgraph isn’t allowed at this time
54+
- Aggregated entities can be used in composition, but the composed entities on them cannot also use aggregations directly
55+
- 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)
56+
57+
Additionally, you can explore the [example-composable-subgraph](https://github.com/graphprotocol/example-composable-subgraph) repository for a working implementation of composable Subgraphs
58+
59+
## Get Started
60+
61+
The following guide provides examples for defining 3 source Subgraphs to create one powerful composed Subgraph.
62+
63+
### Specifics
64+
65+
- To keep this example simple, all source Subgraphs use only block handlers. However, in a real environment, each source Subgraph will use data from different smart contracts.
66+
- The examples below show how to import and extend the schema of another Subgraph to enhance its functionality.
67+
- Each source Subgraph is optimized with a specific entity.
68+
- All the commands listed install the necessary dependencies, generate code based on the GraphQL schema, build the Subgraph, and deploy it to your local Graph Node instance.
69+
70+
### Step 1. Deploy Block Time Source Subgraph
71+
72+
This first source Subgraph calculates the block time for each block.
73+
74+
- It imports schemas from other Subgraphs and adds a `block` entity with a `timestamp` field, representing the time each block was mined.
75+
- It listens to time-related blockchain events (e.g., block timestamps) and processes this data to update the Subgraph's entities accordingly.
76+
77+
To deploy this Subgraph locally, run the following commands:
78+
79+
```bash
80+
npm install
81+
npm run codegen
82+
npm run build
83+
npm run create-local
84+
npm run deploy-local
85+
```
86+
87+
### Step 2. Deploy Block Cost Source Subgraph
88+
89+
This second source Subgraph indexes the cost of each block.
90+
91+
#### Key Functions
92+
93+
- It imports schemas from other Subgraphs and adds a `block` entity with cost-related fields.
94+
- It listens to blockchain events related to costs (e.g. gas fees, transaction costs) and processes this data to update the Subgraph's entities accordingly.
95+
96+
To deploy this Subgraph locally, run the same commands as above.
97+
98+
### Step 3. Define Block Size in Source Subgraph
99+
100+
This third source Subgraph indexes the size of each block. To deploy this Subgraph locally, run the same commands as above.
101+
102+
#### Key Functions
103+
104+
- It imports existing schemas from other Subgraphs and adds a `block` entity with a `size` field representing each block's size.
105+
- It listens to blockchain events related to block sizes (e.g., storage or volume) and processes this data to update the Subgraph's entities accordingly.
106+
107+
### Step 4. Combine Into Block Stats Subgraph
108+
109+
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.
110+
111+
> Note:
112+
>
113+
> - Any change to a source Subgraph will likely generate a new deployment ID.
114+
> - Be sure to update the deployment ID in the data source address of the Subgraph manifest to take advantage of the latest changes.
115+
> - All source Subgraphs should be deployed before the composed Subgraph is deployed.
116+
117+
#### Key Functions
118+
119+
- It provides a consolidated data model that encompasses all relevant block metrics.
120+
- It combines data from 3 source Subgraphs, and provides a comprehensive view of block statistics, enabling more complex queries and analyses.
121+
122+
## Key Takeaways
123+
124+
- This powerful tool will scale your Subgraph development and allow you to combine multiple Subgraphs.
125+
- The setup includes the deployment of 3 source Subgraphs and one final deployment of the composed Subgraph.
126+
- This feature unlocks scalability, simplifying both development and maintenance efficiency.
127+
128+
## Additional Resources
129+
130+
- Check out all the code for this example in [this GitHub repo](https://github.com/graphprotocol/example-composable-subgraph).
131+
- To add advanced features to your Subgraph, check out [Subgraph advanced features](/developing/creating/advanced/).
132+
- To learn more about aggregations, check out [Timeseries and Aggregations](/subgraphs/developing/creating/advanced/#timeseries-and-aggregations).

0 commit comments

Comments
 (0)