Subgraphs
Getting historical data on a smart contract can be frustrating when you’re building a dApp. The Graph provides an easy way to query smart contract data through APIs known as subgraphs. The Graph’s infrastructure relies on a decentralized network of indexers, enabling your dapp to become truly decentralized.Quick Start
These subgraphs only take a few minutes to set up and get running. To get started, follow these three steps:- Initialize your subgraph project
- Deploy & Publish
- Query from your dapp
1. Initialize your subgraph project
Create a subgraph on Subgraph Studio
Go to the Subgraph Studio and connect your wallet. Once your wallet is connected, you can begin by clicking “Create a Subgraph”. Please choose a good name for the subgraph because this name can’t be edited later. It is recommended to use Title Case: “Subgraph Name Chain Name.”Install the Graph CLI
On your local machine run the following:Initialize your Subgraph
You can copy this directly from your subgraph page to include your specific subgraph slug:2. Deploy & Publish
Deploy to Subgraph Studio
First run these commands:Test your subgraph
You can test your subgraph by making a sample query in the playground section. The Details tab will show you an API endpoint. You can use that endpoint to test from your dapp.Publish Your Subgraph to The Graph’s Decentralized Network
Once your subgraph is ready to be put into production, you can publish it to the decentralized network. On your subgraph’s page in Subgraph Studio, click on the Publish button:
Before you can query your subgraph, Indexers need to begin serving queries on it. In order to streamline this process, you can curate your own subgraph using GRT.
When publishing, you’ll see the option to curate your subgraph. As of May 2024, it is recommended that you curate your own subgraph with at least 3,000 GRT to ensure that it is indexed and available for querying as soon as possible.
Note: The Graph’s smart contracts are all on Arbitrum One, even though your subgraph is indexing data from Ethereum, BSC or any other supported chain.
3. Query your Subgraph
Congratulations! You can now query your subgraph on the decentralized network! For any subgraph on the decentralized network, you can start querying it by passing a GraphQL query into the subgraph’s query URL which can be found at the top of its Explorer page. Here’s an example from the CryptoPunks Ethereum subgraph by Messari:Getting your own API Key
Appendix
Sample Query
This query shows the most expensive CryptoPunks sold.Sample code
Additional resources:
- To explore all the ways you can optimize & customize your subgraph for a better performance, read more about creating a subgraph here.
- For more information about querying data from your subgraph, read more here.
Popular Subgraphs
- fuse-nft - all NFTs on the Fuse Network
- fuse-blocks - blocks details of the network
- voltage-dex - Voltage DEX subgraph for on-chain trading information
- fuse-ethereum-amb - subgraph for Fuse -> Ethereum AMB bridge
- etherem-fuse-amb - subgraph for Ethereum -> Fuse AMB bridge