Get Started with The Graph
Getting historical data on smart contracts can be challenging when building dApps. The Graph provides an easy way to query smart contracts data through APIs known as subgraphs. Its infrastructure relies on a decentralized network of indexers, enabling dApps to achieve true decentralization.
These subgraphs only take a few minutes to set up and get running and is supported on: .
To get started, follow these steps below:
- Initialize a subgraph project
- Deploy & Publish
- Query from a dApp
- Querying is a way to access
- Publish
Pricing: All developers receive 100K free queries per month on the decentralized network. After these free queries, you only pay based on usage at $4 for every 100K queries.
Getting Started
Initialize a subgraph project
To initialize a Subgraph project, we need to create a subgraph on Subgraph Studio.
Go to the Subgraph Studio and connect your wallet. Once wallet is connected, begin by clicking “Create a Subgraph”.
Remember to choose a clear and descriptive name for the subgraph since it can’t be edited later.
It is recommended to use a Title Case: “Subgraph Name Chain Name.”
On the subgraph’s page, all the CLI commands needed will be visible on the right side of the page:
Install the Graph CLI
Run the following command locally:
npm install -g @graphprotocol/graph-cli
You must have at least v0.85.0 to deploy subgraphs on Rootstock mainnet and testnet.
Initialize a Subgraph
This can be copied directly from your subgraph page to include a specific subgraph slug:
graph init --studio <SUBGRAPH_SLUG>
You’ll be prompted to provide some info on your subgraph like this:
Once contract is verified on the block explorer, the CLI will automatically obtain the ABI and set up the subgraph. The default settings will generate an entity for each event.
2. Deploy & Publish
Deploy to Subgraph Studio
Run the commands below:
$ graph codegen
$ graph build
To authenticate and deploy your subgraph, run the commands below. You can copy these commands directly from your subgraph’s page in Studio to include a specific deploy key and subgraph slug:
$ graph auth --studio <DEPLOY_KEY>
$ graph deploy --studio <SUBGRAPH_SLUG>
You will be asked to provide a version label (e.g., v0.0.1). You can use any format that works for you.