> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tenderly.co/llms.txt
> Use this file to discover all available pages before exploring further.

# The Graph and Virtual Environments

> Guide to setting up and using Tenderly Virtual Environments alongside The Graph.

This guide provides detailed steps to set up and use Tenderly Virtual Environments alongside The Graph.

To use The Graph with Virtual Environments, you need to run your own Graph node. In case you're staging your dapp, you'll have to have The Graph instance hosted on your cloud provider of choice.

## Prerequisites

Ensure you have the following prerequisites installed on your system:

* Node.js and npm
* Docker
* A [Tenderly account](https://dashboard.tenderly.co/login?redirectTo=testnets)
* A Virtual Environment RPC
* A smart contract deployed to Virtual Environment using [Hardhat](/virtual-environments/develop/deploy-contracts#hardhat) or [Foundry](/virtual-environments/develop/deploy-contracts#foundry)

## Set up The Graph

<Steps>
  ### Install Graph CLI Globally

  Install the Graph CLI globally on your system using npm:

  ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  npm install -g @graphprotocol/graph-cli
  ```

  Refer to the official [Graph CLI documentation](https://thegraph.com/docs/en/developer/cli/) for more details.

  ### Set up The Graph project

  Inside your project directory, initialize The Graph project.

  Notes:

  * Adapt `--abi` to point to contrac ABI on your local system
  * Modify the `--contract-name` and `--from-contract` with contract name and address, respectively
  * Set he `--start-block`
  * Change the `--network` so it references the network you based your Virtual Environment off of (e.g. `mainnet`, `base`, etc.).

  <Note>
    We recommend having `network` match the original network's name, without labeling it as `virtual_`. This enables effortless switching of RPCs when deploying to different staging and demoing environments.
  </Note>

  ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  graph init your-subgraph-name subgraph-dir \
  --protocol ethereum \
  --studio \
  --abi path/to/abi/ERC20Token.json \
  --contract-name ERC20TokenFactory \
  --from-contract 0x7F2Fa32C991fD2F8dF3EB23C394f550376e9b30d \
  --start-block 0 \
  --network mainnet
  ```

  <Card title="Graph Initialization documentation" href="" />

  ### Update `docker-compose.yml`

  Edit the `docker-compose.yml` file (below) and update `environment.ethereum` so it refers to the Virtual Environment RPC.

  ```yaml showLineNumbers lines={15} theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  version: '3'
  services:
    graph-node:
      image: graphprotocol/graph-node
      ports:
        - '8020:8020'
        - '8000:8000'
        - '5001:5001'
      environment:
        postgres_host: postgres
        postgres_user: graph-node
        postgres_pass: let-me-in
        postgres_db: graph-node
        ipfs: 'ipfs:5001'
        ethereum: 'mainnet:https://virtual.mainnet.rpc.tenderly.co/d0a6...-40..2c-81..0f-a5..90'
  ```

  <Card title="Graph Docker documentation" href="" />

  ### Run Docker Compose locally

  After setting up docker-compose config, run the following:

  ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  docker-compose up
  ```

  ### Create and deploy your subgraph

  Run the following commands, replacing `your-subgraph-name` with the desired name for your subgraph.

  ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  graph codegen
  graph create --node http://localhost:8020 your-subgraph-name
  graph deploy --node http://localhost:8020 --ipfs http://localhost:5001 your-subgraph-name
  ```

  <Card title="Graph Deployment documentation" href="" />

  ### Access the subgraph

  After deployment, access your subgraph through the URL below. Open this URL in your browser to access the subgraph and run queries:

  `Deployed to http://localhost:8000/subgraphs/name/your-subgraph-name`

  <Card title="Graph Query documentation" href="" />

  ### Deploy to Cloud

  For dapp staging, deploy The Graph node your cloud provider of choice, and connect your dapp UI/backend to that node for querying.
</Steps>
