Quick Start

Reading time: 5 min

Learn how to easily publish and query a subgraph on The Graph.

Prerequisites for this guide

Link to this section
  • A crypto wallet
  • A smart contract address on one of the supported networks

Step-by-step

Link to this section

1. Install the Graph CLI

Link to this section

You must have Node.js and a package manager of your choice (npm, yarn or pnpm) installed to use the Graph CLI. Check for the most recent CLI version.

On your local machine, run one of the following commands:

Using npm:

npm install -g @graphprotocol/graph-cli@latest

Using yarn:

yarn global add @graphprotocol/graph-cli

2. Create your subgraph

Link to this section

If your contract has events, the init command will automatically create a scaffold of a subgraph.

Create via Graph CLI

Link to this section

Use the following command to create a subgraph in Subgraph Studio using the CLI:

graph init --product subgraph-studio

Create via Subgraph Studio

Link to this section

Subgraph Studio lets you create, manage, deploy, and publish subgraphs, as well as create and manage API keys.

  1. Go to Subgraph Studio and connect your wallet.
  2. Click "Create a Subgraph". It is recommended to name the subgraph in Title Case: "Subgraph Name Chain Name".

For additional information on subgraph creation and the Graph CLI, see Creating a Subgraph.

3. Initialize your subgraph

Link to this section

From an existing contract

Link to this section

The following command initializes your subgraph from an existing contract:

graph init --studio <SUBGRAPH_SLUG>

Note: If your contract was verified on Etherscan, then the ABI will automatically be created in the CLI.

You can find commands for your specific subgraph on the subgraph page in Subgraph Studio.

When you initialize your subgraph, the CLI will ask you for the following information:

  • Protocol: Choose the protocol your subgraph will be indexing data from.
  • Subgraph slug: Create a name for your subgraph. Your subgraph slug is an identifier for your subgraph.
  • Directory to create the subgraph in: Choose your local directory.
  • Ethereum network (optional): You may need to specify which EVM-compatible network your subgraph will be indexing data from.
  • Contract address: Locate the smart contract address you’d like to query data from.
  • ABI: If the ABI is not auto-populated, you will need to input it manually as a JSON file.
  • Start Block: You should input the start block to optimize subgraph indexing of blockchain data. Locate the start block by finding the block where your contract was deployed.
  • Contract Name: Input the name of your contract.
  • Index contract events as entities: It is suggested that you set this to true, as it will automatically add mappings to your subgraph for every emitted event.
  • Add another contract (optional): You can add another contract.

See the following screenshot for an example for what to expect when initializing your subgraph:

Subgraph command

4. Write your subgraph

Link to this section

The init command in the previous step creates a scaffold subgraph that you can use as a starting point to build your subgraph.

When making changes to the subgraph, you will mainly work with three files:

  • Manifest (subgraph.yaml) - defines what data sources your subgraph will index.
  • Schema (schema.graphql) - defines what data you wish to retrieve from the subgraph.
  • AssemblyScript Mappings (mapping.ts) - translates data from your data sources to the entities defined in the schema.

For a detailed breakdown on how to write your subgraph, check out Creating a Subgraph.

5. Deploy your subgraph

Link to this section

Remember, deploying is not the same as publishing.

  • When you deploy a subgraph, you push it to Subgraph Studio, where you can test, stage and review it.
  • When you publish a subgraph, you are publishing it onchain to the decentralized network.
  1. Once your subgraph is written, run the following commands:

    graph codegen
    graph build
  2. Authenticate and deploy your subgraph. The deploy key can be found on the subgraph's page in Subgraph Studio.

Deploy key

graph auth --studio <DEPLOY_KEY>
graph deploy --studio <SUBGRAPH_SLUG>
  • The CLI will ask for a version label.
    • It's strongly recommended to use semantic versioning, e.g. 0.0.1. That said, you can choose any string for the version such as: v1, version1, asdf, etc.

6. Review your subgraph

Link to this section

If you’d like to examine your subgraph before publishing it to the network, you can use Subgraph Studio to do the following:

  • Run a sample query.

  • Analyze your subgraph in the dashboard to check information.

  • Check the logs on the dashboard to see if there are any errors with your subgraph. The logs of an operational subgraph will look like this:

    Subgraph logs

7. Publish your subgraph to The Graph Network

Link to this section

Publishing a subgraph to the decentralized network makes it available for Curators to begin curating it and Indexers to begin indexing it.

Publishing with Subgraph Studio

Link to this section
  1. To publish your subgraph, click the Publish button in the dashboard.
  2. Select the network to which you would like to publish your subgraph.

Publishing from the CLI

Link to this section

As of version 0.73.0, you can also publish your subgraph with the Graph CLI.

  1. Open the graph-cli.

  2. Use the following commands:

    graph codegen && graph build

    Then,

    graph publish
  3. A window will open, allowing you to connect your wallet, add metadata, and deploy your finalized subgraph to a network of your choice.

cli-ui

To customize your deployment, see Publishing a Subgraph.

Adding signal to your subgraph

Link to this section
  1. To attract indexers to query your subgraph, you should add GRT curation signal to it.

    • This action improves quality of service, reduces latency, and enhances network redundancy and availability for your subgraph.
  2. If eligible for indexing rewards, Indexers receive GRT rewards based on the signaled amount.

    • It’s recommended to curate at least 3,000 GRT to attract 3 Indexers. Check reward eligibility based on subgraph feature usage and supported networks.

To learn more about curation, read Curating.

To save on gas costs, you can curate your subgraph in the same transaction you publish it by selecting this option:

Subgraph publish

8. Query your subgraph

Link to this section

Now, you can query your subgraph by sending GraphQL queries to its Query URL, which you can find by clicking the Query button.

For more information about querying data from your subgraph, read Querying The Graph.

Edit page

Previous
L2 Transfer Tools Guide
Next
Supported Networks
Edit page