@good-idea/unwind-edges

中文文档
1.5.3发布于 2020/11/09MITTypeScriptESM + CJS零依赖近 30 天下载 2883

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

中文使用文档
文档目录4 节
  1. unwindEdges
  2. paginate
  3. 倾角参数
  4. 光标

Codecov Coverage

风向

uwindEdges 是一个简单的工具,用于从GraphQL 回复中提取与 中继光标连接光谱。这个结构对于处理您的请求中的页标非常方便,但在此之后,您通常只需要节点.

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

使用量

unwindEdges

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

示例

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

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

示例

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 参数以返回有限的节点选择.

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你会得到什么:

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 共发布 13 个版本,首次发布于 2019/05/27;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.5.3latest2020/11/096 年前
1.5.22020/10/086 年前
1.5.12020/10/076 年前
1.5.02020/10/076 年前
1.4.02020/04/306 年前
1.3.02019/11/107 年前

关于 @good-idea/unwind-edges 的常见问题

@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 和提交记录判断。