Back to Hub
CREATED BY CHAINLINK LABS

Indexer Data Fetch

Workflows for pulling data from The Graph indexer with scheduled cron triggers using the pull pattern.

Workflows for pulling data from The Graph indexer with scheduled cron triggers. These workflows demonstrate the pull pattern where the workflow initiates and fetches data on a schedule.

Directory Structure

building-blocks/indexer-fetch/
โ”œโ”€โ”€ README.md (this file)
โ”œโ”€โ”€ indexer-fetch-go/     (Go-based workflow)
โ”‚   โ””โ”€โ”€ my-workflow/
โ”‚       โ”œโ”€โ”€ workflow.go
โ”‚       โ”œโ”€โ”€ main.go
โ”‚       โ”œโ”€โ”€ config.staging.json
โ”‚       โ”œโ”€โ”€ config.production.json
โ”‚       โ””โ”€โ”€ workflow.yaml
โ””โ”€โ”€ indexer-fetch-ts/     (TypeScript-based workflow)
    โ””โ”€โ”€ workflow/
        โ”œโ”€โ”€ main.ts
        โ”œโ”€โ”€ config.staging.json
        โ”œโ”€โ”€ config.production.json
        โ”œโ”€โ”€ package.json
        โ””โ”€โ”€ workflow.yaml

Overview

These workflows demonstrate how to:

  • Query The Graph indexer using GraphQL
  • Use cron triggers to schedule periodic data fetching
  • Process and return JSON-formatted indexer data
  • Implement the same functionality in both Go and TypeScript

Both workflows query the Uniswap V4 subgraph on The Graph and fetch:

  • Pool manager statistics (pool count, transaction count, total volume)
  • ETH price data from bundles

Workflows

1 indexer-fetch-go (Go Implementation)

Language: Go

Features:

  • Uses http.SendRequest pattern from CRE Go SDK
  • Implements ConsensusIdenticalAggregation for deterministic data
  • Returns formatted JSON with timestamp and endpoint info

Running the workflow:

cd building-blocks/indexer-fetch/indexer-fetch-go
cre workflow simulate my-workflow --target staging-settings
2 indexer-fetch-ts (TypeScript Implementation)

Language: TypeScript

Features:

  • Uses runInNodeMode pattern from CRE TypeScript SDK
  • Implements custom first-result aggregation for deterministic data
  • Returns formatted JSON with timestamp and endpoint info

Running the workflow:

cd building-blocks/indexer-fetch/indexer-fetch-ts
cre workflow simulate workflow --target staging-settings

Configuration

Both workflows use the same configuration structure in their respective config.staging.json files:

{
  "schedule": "0 * * * * *",
  "graphqlEndpoint": "https://gateway.thegraph.com/api/bca58895bc60dcb319e3cbdfd989b964/subgraphs/id/Gqm2b5J85n1bhCyDMpGbtbVn4935EvvdyHdHrx3dibyj",
  "query": "{ poolManagers(first: 5) { id poolCount txCount totalVolumeUSD } bundles(first: 5) { id ethPriceUSD } }",
  "variables": {}
}

Configuration Options

  • schedule: Cron expression in 6-field format (second minute hour day month weekday)

    • "0 * * * * *" - Every minute at second 0
    • "*/30 * * * * *" - Every 30 seconds
    • "0 */5 * * * *" - Every 5 minutes at second 0
  • graphqlEndpoint: The Graph API endpoint URL

    • Gateway endpoint: https://gateway.thegraph.com/api/{api-key}/subgraphs/id/{subgraph-id}
    • Studio endpoint: https://api.studio.thegraph.com/query/{id}/{name}/version/latest
  • query: GraphQL query string

    • Simple queries without variables work best
    • See The Graph documentation for query syntax
  • variables: Object with variables for the GraphQL query (optional)

Setup and Testing

1 Prerequisites

For Go workflow:

  1. Install CRE CLI
  2. Login: cre login
  3. Install Go

For TypeScript workflow:

  1. Install CRE CLI
  2. Login: cre login
  3. Install Bun (or Node.js)
  4. Run bun install in the workflow directory
2 Running the Workflows

Go Workflow:

cd building-blocks/indexer-fetch/indexer-fetch-go
cre workflow simulate workflow --target staging-settings

TypeScript Workflow:

cd building-blocks/indexer-fetch/indexer-fetch-ts
cre workflow simulate workflow --target staging-settings
3 Example Output

Both workflows return JSON output like:

{
  "timestamp": "2025-11-18T18:43:08.452Z",
  "endpoint": "https://gateway.thegraph.com/api/.../subgraphs/id/...",
  "data": {
    "bundles": [
      {
        "ethPriceUSD": "3157.000458184067393927942592490315",
        "id": "1"
      }
    ],
    "poolManagers": [
      {
        "id": "0x498581ff718922c3f8e6a244956af099b2652b2b",
        "poolCount": "5123368",
        "totalVolumeUSD": "5611562100.854190095192400782985064",
        "txCount": "480580367"
      }
    ]
  }
}

Example Use Cases

1. Monitoring Uniswap V4 Pools

Query pool statistics every minute:

{
  "schedule": "0 * * * * *",
  "graphqlEndpoint": "https://gateway.thegraph.com/api/{key}/subgraphs/id/{id}",
  "query": "{ poolManagers(first: 5) { id poolCount totalVolumeUSD } }",
  "variables": {}
}

2. Tracking Token Prices

Monitor token prices every 30 seconds:

{
  "schedule": "*/30 * * * * *",
  "graphqlEndpoint": "https://gateway.thegraph.com/api/{key}/subgraphs/id/{id}",
  "query": "{ tokens(first: 10, orderBy: volumeUSD, orderDirection: desc) { id symbol volumeUSD } }",
  "variables": {}
}

3. DeFi Protocol Metrics

Check protocol statistics every 5 minutes:

{
  "schedule": "0 */5 * * * *",
  "graphqlEndpoint": "https://gateway.thegraph.com/api/{key}/subgraphs/id/{id}",
  "query": "{ protocols(first: 1) { totalValueLockedUSD totalVolumeUSD txCount } }",
  "variables": {}
}

Reference Documentation

Get the latest Chainlink content straight to your inbox.