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.
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
- Using yarn
Initializing your subgraph
- Using a smart contract
- Using an existing example subgraph
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
- Open
Subgraph Studio
>API Keys
- Click on “Create API Key”
- Enter the name of your API Key and hit “Create API Key”
- Enter email to claim 1,000 free queries
- 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.