# @grantjs/schema 中文文档

规范地址：https://npmcn.com/package/%40grantjs/schema/

版本：1\.29\.0
npm 元数据更新时间：2026-11-10T14:39:26.396Z
中文译文更新时间：2026-11-17T05:23:29.141Z
软件包许可：MIT

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

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

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

- 包体积（解压后）：959 kB，654 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：1 个
- 首次发布：2026-03-11T22:38:27.583Z
- 最新版本发布：2026-11-10T14:39:25.862Z
- 近 30 天下载量：6084（2026-10-17 至 2026-11-15，npm 官方下载统计）
- 已发布版本数：69（近 12 个月 69 个）

## 中文简介

Grant 图QL 计划和生成的类型

## 安装

```sh
npm install @grantjs/schema
```

也可以使用：`pnpm add @grantjs/schema`、`yarn add @grantjs/schema`、`bun add @grantjs/schema`

## README

# @ granjs/ schema (英语)

GraphQL 计划并为 Grant 生成类型 .

## 概览

此套件包含:

-   图QL 计划定义
-   生成类型脚本
-   图QL 操作和解析器
-   其他特定平台类型

## 安装

```bash
npm install @grantjs/schema
```

## 使用量

### 基本类型

```typescript
import { User, Role, Permission, Organization, Project } from '@grantjs/schema';

const user: User = {
  id: '1',
  email: 'user@example.com',
  // ... other properties
};
```

### 图QL 操作

```typescript
import { GetUsersQuery, CreateUserMutation } from '@grantjs/schema';

// Use in your GraphQL operations
const GET_USERS = gql`
  query GetUsers {
    users {
      id
      email
      roles {
        id
        name
      }
    }
  }
`;
```

### 解析器

```typescript
import { Resolvers } from '@grantjs/schema';

const resolvers: Resolvers = {
  Query: {
    users: () => {
      // Your resolver implementation
    },
  },
};
```

## 发展

### 生成类型

```bash
# Generate types from GraphQL schema
pnpm generate

# Watch for changes and regenerate
pnpm dev
```

在下方生成输出 `src/generated/` 承诺,必须与其 来源。 CI 运行 `pnpm codegen:check` (再生成,在任何 diff 上失败),所以编辑 `.graphql` 文档或 `codegen.ts` 从来没有 `src/generated/` 直接说.

### 测试

```bash
pnpm test
```

对合并的SDL的结构性断言:它构建,每个操作文件 和无法接触到的种类数 `Query`编号 :`Mutation` 被钉住。 见 `src/sdl-contract.test.ts`。 。 。 .

### 构建软件包

```bash
pnpm build
```

## Schema 结构

```
src/
├── schema/                   # GraphQL SDL — one directory per domain, each with
│   │                         # inputs/, types/ and (where the domain is on the graph)
│   │                         # queries/ and mutations/
│   ├── base.graphql          # Scalars, shared interfaces, Tenant/SortOrder, Scope
│   └── root.graphql          # Query/Mutation roots the domains extend
├── operations/               # Client operation documents, one directory per domain
├── generated/                # Codegen output — committed, never hand-edited
│   ├── schema-types.ts       # Schema types (owns every type name)
│   ├── graphql.ts            # Operation types + typed document nodes
│   └── resolvers.ts          # Resolver types; imports from schema-types
├── events/                   # Event catalog and envelope types
├── cdm/, notifications/,     # Hand-written contract types that are not codegen'd
│   webhooks/
└── index.ts                  # Public barrel
```

`apps/api` 装入每个文件 `src/schema/` 输入 `makeExecutableSchema`这样 任何船只在船上都声明 是否收到询问.

## 捐款

修改 GraphQL 方案时:

1.  更新相关的 `.graphql` 文件在 `src/schema/`
2.  运行 `pnpm generate` 重新生成类型
3.  更新任何受影响的解析器或操作
4.  测试更改

## 许可证

麻省理工学院执照 - 看 [自由主义](https://npmcn.com/package/LICENSE) 详细情况.

## 常见问题

### @grantjs/schema 是什么？

Grant 图QL 计划和生成的类型

### 如何安装 @grantjs/schema？

在项目目录运行 npm install @grantjs/schema，或使用 pnpm add / yarn add / bun add。

### @grantjs/schema 支持 TypeScript 吗？

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

### @grantjs/schema 支持 ESM 还是 CommonJS？

以 ES Module 形式发布，推荐使用 import 引入。在 CommonJS 项目中，Node\.js 20\.19\+ / 22\.12\+ 可以直接 require\(\) 不含顶层 await 的 ES Module，更早的版本需要改用动态 import\(\)。

### @grantjs/schema 可以商用吗？

@grantjs/schema 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @grantjs/schema 的下载量有多少？

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

### @grantjs/schema 还在维护吗？

最新版本 1\.29\.0 发布于 2026/11/10（6 天前）；至今共发布 69 个版本，其中过去 12 个月发布了 69 个；首次发布于 2026/03/12。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

