@graphprotocol/graph-ts

中文文档
0.38.2发布于 2025/10/11TypeScriptESM + CJS构建来源已验证近 30 天下载 12.5万

TypeScript/AssemblyScript library for writing subgraph mappings for The Graph

原始使用文档
文档目录4 节
  1. Usage
  2. Helper Functions for AssemblyScript
  3. API
  4. License

The Graph TypeScript Library (graph-ts)

npm (scoped) Build Status

TypeScript/AssemblyScript library for writing subgraph mappings to be deployed to The Graph.

Usage

For a detailed guide on how to create a subgraph, please see the Graph CLI docs.

One step of creating the subgraph is writing mappings that will process blockchain events and will write entities into the store. These mappings are written in TypeScript/AssemblyScript.

The graph-ts library provides APIs to access the Graph Node store, blockchain data, smart contracts, data on IPFS, cryptographic functions and more. To use it, all you have to do is add a dependency on it:

npm install --dev @graphprotocol/graph-ts # NPM
yarn add --dev @graphprotocol/graph-ts    # Yarn

After that, you can import the store API and other features from this library in your mappings. A few examples:

import { crypto, store } from '@graphprotocol/graph-ts'
// This is just an example event type generated by `graph-cli`
// from an Ethereum smart contract ABI
import { NameRegistered } from './types/abis/SomeContract'
// This is an example of an entity type generated from a
// subgraph's GraphQL schema
import { Domain } from './types/schema'

function handleNameRegistered(event: NameRegistered) {
  // Example use of a crypto function
  let id = crypto.keccak256(name).toHexString()

  // Example use of the generated `Entry` class
  let domain = new Domain()
  domain.name = name
  domain.owner = event.params.owner
  domain.timeRegistered = event.block.timestamp

  // Example use of the store API
  store.set('Name', id, entity)
}

Helper Functions for AssemblyScript

Refer to the helper-functions.ts file in this repository for a few common functions that help build on top of the AssemblyScript library, such as byte array concatenation, among others.

API

Documentation on the API can be found here.

For examples of graph-ts in use take a look at one of the following subgraphs:

License

Copyright © 2018 Graph Protocol, Inc. and contributors.

The Graph TypeScript library is dual-licensed under the MIT license and the Apache License, Version 2.0.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

版本与发布记录

@graphprotocol/graph-ts 共发布 323 个版本,首次发布于 2018/10/02;过去 12 个月发布了 0 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.38.2latest2025/10/111.1 年前
0.38.2-alpha-20251008114518-8e14d8751aa282e0adcf518d3004329b68f5e5ddalpha2025/10/081.1 年前
0.38.2-alpha-20251006175524-ab51dfe085c626ddb40f70da993d6393642fe3bf2025/10/071.1 年前
0.38.2-alpha-20250927162837-f36a5d2bf663f76f86b7197fa16a1de259a915ca2025/09/281.1 年前
0.38.2-alpha-20250925210816-9dbcf43153e9c05c36b2aa5828a83235bd1030da2025/09/261.1 年前
0.38.2-alpha-20250925210748-be69e5885a5228ff87a9637cf651afecc18a342e2025/09/261.1 年前

其他发布通道:rc → 0.38.2-rc-20250623204614-108714e736477f550cd8fbe48db7365f0e380db7,alpha → 0.38.2-alpha-20251008114518-8e14d8751aa282e0adcf518d3004329b68f5e5dd。

关于 @graphprotocol/graph-ts 的常见问题

@graphprotocol/graph-ts 是什么?

TypeScript/AssemblyScript library for writing subgraph mappings for The Graph

如何安装 @graphprotocol/graph-ts?

在项目目录运行 npm install @graphprotocol/graph-ts,或使用 pnpm add / yarn add / bun add。

@graphprotocol/graph-ts 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@graphprotocol/graph-ts 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@graphprotocol/graph-ts 可以商用吗?

@graphprotocol/graph-ts 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@graphprotocol/graph-ts 的下载量有多少?

根据 npm 官方下载统计,@graphprotocol/graph-ts 在 2026/10/17 – 2026/11/15 这 30 天内被下载 124,868 次,日均约 4,162 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@graphprotocol/graph-ts 还在维护吗?

最新版本 0.38.2 发布于 2025/10/11(1.1 年前);至今共发布 323 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/10/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。