Understanding subgraphs
Guide to Subgraphs on SettleMint platform
π§Ύ subgraph project structure β deep dive for mycontract.sol
This document provides a complete, line-by-line breakdown of a subgraph project
folder designed to index events from an Ethereum smart contract named
MyContract.sol
. It follows the conventions used by
The Graph Protocol and tooling like
SettleMint Subgraph Generator.
π folder structure
π§ subgraph.config.json
β purpose:
This file is consumed by SettleMint's CLI or The Graph CLI (graph codegen
,
graph build
) to bootstrap subgraph code and configuration.
π breakdown:
Field | Description |
---|---|
output | Directory for generated TypeScript types (generated/ folder). Often linked to schema.graphql , ABI , and handler typing. |
chain | Blockchain environment to index (e.g., mainnet , localhost , polygon , arbitrum-one ). |
datasources | Array of contract modules to be indexed. Each maps to a folder and .yaml config inside datasources/ . |
name | Logical name of this contract or module (used in code and schema). |
address | On-chain deployed address of MyContract.sol . Must be accurate for indexing to work. |
startBlock | First block number to index from (skip historical blocks for faster sync). |
module | Name of folder containing *.yaml , *.ts , and *.gql.json files for this contract. |
π‘ Tip: Use the block number where your contract was deployed to avoid unnecessary indexing of irrelevant historical data.
π datasources/mycontract.gql.json
β purpose:
This is a JSON-encoded GraphQL schema, which defines the shape of your
indexed data. Each type
here corresponds to an entity stored in the
subgraphβs database.
π§Ύ example:
π field types:
ID!
: Required unique identifier (string
format, often tx hash or entity composite key).Bytes
: EVM-style address or 32-byte values.BigInt
: For uint256, block numbers, timestamps.String
,Boolean
,Int
: Basic GraphQL types.@derivedFrom
: Used for reverse relationships (foreign keys) but only in.graphql
, not.gql.json
.
π‘ Use
id
as a combination oftransactionHash-logIndex
for uniqueness in event-based entities.
π datasources/mycontract.yaml
β purpose:
This is the manifest file that connects your smart contract to its:
- Deployed address
- ABI definition
- Events you want to index
- GraphQL schema
- Event handler functions
π§Ύ example:
π key fields:
Section | Field | Description |
---|---|---|
dataSources | name | Logical label for contract. |
source.address | Deployed contract address. | |
source.abi | Refers to name under abis: . | |
startBlock | Block number from which to start processing. | |
mapping.entities | List of entities affected by this contract. | |
abis.file | Path to .json ABI file of contract (must match source). | |
eventHandlers | event: | Signature of the Solidity event being indexed. |
handler: | Name of exported function from .ts file that handles this event. |
β οΈ
event:
must exactly match the ABI event signature, including indexed fields.
π datasources/mycontract.ts
β purpose:
This file contains AssemblyScript handler functions that transform on-chain events into GraphQL entities. These handlers are executed by the subgraph runtime when matching events appear on-chain.
π§Ύ example:
π what's happening:
- Event is imported from auto-generated
MyContract.ts
(compiled ABI). - A helper is called to load or create an entity (
fetchMyEntity
). - Event parameters and context (tx sender, timestamp) are assigned to fields.
- Entity is stored using
.save()
.
𧩠Best practice: use
fetch*
functions to reduce duplication and enforce default values.
π fetch/mycontractdata.ts
β purpose:
Contains utility functions to fetch an entity from the store or create it if it doesn't exist. Useful for enforcing schema defaults and ensuring consistent object initialization.
π§Ύ example:
π why it's important:
- Prevents null pointer errors in
.ts
handler files. - Centralizes logic for initializing new entities.
- Keeps handler functions small and readable.
β summary
File | Purpose |
---|---|
subgraph.config.json | Global configuration (network, modules, output path). |
mycontract.gql.json | GraphQL schema defining the structure of data to be stored. |
mycontract.yaml | Contract address, ABI, events, and mapping file linkage. |
mycontract.ts | Event-to-entity transformation logic in AssemblyScript. |
mycontractdata.ts | Reusable logic to load or create GraphQL entities safely. |
π οΈ best practices
- π§ͺ Always test your subgraph locally with a dev chain before deploying.
- 𧱠Use composite IDs (
txHash-logIndex
) for events to avoid collisions. - βοΈ Keep
fetch/
files modular to reduce logic in event handlers. - π Use
BigInt
,Bytes
, andBoolean
types consistently with EVM data.
π This setup is modular, clean, and scalable for multi-contract projects, cross-chain indexing, or advanced state machine tracking.