Subgraph Development

Building a Custom Subgraph.

Subgraphs are a key piece in the Web3 stack to access blockchain data. Learn how to build, test, and publish a new subgraph to The Graph’s decentralized network by indexing data from smart contracts.

About The Graph

The Graph is the indexing and query layer
of web3. Developers build and publish open
APIs, called subgraphs, that applications
can easily query using GraphQL.

About Subgraphs

A subgraph extracts data from a blockchain, processing it and storing it so that it can be easily queried via GraphQL.

Anatomy of a Subgraph.

  • subgraph.yaml : A YAML file that describes the subgraph configuration
    such as smart contract address, entities, events, etc.
  • schema.graphql : A GraphQL schema that defines the data you want
    your GraphQL API to query to serve to your frontend client
  • AssemblyScript Mappings: AssemblyScript1
    code that translates from
    the event data emitted from the smart contract to the entities defined in
    your schema (e.g. mapping.ts in this cheatsheet)

Note: The subgraph folder structure includes several other files and folders such as
generated, node_modules, build, etc. The above list only delineates the files that are
involved in the subgraph definition process.

QR-code

Assembly Script: Scan this code or use this link: assembly script

Building a Custom Subgraph.

Subgraphs are a key piece in the Web3 stack to access blockchain data. Learn how to build, test, and publish a new subgraph to The Graph’s decentralized network by indexing data from smart contracts.

Initialization

Up-and-vibing with your first subgraph!

Installing the Graph CLI

  • Using npm
Copy to Clipboard
  • Using yarn
Copy to Clipboard

Initializing your subgraph

  • Using a smart contract
Copy to Clipboard
  • Using an existing example subgraph
Copy to Clipboard

Configuration

Configure your subgraph

Define the following fields in subgraph.yaml

  • Datasource: Smart contract address
  • Start block: Smart contract start block
  • Entities: Data to be defined in schema.graphql
  • Event Handlers: Event hazzndlers to be defined in mapping.ts

Defining the entities

Define the entities in schema.graphql

Run codegen command

graph codegen

Auth & Deployment

Updating AssemblyScript Mappings

Installing the Graph CLI

Configure the AssemblyScript mappings in the src/mapping.ts file to handle the events defined in subgraph.yaml

Authenticating your subgraph

graph auth --studio

Deploying your subgraph

graph deploy --studio

Testing your subgraph

Test a sample query in the GraphQL playground in
your subgraph dashboard

Querying an existing subgraph.

Each subgraph deployed on the decentralized network has a query URL that allows you to query the blockchain data indexed by that subgraph. Let’s learn how to use an existing subgraph to query data to your frontend application!

Using the Graph Explorer

API Keys are required to query subgraphs. You can receive 1,000 free queries on your first API key. You can create and manage your API keys in the Subgraph Studio from the API Keys section

  1. Open Subgraph Studio > API Keys
  2. Click on “Create API Key
  3. Enter the name of your API Key and hit “Create API Key
  4. Enter email to claim 1,000 free queries
  5. Click on “Copy” to copy the API Key from the Overview section

Adding the API Key

Update the query URL for the subgraph with the newly created API Key from the previous step.

Note: Querying subgraphs using your API keys will generate query fees that will be paid in GRT from the Billing section.

You can learn more about billing here: thegraph.com/docs/en/billing/

Go Back

Developer Knowledge Hub