How to test subgraph locally

To test the subgraph locally on your computer, you’ll need to set up a local Graph Node and use
it to deploy your subgraph. Follow these steps to test your subgraph locally:

1. Set up Docker:
Install Docker Desktop if you haven’t already. Docker is required to run the Graph Node, IPFS,
and a local Ethereum testnet (if needed) on your computer.

2. Clone the Graph Node repository:
Clone the Graph Node repository and navigate to its directory:

git clone https://github.com/graphprotocol/graph-node.git
cd graph-node/docker

3. Configure the `docker-compose.yml` file:
Open the `docker-compose.yml` file and edit the `ethereum` field in the `graph-node` service
section to use the appropriate Ethereum network RPC URL. For example, if you’re using a local
testnet like Ganache, set the URL to `http://host.docker.internal:8545`. If you’re using a public
Ethereum network, you can use a service like Infura or QuickNode for the RPC URL.

4. Start the Graph Node:
In the `graph-node/docker` directory, run the following command to start the Graph Node,
IPFS, and (optionally) a local Ethereum testnet:

docker-compose up

Wait for the Graph Node to start, and ensure it’s connected to the Ethereum network.

5. Prepare your subgraph for deployment:
In your subgraph project, ensure the `subgraph.yaml` file has the correct `network` field set
for the Ethereum network you’re using. For example, if you’re using a local Ganache testnet, set
the `network` field to `mainnet`. If you’re using a public Ethereum network, set it to the
appropriate network name, such as `mainnet`, `rinkeby`, or `ropsten`.

6. Deploy your subgraph to the local Graph Node:
In your subgraph project directory, run the following commands:

yarn codegen
yarn build
graph create –node http://localhost:8020/ /

graph deploy –node http://localhost:8020/ –ipfs http://localhost:5001/ /

7. Test your subgraph:
Open the GraphiQL interface in your browser at `http://localhost:8000/`. You can now run
GraphQL queries against your local subgraph to test its functionality. For example, to query for
all token transfers, you could run the following query:

transfers {
metadataURI

Once you’ve tested your subgraph and are satisfied with its performance, you can deploy it to a
hosted service like The Graph’s hosted service, as described in the previous answer.