# @good-idea/unwind-edges 中文文档

规范地址：https://npmcn.com/package/%40good-idea/unwind-edges/

版本：1\.5\.3
npm 元数据更新时间：2022-04-05T14:58:51.852Z
中文译文更新时间：2026-11-16T19:20:48.593Z
软件包许可：MIT

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

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

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

- 包体积（解压后）：26.4 kB，14 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=8\.9
- 运行依赖：0 个
- 首次发布：2019-05-27T01:27:06.926Z
- 最新版本发布：2020-11-08T21:51:29.305Z
- 近 30 天下载量：2883（2026-10-17 至 2026-11-15，npm 官方下载统计）
- 已发布版本数：13（近 12 个月 0 个）

## 中文简介

用于提取相接图QL数据的简单工具

## 安装

```sh
npm install @good-idea/unwind-edges
```

也可以使用：`pnpm add @good-idea/unwind-edges`、`yarn add @good-idea/unwind-edges`、`bun add @good-idea/unwind-edges`

## README

[![Codecov Coverage](https://img.shields.io/codecov/c/github/good-idea/unwind-edges/master.svg?style=flat-square)](https://codecov.io/gh/good-idea/unwind-edges/) ![](https://img.badgesize.io/https://unpkg.com/@good-idea/unwind-edges/lib/index.js.svg) ![](https://img.badgesize.io/https://unpkg.com/@good-idea/unwind-edges/lib/index.js.svg?compression=gzip)

# 风向

`uwindEdges` 是一个简单的工具,用于从GraphQL 回复中提取与 [中继光标连接光谱](https://facebook.github.io/relay/graphql/connections.htm)。这个结构对于处理您的请求中的页标非常方便,但在此之后,您通常只需要节点.

你也可以使用这个 `paginate` 导出以将一个节点数组转换成被相接的对象.

# 使用量

## `unwindEdges`

使用光标连接并返回一个(1) 简单节点数组的倒数, 并附加一个 `__cursor` 属性,和 (2)原始页面信息.

示例

```ts
import { unwindEdges } from '@good-idea/unwind-edges'

const response = {
  allUsers: {
    pageInfo: {
      hasNextPage: true,
      hasPreviousPage: false,
    },
    edges: [
      {
        cursor: 'x123',
        node: {
          id: 'abc',
          name: 'frank',
        },
      },
      {
        cursor: 'y234',
        node: {
          id: 'def',
          name: 'ursula',
        },
      },
      {
        cursor: 'z456',
        node: {
          id: 'ghi',
          name: 'ira',
        },
      },
    ],
  },
}

const [users, { pageInfo, firstCursor, lastCursor }] = unwindEdges(response.allUsers)

console.log(users)
/**
 *  [
 *    { id: 'abc', name: 'frank', __cursor: 'x123' },
 *    { id: 'def', name: 'ursula', __cursor: 'y234' },
 *    { id: 'ghi', name: 'ira', __cursor: 'z456' }
 *  ]
 */

console.log(pageInfo)
/**
 *  {
 *    hasPreviousPage: false,
 *    hasNextPage: true
 *  }
 */

console.log(firstCursor) // => 'x123'
console.log(lastCursor) // => 'x123'
```

## `paginate`

使用一系列对象并返回中继器式连接对象.

示例

```js
import { paginate } from '@good-idea/unwind-edges'
const users = [
  { id: 'abc', name: 'frank' },
  { id: 'def', name: 'ursula' },
  { id: 'ghi', name: 'ira' },
]

const paginatedUsers = paginate(users)

console.log(paginatedUsers)

/**
 *  {
 *    edges: [
 *      {
 *        cursor: 'abc',
 *        node: {
 *          id: 'abc',
 *          name: 'frank'
 *        }
 *      },
 *      {
 *        cursor: 'def',
 *        node: {
 *          id: 'def',
 *          name: 'ursula'
 *        }
 *      },
 *      {
 *        cursor: 'ghi',
 *        node: {
 *          id: 'ghi',
 *          name: 'ira'
 *        }
 *      },
 *    ],
 *    pageInfo: {
 *      hasNextPage: false,
 *      hasPrevPage: false,
 *      hasPreviousPage: false,
 *      firstCursor: 'abc',
 *      lastCursor: 'ghi'
 *    }
 *  }
 */
```

### 倾角参数

您也可以指定 `first` 财务报告和审定财务报表 `after` 参数以返回有限的节点选择.

```js
const users = [
  { id: 1, name: 'frank' },
  { id: 2, name: 'ursula' },
  // ...
  { id: 50, name: 'ira' },
]

const paginated = paginate(users, { first: 10 })

console.log(paginated)
/**
 *  {
 *    edges: [
 *      {
 *        cursor: 'abc',
 *        node: {
 *          id: 'abc',
 *          name: 'frank'
 *        }
 *      },
 *      {
 *        cursor: 'def',
 *        node: {
 *          id: 'def',
 *          name: 'ursula'
 *        }
 *      },
 *      // ...
 *      {
 *        cursor: 'xyz',
 *        node: {
 *          id: 'xyz',
 *          name: 'fancy'
 *        }
 *      },
 *    ],
 *    pageInfo: {
 *      hasNextPage: true,
 *      hasPrevPage: false,
 *      hasPreviousPage: false,
 *      firstCursor: 'abc',
 *      lastCursor: 'xyz'
 *    }
 *  }
 */
```

为了保证 `hasNextPage` 返回适当的值,确保通过 _更多_ 结点比您指定的参数 `first`例如,从数据库中取出项目时,通常的做法是将项目比作项目.

```
const users = getUsersFromDB({ first: 11 })

const paginated = paginate(users, { first: 10 })

console.log(paginated.pageInfo.hasNextPage)
// `true` if your database returned more than 10 users
// `false` if your database returned 10 users or less
```

`hasPrevPage` 编号 : `hasPreviousPage` 将返回 `true` 如果您指定一个 `after` 参数 :

```
const users = getUsersFromDB({ first: 50 })

const paginated = paginate(users, { first: 10, after: 'def' })

console.log(paginated.pageInfo.hasPrevPage) // => true
console.log(paginated.pageInfo.firstCursor) // => 'ghi'
```

### 光标

`paginate` 将自动查找 `id` 或者说 `_id` 在您的节点上进行提取 `cursor`。如果需要从另一个属性中取出光标,则可以在 `cursorKey` 第三参数中的属性 :

```
const users = [
  { id: 'abc', name: 'frank', },
  { id: 'def', name: 'ursula' },
  { id: 'ghi', name: 'ira' }
]

const paginated = paginate(users, {}, { cursorKey: 'name' })

console.log(paginated.edges[0].cursor) // => 'frank'
```

# 类型标记使用

如果你在Typescript中工作,你可以通过一个额外的类型参数来告诉TS你会得到什么:

```ts
interface User {
  id: string
  name: string
}

const [users] = unwindEdges<User>(sampleResponse)

console.log(users[0].id) // 👍
console.log(users[0].name) // 👍
console.log(users[0].__cursor) // 👍
console.log(users[0].birthday) // ❗️ Property 'birthday' does not exist on type 'NodeWithCursor<User>'.
```

该模块还输出 `Paginated<T>` 接口,如果想要为中继连接创建接口,可以方便:

```
type UserConnection = Paginated<User>
```

## 常见问题

### @good-idea/unwind-edges 是什么？

用于提取相接图QL数据的简单工具

### 如何安装 @good-idea/unwind-edges？

在项目目录运行 npm install @good-idea/unwind-edges，或使用 pnpm add / yarn add / bun add。

### @good-idea/unwind-edges 支持 TypeScript 吗？

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

### @good-idea/unwind-edges 支持 ESM 还是 CommonJS？

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

### @good-idea/unwind-edges 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>=8\.9。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @good-idea/unwind-edges 可以商用吗？

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

### @good-idea/unwind-edges 的下载量有多少？

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

### @good-idea/unwind-edges 还在维护吗？

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

