We’ve learned that developers can use The Graph to index and query data that is stored on a blockchain such as Ethereum. But how does it all work?
Subgraph descriptions are teaching The Graph what data to index and how to do so. These descriptions are known as the subgraph manifest. Within the subgraph manifest, the following is defined:
Defining smart contracts of interest for the subgraph
Events in the smart contract to monitor
How to map event data to data that will be stored in the database
Once a developer has created a subgraph manifest, they can use The Graph CLI to store it in IPFS. This tells the hosted service to start indexing data for the subgraph.
Each step of the data flow.
Data is added to a blockchain through a transaction on a smart contract by a decentralized application
While processing the transaction, one or more events are emitted by the smart contract
New blocks on the blockchain are continuously scanned by The Graph Node.
4. Event discovery
If a block contains events for a subgraph, The Graph Node runs the provided mapping handlers. The data entities stored in response to blockchain events are mapped by a WASM module.
dApps query The Graph Node by using the GraphQL endpoint. Queries are translated into queries for the underlying data store. After this, the data is fetched by utilizing the store’s indexing capabilities.
The queried data is displayed for end-users through the UI of the decentralized application. The user interface is then used for creating new transactions on the blockchain.
The central piece to indexing data from a blockchain through The Graph is the subgraph manifest. It defines which subgraphs and which events therein to monitor and how to map the data.
Transactions on the blockchain trigger the process of creating data entities that are stored by The Graph Node. The data is then queried by dApps and displayed through their UI.