Pylon
使用 Pylon 构建 GraphQL API 既简单又直观。Pylon 是一个基于 Hono 的后端框架,它提供了代码优先的 GraphQL API 开发。
GraphQL 架构是根据您的 TypeScript 定义实时生成的,让您能够专注于编写服务逻辑。这种方法显著提高了开发速度,增强了类型安全,并减少了错误。
代码中的任何重大更改都会立即反映在您的 API 中,使您能够立即看到更改如何影响其功能。
查看 Pylon 以获取更多信息。
设置新的 Pylon 服务
Pylon 允许您使用 npm create pylon
命令创建新的服务。此命令将创建一个新的 Pylon 项目,其中包含基本项目结构和配置。在设置过程中,您可以选择首选运行时,例如 Bun、Node.js 或 Cloudflare Workers。
本指南使用 Bun 运行时。
创建新项目
要创建新的 Pylon 项目,请运行以下命令
npm create pylon my-pylon@latest
这将创建一个名为 my-pylon
的新目录,其中包含基本的 Pylon 项目结构。
项目结构
Pylon 项目的结构如下
my-pylon/
├── .pylon/
├── src/
│ ├── index.ts
├── package.json
├── tsconfig.json
.pylon/
: 包含项目生产构建。src/
: 包含项目的源代码。src/index.ts
: Pylon 服务的入口点。package.json
: npm 包配置文件。tsconfig.json
: TypeScript 配置文件。
基本示例
这是一个基本 Pylon 服务的示例
import { app } from '@getcronit/pylon'
export const graphql = {
Query: {
sum: (a: number, b: number) => a + b,
},
Mutation: {
divide: (a: number, b: number) => a / b,
},
}
export default app
保护 API
Pylon 与 ZITADEL(一个云原生身份和访问管理解决方案)集成,为您的 API 提供安全的身份验证和授权。您可以按照 ZITADEL 文档 中概述的步骤轻松保护您的 Pylon API。
创建更复杂的 API
Pylon 允许您通过利用其实时架构生成功能来创建更复杂的 API。有关支持的 TypeScript 类型以及如何定义 API 的更多信息,请参阅 Pylon 文档
此示例演示了如何在 Pylon 中定义复杂类型和服务。通过利用 TypeScript 类和方法,您可以创建与数据库、外部服务和其他资源交互的强大 API。
import { app } from '@getcronit/pylon'
class Post {
id: string
title: string
constructor(id: string, title: string) {
this.id = id
this.title = title
}
}
class User {
id: string
name: string
constructor(id: string, name: string) {
this.id = id
this.name = name
}
static async getById(id: string): Promise<User> {
// Fetch user data from the database
return new User(id, 'John Doe')
}
async posts(): Promise<Post[]> {
// Fetch posts for this user from the database
return [new Post('1', 'Hello, world!')]
}
async $createPost(title: string, content: string): Promise<Post> {
// Create a new post for this user in the database
return new Post('2', title)
}
}
export const graphql = {
Query: {
user: User.getById,
},
Mutation: {
createPost: (userId: string, title: string, content: string) => {
const user = User.getById(userId)
return user.$createPost(title, content)
},
},
}
export default app
调用 API
可以使用任何 GraphQL 客户端库调用 Pylon API。出于开发目的,建议使用 Pylon Playground,它是一个基于 Web 的 GraphQL IDE,允许您实时与您的 API 交互。
- 通过在项目目录中运行
bun run dev
启动 Pylon 服务器。 - 通过导航到
http://localhost:3000/graphql
在浏览器中打开 Pylon Playground。 - 在左侧窗格中编写您的 GraphQL 查询或变异。
访问 Hono 上下文
您可以通过使用 getContext
函数在代码中的任何位置访问 Hono 上下文。此函数返回当前上下文对象,其中包含有关请求、响应和其他上下文特定数据的信息。
import { app, getContext } from '@getcronit/pylon'
export const graphql = {
Query: {
hello: () => {
const context = getContext()
return `Hello, ${context.req.headers.get('user-agent')}`
},
},
}
export default app
有关 Hono 上下文对象及其属性的更多信息,请参阅 Hono 文档 和 Pylon 文档。
Hono 在哪里适合?
Pylon 基于 Hono,Hono 是一个轻量级 Web 框架,用于构建 Web 应用程序和 API。Hono 提供了用于处理 HTTP 请求和响应的核心功能,而 Pylon 扩展了此功能以支持 GraphQL API 开发。
除了 GraphQL,Pylon 还允许您访问底层的 Hono 应用程序实例以添加自定义路由和中间件。这使您能够构建利用 Hono 的全部功能的更复杂的 API 和服务。
import { app } from '@getcronit/pylon'
export const graphql = {
Query: {
sum: (a: number, b: number) => a + b,
},
Mutation: {
divide: (a: number, b: number) => a / b,
},
}
// Add a custom route to the Pylon app
app.get('/hello', (ctx, next) => {
return new Response('Hello, world!')
})
结论
Pylon 是一个强大的 Web 框架,简化了 GraphQL API 的开发。通过利用 TypeScript 类型定义,Pylon 提供了实时架构生成,增强了类型安全并减少了错误。使用 Pylon,您可以快速构建满足您业务需求的安全且可扩展的 API。Pylons 与 Hono 的集成使您能够在专注于 GraphQL API 开发的同时使用 Hono 的所有功能。
有关 Pylon 的更多信息,请查看 官方文档。