Deploy a Subgraph

Deploying a subgraph involves creating a GraphQL schema, defining mappings to extract data from a blockchain or data source, and deploying the subgraph to a GraphNode service. In this tutorial, we'll walk you through the process of deploying a subgraph using the Graph CLI.

Prerequisites

  1. Node.js and npm: Make sure you have Node.js and npm installed on your system by checking in Terminal (or you can download them from the Node.js website):

    $ node -v
    v19.9.0
    $ npm -v
    9.6.3
  2. Graph CLI: Install the Graph CLI globally using npm:

    npm install -g @graphprotocol/graph-cli
  3. Access to a graph node: Ensure that you have access to a running graph node service. You have set up your own graph node at here.

We will deploy a subgraph for smart contract MyToken.sol on Solaris mainnet: 0xb4C122037a433aEc9FcB25A415Fe1f91845e3958.

Create new project

Create new Node.js project and install dependencies:

mkdir sample_subgraph
cd sample_subgraph
npm init -y

Let's modify your package.json file to set up Node.js project:

Now install dependencies:

Add contract ABI

At project directory, create new folder called abis. Then create new file MyToken.json that located inside abis folder:

MyToken.json contains MyToken contract's ABI. You can retrieve ABI of verified contract via explorer (Contract detail page > tab Contract > button Code > Contract ABI) or copy from compiled ABI with hardhat (artifacts folder). It has this format:

Define schema

A GraphQL schema that defines what data is stored for your subgraph, and how to query it via GraphQL. The schema for your subgraph is in the file schema.graphql. GraphQL schemas are defined using the GraphQL interface definition language. If you've never written a GraphQL schema, it is recommended that you check out this primer on the GraphQL type system. Reference documentation for GraphQL schemas can be found in the GraphQL API section.

At project directory, create new file called schema.graphql and paste following contents:

Learn more about defining entities in your subgraph.

Subgraph manifest

Subgraph manifest is a configuration file for a subgraph. Below is the subgraph manifest for our sample:

Let's break down the elements of this subgraph manifest:

  • specVersion: 0.0.4: This specifies the version of the subgraph manifest. It indicates which version of the subgraph schema and configuration format is being used.

  • description: Sample Subgraph: This is a human-readable description of the subgraph, providing information about its purpose or functionality.

  • schema.file: This points to the GraphQL schema file used for defining the data structure and types that the subgraph will query and expose.

  • dataSources: This section defines the data sources that the subgraph will query:

    • kind: Specifies that the data source is smart contract.

    • name: A user-assigned name for the data source.

    • source.abi: The ABI for the smart contract with the name MyToken.

    • source.address: The address of contract MyToken.

    • source.startBlock: block number from which indexing should start.

  • mapping: specifies how the data from the Ethereum smart contract is mapped to the subgraph:

    • kind: ethereum/events: This indicates that the data source is indexed based on events.

    • apiVersion: 0.0.6: The version of the AssemblyScript being used.

    • language: wasm/assemblyscript: The programming language used for writing the mapping logic.

    • entities: the entities that the data source writes to the store. The schema for each entity is defined in the schema.graphql file

    • abis: one or more named ABI files for the source contract as well as any other smart contracts that you interact with from within the mappings.

    • eventHandlers: lists the smart contract events this subgraph reacts to and the handlers in the mapping—./src/mytoken.ts in the example—that transform these events into entities in the store.

Check out this link to learn more about subgraph manifest.

Define mappings

The mappings take data from a particular source and transform it into entities that are defined within your schema. Mappings are written in a subset of TypeScript called AssemblyScript which can be compiled to WASM (WebAssembly). AssemblyScript is stricter than normal TypeScript, yet provides a familiar syntax.

For each event handler that is defined in subgraph.yaml under mapping.eventHandlers, create an exported function of the same name. Each handler must accept a single parameter called eventwith a type corresponding to the name of the event which is being handled.

At project directory, create a new folder src. Add 2 files as below:

Learn more about writing mappings here.

Codegen, create and deploy subgraph

At the project structure, open up your Terminal, to generate code:

After finishing, generated folder will be created. It contains AssemblyScript files that defined in GraphQL schema and ABI files:

Then create your subgraph on graphnode:

You'll see something similar:

Next deploy your subgraph:

The output is as below:

Visit your subgraph's GraphiQL

Use your browser to access deployed subgraph: http://127.0.0.1:8000/subgraphs/name/u2u/subgraph/graphql

Summary

We have gone through how to deploy subgraph on graphnode that watches U2U network. For more detail please visit The Graph documentation.

To sum up:

  1. Add contract ABI

  2. Write mappings for contract in /src/*

  3. Define models in schema.graphql

  4. Wire up contract, its events, mappings in subgraph.yaml

  5. Try codegen to see if any errors

  6. Run create then deploy

Last updated