Skip to content

Commit c1fb415

Browse files
Edits
1 parent 2145a11 commit c1fb415

File tree

1 file changed

+46
-71
lines changed

1 file changed

+46
-71
lines changed

website/src/pages/en/subgraphs/guides/subgraph-composition.mdx

Lines changed: 46 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -53,102 +53,77 @@ While the source Subgraph is a standard Subgraph, the dependent Subgraph uses th
5353
- 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)
5454

5555
Additionally, you can explore the [example-composable-subgraph](https://github.com/graphprotocol/example-composable-subgraph) repository for a working implementation of composable subgraphs
56+
## Get Started
5657

57-
### Source Subgraph
58-
59-
The source Subgraph tracks events from the Sushiswap v3 Subgraph on the Base chain. This Subgraph's configuration file is `source/subgraph.yaml`.
60-
61-
> The `source/subgraph.yaml` employs the advanced Subgraph feature, [declarative `eth_calls`](https://thegraph.com/docs/en/subgraphs/developing/creating/advanced/#declared-eth_call). To review the code for this `source/subgraph.yaml`, check out the [source Subgraph example repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph/blob/a5f13cb4b961f92d5c5631dca589c54feb1c0a19/source/subgraph.yaml).
62-
63-
### Dependent Subgraph
64-
65-
The dependent Subgraph is in the `dependent/subgraph.yaml` file, which specifies the source Subgraph as a data source. This Subgraph uses entities from the source to trigger specific actions based on changes to those entities.
58+
The following guide provides examples for defining 3 source Subgraphs to create one powerful composed Subgraph.
6659

67-
> To review the code for this `dependent/subgraph.yaml`, check out the [dependent Subgraph example repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph/blob/main/dependant/subgraph.yaml).
60+
### Specifics
6861

69-
## Get Started
62+
- 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.
63+
- The examples below show how to import and extend the schema of another Subgraph to enhance its functionality.
64+
- Each source Subgraph is optimized with a specific entity.
65+
- 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.
7066

71-
The following is a guide that illustrates how to use one Subgraph as a data source for another. This example uses:
67+
### Step 1. Deploy Block Time Source Subgraph
7268

73-
- Sushiswap v3 Subgraph on Base chain
74-
- Two Subgraphs (but you can use up to **5 source** Subgraphs in your own development).
69+
This first source Subgraph calculates the block time for each block.
7570

76-
### Step 1. Set Up Your Source Subgraph
71+
- It imports schemas from other Subgraphs and adds a `block` entity with a `timestamp` field, representing the time each block was mined.
72+
- It listens to time-related blockchain events (e.g., block timestamps) and processes this data to update the Subgraph's entities accordingly.
7773

78-
To set the source Subgraph as a data source in the dependent Subgraph, include the following in `subgraph.yaml`:
74+
To deploy this Subgraph locally, run the following commands:
7975

80-
```yaml
81-
specVersion: 1.3.0
82-
schema:
83-
file: ./schema.graphql
84-
dataSources:
85-
- kind: subgraph
86-
name: Factory
87-
network: base
88-
source:
89-
address: 'QmdXu8byAFCGSDWsB5gMQjWr6GUvEVB7S1hemfxNuomerz'
90-
startBlock: 82522
76+
```bash
77+
npm install
78+
npm run codegen
79+
npm run build
80+
npm run create-local
81+
npm run deploy-local
9182
```
9283

93-
Here, `source.address` refers to the Deployment ID of the source Subgraph, and `startBlock` specifies the block from which indexing should begin.
84+
### Step 2. Deploy Block Cost Source Subgraph
9485

95-
### Step 2. Define Handlers in Dependent Subgraph
86+
This second source Subgraph indexes the cost of each block.
9687

97-
Below is an example of defining handlers in the dependent Subgraph:
88+
#### Key Functions
9889

99-
```typescript
100-
export function handleInitialize(trigger: EntityTrigger<Initialize>): void {
101-
if (trigger.operation === EntityOp.Create) {
102-
let entity = trigger.data
103-
let poolAddressParam = Address.fromBytes(entity.poolAddress)
90+
- It imports schemas from other Subgraphs and adds a `block` entity with cost-related fields.
91+
- 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.
10492

105-
// Update pool sqrt price and tick
106-
let pool = Pool.load(poolAddressParam.toHexString()) as Pool
107-
pool.sqrtPrice = entity.sqrtPriceX96
108-
pool.tick = BigInt.fromI32(entity.tick)
109-
pool.save()
93+
To deploy this Subgraph locally, run the same commands as above.
11094

111-
// Update token prices
112-
let token0 = Token.load(pool.token0) as Token
113-
let token1 = Token.load(pool.token1) as Token
95+
### Step 3. Define Block Size in Source Subgraph
11496

115-
// Update ETH price in USD
116-
let bundle = Bundle.load('1') as Bundle
117-
bundle.ethPriceUSD = getEthPriceInUSD()
118-
bundle.save()
97+
This third source Subgraph indexes the size of each block. To deploy this Subgraph locally, run the same commands as above.
11998

120-
updatePoolDayData(entity)
121-
updatePoolHourData(entity)
99+
#### Key Functions
122100

123-
// Update derived ETH price for tokens
124-
token0.derivedETH = findEthPerToken(token0)
125-
token1.derivedETH = findEthPerToken(token1)
126-
token0.save()
127-
token1.save()
128-
}
129-
}
130-
```
101+
- It imports existing schemas from other Subgraphs and adds a `block` entity with a `size` field representing each block's size.
102+
- 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.
131103

132-
In this example, the `handleInitialize` function is triggered when a new `Initialize` entity is created in the source Subgraph, passed as `EntityTrigger<Initialize>`. The handler updates the pool and token entities based on data from the new `Initialize` entity.
104+
### Step 4. Combine Into Block Stats Subgraph
133105

134-
`EntityTrigger` has three fields:
106+
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.
135107

136-
1. `operation`: Specifies the operation type, which can be `Create`, `Modify`, or `Remove`.
137-
2. `type`: Indicates the entity type.
138-
3. `data`: Contains the entity data.
108+
> Note:
109+
>
110+
> - Any change to a source Subgraph will likely generate a new deployment ID.
111+
> - Be sure to update the deployment ID in the data source address of the Subgraph manifest to take advantage of the latest changes.
112+
> - All source Subgraphs should be deployed before the composed Subgraph is deployed.
139113
140-
Developers can then determine specific actions for the entity data based on the operation type.
114+
#### Key Functions
141115

142-
## Key Takeaways
116+
- It provides a consolidated data model that encompasses all relevant block metrics.
117+
- It combines data from 3 source Subgraphs, and provides a comprehensive view of block statistics, enabling more complex queries and analyses.
143118

144-
- Use this powerful tool to quickly scale your Subgraph development and reuse existing data.
145-
- The setup includes creating a base source Subgraph and referencing it in a dependent Subgraph.
146-
- You define handlers in the dependent Subgraph to perform actions based on changes in the source Subgraph's entities.
119+
## Key Takeaways
147120

148-
This approach unlocks composability and scalability, simplifying both development and maintenance efficiency.
121+
- This powerful tool will scale your Subgraph development and allow you to combine multiple Subgraphs.
122+
- The setup includes the deployment of 3 source Subgraphs and one final deployment of the composed Subgraph.
123+
- This feature unlocks scalability, simplifying both development and maintenance efficiency.
149124

150125
## Additional Resources
151126

152-
To use other advanced features in your Subgraph, check out [Subgraph advanced features](/developing/creating/advanced/) and [this Subgraph composition example repo](https://github.com/incrypto32/subgraph-composition-sample-subgraph).
153-
154-
To learn how to define source Subgraphs, check out [this Subgraph composition example repo](https://github.com/isum/subgraph-composition-example).
127+
- Check out all the code for this example in [this GitHub repo](https://github.com/graphprotocol/example-composable-subgraph).
128+
- To add advanced features to your Subgraph, check out [Subgraph advanced features](/developing/creating/advanced/).
129+
- To learn more about aggregations, check out [Timeseries and Aggregations](/subgraphs/developing/creating/advanced/#timeseries-and-aggregations).

0 commit comments

Comments
 (0)