> For the complete documentation index, see [llms.txt](https://easonwang.gitbook.io/blockchain/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://easonwang.gitbook.io/blockchain/the-graph.md).

# The Graph

使用 graphQL 數據結構，讓你可以監聽區塊鏈節點的事件或數據，並且發佈到 graph node 上面。之後可以讓大家 Query 使用。需要自己寫一個 subgraph 的邏輯，然後發布到 graph node上。官方有提供 名為[ studio node](https://thegraph.com/studio/) 的 graph node。也可以在自己本地架設。

### 官方文件

<https://thegraph.com/docs/en/>

### 本地架設 Graph node 教學

<https://thegraph.academy/developers/local-development/>

### 探索已發佈的 Subgraph

{% embed url="<https://thegraph.com/explorer>" %}

## 使用 OpenZeppelin 範例

<https://github.com/OpenZeppelin/openzeppelin-subgraphs>

<https://docs.openzeppelin.com/subgraphs/0.1.x/>

將以上 clone 到你的專案或是 `npm install @openzeppelin/subgraphs`，之後 copy node\_module 裡面 config file，然後放到你的專案內

之後輸入以下（要把 --config 後的路徑改成剛的 config file）

```
npx graph-compiler \
  --config configs/sample.json \
  --include node_modules/@openzeppelin/subgraphs/src/datasources \
  --export-schema \
  --export-subgraph
```

![](/files/E1FxA0nwuxCqLScxi1LU)

之後要去把 編譯出來的 yaml 改名為 subgraph.yaml，然後增加 network 與 contract address 等 config field

ERC721 的 subgraph.yaml 範例：

```
specVersion: 0.0.2
schema:
  file: erc721.schema.graphql
dataSources:
  - kind: ethereum/contract
    name: erc721
    network: mainnet
    source:
      abi: IERC721
    mapping:
      kind: ethereum/events
      apiVersion: 0.0.5
      language: wasm/assemblyscript
      entities:
        - ERC721Contract
      abis:
        - name: IERC721
          file: ../node_modules/@openzeppelin/contracts/build/contracts/IERC721Metadata.json
      eventHandlers:
        - event: Approval(indexed address,indexed address,indexed uint256)
          handler: handleApproval
        - event: ApprovalForAll(indexed address,indexed address,bool)
          handler: handleApprovalForAll
        - event: Transfer(indexed address,indexed address,indexed uint256)
          handler: handleTransfer
      file: ../node_modules/@openzeppelin/subgraphs/src/datasources/erc721.ts

```

之後看你要部署到 studio 還是自己 host 的節點，如果要部署到 studio 就先去 <https://thegraph.com/studio/> create subgraph 然後從 cli auth 後 deploy

```
graph deploy --studio <yaml 內 dataSources 的 name>
```

部署到 local 節點就參考：<https://thegraph.academy/developers/local-development/>

## 查看目前 subgraph 健康度

hosted service 可用此 endpoint `https://api.thegraph.com/index-node/graphq`

```
indexingStatusForCurrentVersion(subgraphName: "org/subgraph") {
  synced
  health
  fatalError {
    message
    block {
      number
      hash
    }
    handler
  }
  chains {
    chainHeadBlock {
      number
    }
    latestBlock {
      number
    }
  }
}
```

## 相關事項

graph-cli 版本 0.16 後有改變指令，如果要用舊版 cli 部署到 studio 或 host-service 可以如下使用 --node 參數

> const HOSTED\_SERVICE\_URL = '<https://api.thegraph.com/deploy/>'
>
> const SUBGRAPH\_STUDIO\_URL = '<https://api.studio.thegraph.com/deploy/>'

```
npx graph deploy --access-token=<...> --node https://api.thegraph.com/deploy/ --ipfs https://api.thegraph.com/ipfs <github username>/<subgraph name>
```

## 相關範例

#### OpenZeppelin:

<https://github.com/OpenZeppelin/openzeppelin-subgraphs>

#### NFT:

<https://github.com/dabit3/custom-nft-subgraph-workshop>

<https://github.com/dabit3/bored-ape-yacht-club-api-and-subgraph>

&#x20;<https://github.com/dabit3/building-a-subgraph-workshop>

#### DeFi:

<https://github.com/token-terminal/tt-subgraphs>

&#x20;<https://github.com/messari/subgraphs>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://easonwang.gitbook.io/blockchain/the-graph.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
