|
| 1 | +--- |
| 2 | +title: Aggregate Data Using Subgraph Composition |
| 3 | +sidebarTitle: 'Build a Composable Subgraph with Multiple Subgraphs' |
| 4 | +--- |
| 5 | + |
| 6 | +Optimize your Subgraph by merging data from three independent, source Subgraphs into a single composable Subgraph to enhance data aggregation. |
| 7 | + |
| 8 | +> Important Reminders: |
| 9 | +> |
| 10 | +> - Subgraph composition is built into the CLI, and you can deploy with [Subgraph Studio](https://thegraph.com/studio/). |
| 11 | +> - This feature requires `specVersion` 1.3.0. |
| 12 | +
|
| 13 | +## Overview |
| 14 | + |
| 15 | +Subgraph composition empowers you to use one Subgraph as a data source for another, allowing it to consume and respond to entity changes. Instead of fetching onchain data directly, a Subgraph can listen for updates from another Subgraph and react to changes. This is useful for aggregating data from multiple Subgraphs or triggering actions based on external updates. |
| 16 | + |
| 17 | +## Prerequisites |
| 18 | + |
| 19 | +To deploy **all** Subgraphs locally, you must have the following: |
| 20 | + |
| 21 | +- A [Graph Node](https://github.com/graphprotocol/graph-node) instance running locally |
| 22 | +- An [IPFS](https://docs.ipfs.tech/) instance running locally |
| 23 | +- [Node.js](https://nodejs.org) and npm |
| 24 | + |
| 25 | +## Get Started |
| 26 | + |
| 27 | +The following guide provides examples for defining three source Subgraphs to create one powerful composed Subgraph. |
| 28 | + |
| 29 | +### Specifics |
| 30 | + |
| 31 | +- 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. |
| 32 | +- The examples below show how to import and extend the schema of another Subgraph to enhance its functionality. |
| 33 | +- Each source Subgraph is optimized with a specific entity. |
| 34 | +- 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. |
| 35 | + |
| 36 | +### Step 1. Deploy Block Time Source Subgraph |
| 37 | + |
| 38 | +This first source Subgraph calculates the block time for each block. |
| 39 | + |
| 40 | +- It imports schemas from other Subgraphs and adds a `block` entity with a `timestamp` field, representing the time each block was mined. |
| 41 | +- It listens to time-related blockchain events (e.g., block timestamps) and processes this data to update the Subgraph's entities accordingly. |
| 42 | + |
| 43 | +To deploy this Subgraph locally, run the following commands: |
| 44 | + |
| 45 | +```bash |
| 46 | +npm install |
| 47 | +npm run codegen |
| 48 | +npm run build |
| 49 | +npm run create-local |
| 50 | +npm run deploy-local |
| 51 | +``` |
| 52 | + |
| 53 | +### Step 2. Deploy Block Cost Source Subgraph |
| 54 | + |
| 55 | +This second source Subgraph indexes the cost of each block. |
| 56 | + |
| 57 | +#### Key Functions |
| 58 | + |
| 59 | +- It imports schemas from other Subgraphs and adds a `block` entity with cost-related fields. |
| 60 | +- 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. |
| 61 | + |
| 62 | +To deploy this Subgraph locally, run the same commands as above. |
| 63 | + |
| 64 | +### Step 3. Define Block Size in Source Subgraph |
| 65 | + |
| 66 | +This third source Subgraph indexes the size of each block. To deploy this Subgraph locally, run the same commands as above. |
| 67 | + |
| 68 | +#### Key Functions |
| 69 | + |
| 70 | +- It imports existing schemas from other Subgraphs and adds a `block` entity with a `size` field representing each block's size. |
| 71 | +- 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. |
| 72 | + |
| 73 | +### Step 4. Combine Into Block Stats Subgraph |
| 74 | + |
| 75 | +This composed Subgraph combines and aggregates the information from the three source Subgraphs above, providing a unified view of block statistics. To deploy this Subgraph locally, run the same commands as above. |
| 76 | + |
| 77 | +> Note: |
| 78 | +> |
| 79 | +> - Any change to a source Subgraph will likely generate a new deployment ID. |
| 80 | +> - Be sure to update the deployment ID in the data source address of the Subgraph manifest to take advantage of the latest changes. |
| 81 | +> - All source Subgraphs should be deployed before the composed Subgraph is deployed. |
| 82 | +
|
| 83 | +#### Key Functions |
| 84 | + |
| 85 | +- It provides a consolidated data model that encompasses all relevant block metrics. |
| 86 | +- It combines data from three source Subgraphs, and provides a comprehensive view of block statistics, enabling more complex queries and analyses. |
| 87 | + |
| 88 | +## Key Takeaways |
| 89 | + |
| 90 | +- This powerful tool will scale your Subgraph development and allow you to combine multiple Subgraphs. |
| 91 | +- The setup includes the deployment of three source Subgraphs and one final deployment of the composed Subgraph. |
| 92 | +- This feature unlocks scalability, simplifying both development and maintenance efficiency. |
| 93 | + |
| 94 | +## Additional Resources |
| 95 | + |
| 96 | +- Check out all the code for this example in [this GitHub repo](https://github.com/isum/subgraph-composition-example). |
| 97 | +- To add advanced features to your Subgraph, check out [Subgraph advanced features](/developing/creating/advanced/). |
| 98 | +- To learn more about aggregations, check out [Timeseries and Aggregations](/subgraphs/developing/creating/advanced/#timeseries-and-aggregations). |
0 commit comments