Last updated: June 2025
This is a basic subgraph provided for reference purposes. It serves as a starting point for developers looking to understand how subgraphs work with The Graph.
This example is comprised of three core files that define the schema, the manifest, and the mapping logic.
Defines the Gravatar entity stored in The Graph's data store:
type Gravatar @entity(immutable: false) {
id: ID! # Unique identifier (uint256 → hex string)
owner: Bytes! # Address of the Gravatar owner
displayName: String! # Chosen display name
imageUrl: String! # URL of the avatar image
}@entity(immutable: false)allows updates to existing entities.- Fields map directly to Solidity event parameters.
The manifest that configures your subgraph deployment:
specVersion: 1.3.0
description: Gravatar for Ethereum
repository: https://github.com/graphprotocol/examples/tree/main/subgraphs/basic-examples/init-subgraph
schema:
file: ./schema.graphql
dataSources:
- kind: ethereum/contract
name: Gravity # Data source name
network: mainnet # Target network
source:
address: '0x2E645469f354BB4F5c8a05B3b30A929361cf77eC'
abi: Gravity
mapping:
kind: ethereum/events # Mapping type
apiVersion: 0.0.6
language: wasm/assemblyscript
file: ./src/mapping.ts # Mapping implementation
entities:
- Gravatar
abis:
- name: Gravity
file: ./artifacts/contracts/Gravity.sol/GravatarRegistry.json
eventHandlers:
- event: NewGravatar(uint256,address,string,string)
handler: handleNewGravatar
- event: UpdatedGravatar(uint256,address,string,string)
handler: handleUpdatedGravatarspecVersion: Graph spec version.dataSources: Array of contract sources and their mappings.eventHandlers: Connect Ethereum events to AssemblyScript functions.
AssemblyScript mapping logic for processing events:
import { NewGravatar, UpdatedGravatar } from "../generated/Gravity/Gravity";
import { Gravatar } from "../generated/schema";
// Creates a new Gravatar entity when the NewGravatar event is emitted.
export function handleNewGravatar(event: NewGravatar): void {
let gravatar = new Gravatar(event.params.id.toHex());
gravatar.owner = event.params.owner;
gravatar.displayName = event.params.displayName;
gravatar.imageUrl = event.params.imageUrl;
gravatar.save();
}
// Updates an existing Gravatar entity on UpdatedGravatar or creates one if missing.
export function handleUpdatedGravatar(event: UpdatedGravatar): void {
let id = event.params.id.toHex();
let gravatar = Gravatar.load(id);
if (gravatar == null) {
gravatar = new Gravatar(id);
}
gravatar.owner = event.params.owner;
gravatar.displayName = event.params.displayName;
gravatar.imageUrl = event.params.imageUrl;
gravatar.save();
}- Imports generated types for events and schema.
event.paramsfields map one-to-one with entity attributes.Gravatar.load: Checks for existing entity to apply updates idempotently.
Subgraph Studio is where we will interact with our Subgraph and get its endpoints after it has been deployed and is indexing our smart contract's data.
- Open Subgraph Studio and click Create a Subgraph.
- Name it. For this example, we will call this subgraph
nft-transfers. - Select the chain where your smart contract is deployed from the network list.
- Click Save.
Now, let's spin up our Subgraph locally to prepare it to deploy to Subgraph Studio.
-
Install the CLI:
yarn install -g @graphprotocol/graph-cli
-
Inside an empty project folder, run:
graph init nft-transfers
-
Follow the prompts:
- Choose the chain where your smart contract is deployed.
- Select Smart Contract.
- Paste your contract address when asked.
The CLI autogenerates three core files:
subgraph.yaml— Manifest of data sources & handlers.schema.graphql— Entity definitions (already includes a Transfer entity for ERC-721).mapping.ts— AssemblyScript logic.
Now that our Subgraph is scaffolded locally, we can deploy it to Subgraph Studio and begin indexing!
-
Back in your project folder:
yarn codegen && yarn build -
Authenticate once per machine:
graph auth --studio <DEPLOY_KEY>
-
Deploy:
graph deploy --studio nft-transfers
After the initial sync finishes, Studio will display a GraphQL Playground for further testing.
For more information see the docs on https://thegraph.com/docs/.