Deploy a Subgraph Using The Graph and Hedera JSON-RPC Relay
In this tutorial, you’ll learn how to create and deploy a subgraph using The Graph protocol. By indexing specific network data using user-defined data structures called “subgraphs,” developers can easily query the indexed data through a GraphQL API, creating robust backends for dApps. Subgraphs simplify the process of obtaining blockchain/network data for developers building dApps. This approach removes the complexities of interacting directly with the network, allowing developers to focus on building. Although Hedera supports subgraphs, its hosted service is currently unavailable, so we’ll need to set up and run a local graph node to deploy our subgraph.By the end of this tutorial, you’ll be able to configure a mirror node, query data from your subgraph using the GraphQL API, and integrate it into your dApp. You’ll also have a better understanding of how to define custom data schemas, indexing rules, and queries for your subgraph, allowing you to tailor it to your specific use case.
Note: While it is possible to present and interact with HTS tokens in a
similar manner as ERC-20/721 tokens, the network is presently unable to
capture all the expected ERC-20/721 event logs. In other words, if ERC-like
operations are conducted on HTS tokens, not all of them will be captured in
smart contract event logging.
*Note: When searching for contract addresses, there are two types with different formats - the public smart contract address (0x…) or contract ID (0.0.12345).*
Graph CLI installation
Open your terminal and run the following command:
Copy
Ask AI
npm install -g @graphprotocol/graph-cli
Test to see if it was installed correctly by running:
Copy
Ask AI
graph -v
*Note: The Graph CLI will be installed globally, so you can run the command in any directory.*
Open a terminal window and navigate to the directory where you want your subgraph project stored. Clone the hedera-subgraph-example repo, change directories, and install dependencies:
Rename the subgraph.template.yaml file to subgraph.yaml before moving on to the next step. The subgraph project structure should look something like this:
In the testnet.json file, under the config folder, replace the startBlock and Greeter fields with your start block number and contract address. The JSON file should look something like this:
In this step, you will use the Greeter contract from the Hardhat tutorial as an example subgraph, to configure four main project files: the subgraph manifest, GraphQL schema, event mappings, and Docker compose configuration. The manifest specifies which events the subgraph will listen for, while mappings map each event emitted by the smart contract into entities that can be indexed.
The subgraph manifest (subgraph.yaml) contains important information about your subgraph, such as its name, description, and data sources. To specify the data sources your subgraph will index, you need to define the dataSources field in the manifest. It’s also recommended to add the start block number to the startBlock property to reduce the indexing time. Here’s a guide on how to find the start block number.
Add your deployed Greeter public smart contract address to the address property.
Add your start block number of the deployed contract in the startBlock property.
The eventHandlers field specifies how each mapping connects to various event triggers. Whenever an event defined in this section is emitted from your contract, the corresponding mapping function designated as the handler will be executed.
The GraphQL schema (schema.graphql) defines the structure of the data you want to index in your subgraph. You will need to specify the entity properties that you want to index. For this example, the schema defines a GraphQL entity type called “Greeting” with two entity fields: id and currentGreeting.
schema.graphql
Copy
Ask AI
type Greeting @entity { id: ID! currentGreeting: String!}
The mappings.ts file maps events emitted by your smart contract into entities that can be indexed by a subgraph. It uses AssemblyScript to connect the events to the data schema. AssemblyScript types for entities and events can be generated in the terminal (by running the codegen command) and imported into the mappings file. This allows easy access to the event object’s properties in the code editor.
mappings.ts
Copy
Ask AI
import { GreetingSet } from "../generated/Greeter/IGreeter";import { Greeting } from "../generated/schema";export function handleGreetingSet(event: GreetingSet): void { // Entities can be loaded from the store using a string ID; this ID // needs to be unique across all entities of the same type let entity = Greeting.load(event.transaction.hash.toHexString()); // Entities only exist after they have been saved to the store; // `null` checks allow to create entities on demand if (!entity) { entity = new Greeting(event.transaction.hash.toHex()); } // Entity fields can be set based on event parameters entity.currentGreeting = event.params._greeting; // Entities can be written to the store with `.save()` entity.save();}
To connect a local graph node to a remote network, such as testnet, mainnet, or previewnet, use a docker-compose setup. The API endpoint that connects the graph node to the network is specified within the environment object of the docker-compose.yaml file here. Add the API endpoint URL in the ethereum field in the environment object. For this tutorial, we will use the Hashio Testnet instance of the Hedera JSON-RPC relay, but anyJSON-RPC provider supported by the community can be used.This is what the ethereum field should look like after you enter your API endpoint URL:
Copy
Ask AI
ethereum: 'testnet:https://testnet.hashio.io/api
*Note: For more info on how to set up an indexer, check out* T_he Graph_ docsand theofficial graph-node GitHub repository. For a full subgraph project example, check outthis *repo.*
In this step, you will create the subgraph and deploy it to your local graph node. If everything runs without errors, your terminal should resemble the console check at the end of each subsection.
In the same directory, run the following command to generate AssemblyScript types for entities and events:
Copy
Ask AI
graph codegen
console check ✅
Copy
Ask AI
Skip migration: Bump manifest specVersion from 0.0.2 to 0.0.4✔ Apply migrations✔ Load subgraph from subgraph.yaml Load contract ABI from abis/IGreeter.json✔ Load contract ABIs Generate types for contract ABI: IGreeter (abis/IGreeter.json) Write types to generated/Greeter/IGreeter.ts✔ Generate types for contract ABIs✔ Generate types for data source templates✔ Load data source template ABIs✔ Generate types for data source template ABIs✔ Load GraphQL schema from schema.graphql Write types to generated/schema.ts✔ Generate types for GraphQL schemaTypes generated successfully
You should have a new folder named generated in your project directory. This is what your updated subgraph project structure should look like:
When you run the deploy-local command, your console will prompt you to provide a Version Label. Enter any version number you’d like. This is just a way to keep track of different versions of your subgraph. For instance, if you started with version v0.0.1 today, but then made some changes and wanted to deploy an upgraded version, you bump up the version number to v0.0.2.For example: ✔ Version Label (e.g. v0.0.1) · v0.0.1
console check ✅
Copy
Ask AI
> hedera-subgraph-repo-example@1.0.0 deploy-local> graph deploy --node http://localhost:8020/ --ipfs http://localhost:5001 Greeter✔ Version Label (e.g. v0.0.1) · v0.0.1✔ Apply migrations✔ Load subgraph from subgraph.yamlCompile data source: Greeter => build/Greeter/Greeter.wasm✔ Compile subgraphCopy schema file build/schema.graphqlWrite subgraph file build/Greeter/abis/IGreeter.jsonWrite subgraph manifest build/subgraph.yaml✔ Write compiled subgraph to build/Add file to IPFS build/schema.graphql.. QmVtZMzbjU6QHEFfrCJ5NhbP5vUNrukaussxXZ4Esf3qCmAdd file to IPFS build/Greeter/abis/IGreeter.json.. QmZQbrdhaR2p2EZR6raiLbpgX5hjKW4S5cDgy1VvHKmjtHAdd file to IPFS build/Greeter/Greeter.wasm.. QmYz3qFZ4KHiHXhgbTKFxbCNvE9Serhq8yvGuJPK12K5qf✔ Upload subgraph to IPFSBuild completed: QmbGuuuqtEEqFxjdwSdhiKKpb4GCzqbh3oASAnVVEXRoVWDeployed to http://localhost:8000/subgraphs/name/Greeter/graphqlSubgraph endpoints:Queries (HTTP): http://localhost:8000/subgraphs/name/Greeter
After the subgraph is successfully deployed, open the GraphQL playground, where you can execute queries and fetch indexed data.
Congratulations! You’ve successfully deployed a subgraph to your local graph node!;
Once the node finishes indexing, you can access the GraphQL API at: http://localhost:8000/subgraphs/name/GreeterFollow the steps below to execute the query and fetch the indexed data from the subgraph’s entities:
Enter the following GraphQL query into the left column of the playground (see Step 1 in the screenshot below):
Copy
Ask AI
{ greetings { id currentGreeting }}
Execute the query by clicking on the play button at the top of the playground (see Step 2 in the screenshot below).
The query returns the indexed data from the subgraph’s entities on the right column of the playground (see Step 3 in the screenshot below):
Congratulations! 🎉 You have successfully learned how to deploy a Subgraph using The Graph Protocol and JSON-RPC. Feel free to reach out onDiscordif you have any questions!