Skip to main content

Initializing a subgraph

graph init

Purpose

Creates a new Subgraph project using an example template or an existing smart contract.

Use case

  • Use this command when starting a new Subgraph project.
  • It sets up the necessary folder structure, configuration files subgraph.yaml, and example code.
  • You can specify a contract address and network to scaffold a Subgraph tailored to a specific smart contract.

Example

graph init --from-contract <CONTRACT_ADDRESS> --network mainnet

Generating code from subgraph files

graph codegen

Purpose

Generates AssemblyScript types for smart contract ABIs and the Subgraph schema.

Use case

  • Use this command after making changes to your schema.graphql file or when adding/updating smart contract ABIs.
  • It creates type-safe classes that make it easier to interact with blockchain data in your mappings.

Build your subgraph

graph build

Purpose

Compiles the Subgraph to WebAssembly (WASM), preparing it for deployment.

Use Case

  • Run this command after completing your mappings and configuration.
  • It ensures your Subgraph is ready for deployment by converting it into the WASM format required by 0xGraph.

Deploy your subgraph

graph deploy

Purpose

Deploys the compiled Subgraph to the 0xGraph hosting service.

Use case

  • After building your Subgraph, use this command to upload and activate it on 0xGraph.
  • Specify the deployment target (e.g., staging or production) and the Subgraph name.

Example

graph deploy <your_subgraph> --node 
https://api.subgraph.ormilabs.com/deploy 
--ipfs https://api.subgraph.ormilabs.com/ipfs 
--deploy-key <your_api_key>

Run the subgraph locally

graph local

Purpose

Runs a local test environment (e.g., using Ganache or Hardhat) to test the Subgraph with simulated blockchain data.

Use Case

  • Use this command when developing and testing your Subgraph locally.
  • It allows you to simulate blockchain events and validate mappings.
  • Ideal for testing without deploying to a live environment.

Executes unit tests

graph test

Purpose

Downloads and runs the Matchstick Rust binary to execute unit tests for a Subgraph.

Use Case

  • Use this command to write and run unit tests for your mappings and ensure that your Subgraph logic works correctly.
  • Matchstick is a testing framework built specifically for The Graph.

Add new data source

graph add

Purpose

Adds a new data source to your Subgraph’s manifest file (subgraph.yaml) and updates relevant files such as schema.graphql, ABIs, and mappings.

Use case

  • Use this command when adding additional smart contracts or data sources to your Subgraph.
  • It automates the process of updating your Subgraph configuration and related files.

Example

graph add <CONTRACT_NAME> --from-contract <CONTRACT_ADDRESS>
Ensure you have the latest version of The Graph CLI installed to avoid potential issues with deprecated or updated commands.
I