棱镜-中继-曲线连接
扩展 普丽斯玛 findMany 支持的方法 中继光标连接
安装 二. 支助 使用量 二. 支助 捐款 二. 支助 贡献者 二. 支助 许可证
安装
yarn add @devoxa/prisma-relay-cursor-connection
此模块对等端依赖 @prisma/client您可以在
软件包.json (单位:千美元)peerDependencies) (中文(简体) ).
使用量
一般使用
这个模块验证连接参数以确保它们与Prisma合作. 以下内容: 组合得到支持 :
{}所有资源{ first: number }第一个X资源{ first: number, after: string }Id Y 之后的第一个 X 资源{ last: number }最后一个 X 资源{ last: number, before: string }Id Y 之前的最后 X 资源
如果您正在通过用户提供的数据,则需要在代码中检查两例,以防止 用户同时读取过多的资源 :
- 一个
first|last必须定义 first|last必须低于合理上限(例如100个)
import {
findManyCursorConnection,
ConnectionArguments,
} from '@devoxa/prisma-relay-cursor-connection'
const result = await findManyCursorConnection(
(args) => client.todo.findMany(args),
() => client.todo.count(),
{ first: 5, after: '5c11e0fa-fd6b-44ee-9016-0809ee2f2b9a' } // typeof ConnectionArguments
)
类型安全参数
您也可以使用额外的 FindManyArgs 在保持类型安全的前提下:
import { findManyCursorConnection } from '@devoxa/prisma-relay-cursor-connection'
const baseArgs = {
select: { id: true, isCompleted: true },
where: { isCompleted: true },
}
const result = await findManyCursorConnection(
(args) => client.todo.findMany({ ...args, ...baseArgs }),
() => client.todo.count({ where: baseArgs.where }),
{ last: 5, before: '5c11e0fa-fd6b-44ee-9016-0809ee2f2b9a' }
)
// Type error: Property text does not exist
result.edges[0].node.text
自定义光标
默认情况下,光标是 id 区域。 如果你想用另一个领域,
复合索引,或处理编码/解码,可以通过下列选项:
import { findManyCursorConnection } from '@devoxa/prisma-relay-cursor-connection'
const result = await findManyCursorConnection(
(args) => client.todo.findMany(args),
() => client.todo.count(),
{ first: 5, after: 'eyJpZCI6MTZ9' },
{
getCursor: (record) => ({ id: record.id }),
encodeCursor: (cursor) => Buffer.from(JSON.stringify(cursor)).toString('base64'),
decodeCursor: (cursor) => JSON.parse(Buffer.from(cursor, 'base64').toString('ascii')),
}
)
您可以在 单位测试。 。 。 .
自定义边缘和节点
默认情况下,边缘由 cursor 编辑 node。如果您想要添加
字段到边缘或节点,您可以通过以下选项:
import { findManyCursorConnection } from '@devoxa/prisma-relay-cursor-connection'
const result = await findManyCursorConnection(
(args) => client.todo.findMany(args),
() => client.todo.count(),
{ first: 5, after: 'eyJpZCI6MTZ9' },
{
recordToEdge: (record) => ({
node: { ...record, extraNodeField: 'Foo' },
extraEdgeField: 'Bar',
}),
}
)
箱外的这个会为您推断出修改过的类型 .
解析信息
您可以将 GraphQL 解析信息传送到可自动删除额外 Prisma 选项中
查询您 GraphQL 查询中没有的字段。 这主要有用,如果你不是
使用 totalCount 您的页码逻辑或您只想要查询 totalCount 没有
边缘.
import { findManyCursorConnection } from '@devoxa/prisma-relay-cursor-connection'
import { GraphQLResolveInfo } from 'graphql'
const resolveInfo: GraphQLResolveInfo = {
// ...
}
const result = await findManyCursorConnection(
(args) => client.todo.findMany(args),
() => client.todo.count(),
{ first: 5, after: '5c11e0fa-fd6b-44ee-9016-0809ee2f2b9a' },
{ resolveInfo }
)
捐款
# Setup the test database
yarn prisma migrate dev --preview-feature
# Run the tests
yarn test
贡献者
感谢这些伟大的人emoji 密钥:
大卫·瑞斯 💻 📖 ⚠️ |
肖恩·马西森 💻 ⚠️ |
马克 💻 |
贞成大 💻 ⚠️ |
亚美特·维萨尔 💻 ⚠️ |
尼克·兰德尔 💻 |
伊戈尔·乌尔明切克 📖 |
亚历克斯·施里普夫 💻 |
玛丽娜·瑞拉 💻 |
卡尔文·瓦斯克兹 💻 ⚠️ |
杰夫·顾康 📖 |
Orta 热力学 💻 📖 ⚠️ |
卡来姆-梅德哈特 💻 |
本项目遵循 全部贡献者 规格。 任何捐款都欢迎!
许可证
麻省理工学院