CLI Usage
A detailed list of all active and relevant commands for The Graph CLI, including their purposes, use cases, and examples.
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 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.
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.
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 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.
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.
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:
Ensure you have the latest version of The Graph CLI installed to avoid potential issues with deprecated or updated commands.