风向
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>