@good-idea/unwind-edges

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

Simple utilty for extracting paginated GraphQL data

原始使用文档
文档目录4 节
  1. unwindEdges
  2. paginate
  3. Pagination Arguments
  4. Cursors

Codecov Coverage

unwind-edges

uwindEdges is a simple utility for extracting nodes from GraphQL responses that are paginated with the Relay Cursor Connection spec. This structure is handy for handling pagination in your requests, but after that, you usually just want the nodes.

You can also use the paginate export to transform an array of nodes into a paginated object.

usage

unwindEdges

Takes a Cursor Connection and returns a tuple of (1) simple array of the nodes, with an additional __cursor property, and (2) the original page info.

Example:

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

Takes an array of objects and returns a Relay-style connection object.

Example:

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'
 *    }
 *  }
 */

Pagination Arguments

You can also specify first and after arguments to return a limited selection of nodes.

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'
 *    }
 *  }
 */

In order to make sure that hasNextPage returns the proper value, be sure to pass in more nodes than you specify as your argument to first. For instance, when fetching items from a database, a common practice is to overfetch by one.

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 will return true if you specify an after argument:

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'

Cursors

paginate will automatically look for an id or _id property on your node to extract as it's cursor. If you need to extract a cursor from another property, you can pass in a cursorKey property in the third parameter:

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 Usage

If you're working in Typescript, you can pass in an additional type argument to tell TS what you're going to get back:

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>'.

The module also exports the Paginated<T> interface, which can be handy if you want to create an interface for a Relay Connection:

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 是什么?

Simple utilty for extracting paginated GraphQL data

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