# @graphprotocol/graph-ts 中文文档

规范地址：https://npmcn.com/package/%40graphprotocol/graph-ts/

版本：0\.38\.2
npm 元数据更新时间：2026-09-09T22:30:52.189Z
中文译文更新时间：2026-11-16T22:55:03.065Z
软件包许可：未声明

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40graphprotocol/graph-ts
原文对照：https://npmcn.com/package/%40graphprotocol/graph-ts/?lang=original
项目主页：https://github.com/graphprotocol/graph-tooling#readme
源码仓库：https://github.com/graphprotocol/graph-tooling

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：157 kB，26 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：1 个
- 首次发布：2018-10-02T09:06:05.075Z
- 最新版本发布：2025-10-10T16:45:02.092Z
- 近 30 天下载量：124868（2026-10-17 至 2026-11-15，npm 官方下载统计）
- 已发布版本数：323（近 12 个月 0 个）

## 中文简介

TypeScript/ AssemblyScript 用于为图形编写子图映射库

## 安装

```sh
npm install @graphprotocol/graph-ts
```

也可以使用：`pnpm add @graphprotocol/graph-ts`、`yarn add @graphprotocol/graph-ts`、`bun add @graphprotocol/graph-ts`

## README

# 图形类型脚本库( 图形- ts)

[![npm (scoped)](https://img.shields.io/npm/v/@graphprotocol/graph-ts.svg)](https://www.npmjs.com/package/@graphprotocol/graph-ts) [![Build Status](https://travis-ci.org/graphprotocol/graph-ts.svg?branch=master)](https://travis-ci.org/graphprotocol/graph-ts)

TypeScript/ AssemblyScript 用于编写要部署到的子图映射库 [图表](https://github.com/graphprotocol/graph-node)。 。 。 .

## 使用量

关于如何创建子图的详细指南,请参见: [图 CLI 文件](https://github.com/graphprotocol/graph-cli)。 。 。 .

创建子图的一个步骤是写出将处理块链事件和将 将实体写入商店。 这些映射用TypeScript/AssemblyScript来写.

这个 `graph-ts` 库提供 API 访问 Graph 节点存储、块链数据、 智能 合同、IPFS数据、密码功能等等。 要用它,你只需要加一个 依赖它:

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

之后,您可以导入 `store` API 和您绘图中此库的其他特性 。 A 类 少数实例:

```typescript
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)
}
```

## Assembly 脚本的辅助函数

参见 `helper-functions.ts` 文件输入 [这个](https://github.com/graphprotocol/graph-tooling/blob/main/packages/ts/helper-functions.ts) 用于一些有助于在AssemblyScript 库上建立的共同功能的仓库,例如 字节数组连接等.

## API 密码

关于API的文档可以找到 [这里](https://thegraph.com/docs/en/developer/assemblyscript-api/)。 。 。 .

例如: `graph-ts` 在使用中,请查看以下子图之一:

-   [https://github.com/graphprotocol/ens-subgraph (中文(简体) )](https://github.com/graphprotocol/ens-subgraph)
-   [https://github.com/graphprotocol/decentraland-subgraph (中文(简体) )](https://github.com/graphprotocol/decentraland-subgraph)
-   [https://github.com/graphprotocol/adchain-subgraph (中文(简体) )](https://github.com/graphprotocol/adchain-subgraph)
-   [https://github.com/graphprotocol/0x-子图](https://github.com/graphprotocol/0x-subgraph)
-   [https://github.com/graphprotocol/aragon-subgraph (中文(简体) )](https://github.com/graphprotocol/aragon-subgraph)
-   [https://github.com/graphprotocol/dharma-subgraph 页面存档备份,存于互联网档案馆](https://github.com/graphprotocol/dharma-subgraph)

## 许可证

版权所有 2018 Graph Protocol, Inc.和撰稿人.

图形类型脚本库在 [麻省理工学院许可证](https://github.com/graphprotocol/graph-tooling/blob/main/LICENSE-MIT) 编辑 [Apache 许可证,2.0版本](https://github.com/graphprotocol/graph-tooling/blob/main/LICENSE-APACHE)。 。 。 .

除非适用法律要求或书面同意,否则根据许可证分发的软件 在 "AS IS " BASIS 上分发,不含任何种类的警报或条件,或明示或 隐含。 有关《公约》下许可和限制的具体语言,请参看《公约》 执照.

## 常见问题

### @graphprotocol/graph-ts 是什么？

TypeScript/ AssemblyScript 用于为图形编写子图映射库

### 如何安装 @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 和提交记录判断。

