The Graph
The Graph Quick Start
Getting historical data on a smart contract can be frustrating when you’re building a dapp. The Graph provides a decentralized option to query smart contract data through APIs known as subgraphs, which utilize GraphQL. The Graph’s infrastructure relies on a decentralized network of indexers, enabling your dapp to become truly decentralized.
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
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.
Here’s a step by step walk through:
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.”
You will then land on your subgraph’s page. All the CLI commands you need will be visible on the right side of the page:
Install the Graph CLI
On your local machine run the following:
npm install -g @graphprotocol/graph-cli
Initialize your Subgraph
You can copy this directly from your subgraph page to include your specific subgraph slug:
graph init --studio <SUBGRAPH_SLUG>
You’ll be prompted to provide some info on your subgraph like this:
Simply have your contract verified on the block explorer and the CLI will automatically obtain the ABI and set up your subgraph. The default settings will generate an entity for each event.