REST 转 GraphQL:自动化 API Schema 生成 - Openclaw Skills
作者:互联网
2026-04-14
什么是 REST 转 GraphQL 转换器?
REST 转 GraphQL 转换器是一款高性能实用工具,旨在简化 Web 服务的现代化进程。通过 Openclaw Skills 将此工具集成到您的工作流中,您可以以编程方式将复杂的 RESTful 架构翻译成结构化的 GraphQL Schema,包括查询(Queries)、变更(Mutations)和类型(Types)。它消除了繁琐的手动类型映射任务,让开发者能够专注于构建功能而非样板代码。
该工具对于希望实现后端到前端(BFF)模式或正在进行完整架构迁移的团队特别有价值。它在保留现有业务逻辑完整性的同时,提供了基于图的查询语言的灵活性和效率。
下载入口:https://github.com/openclaw/skills/tree/main/skills/lxgicstudios/rest-to-graphql
安装与下载
1. ClawHub CLI
从源直接安装技能的最快方式。
npx clawhub@latest install rest-to-graphql
2. 手动安装
将技能文件夹复制到以下位置之一
全局模式~/.openclaw/skills/
工作区
/skills/
优先级:工作区 > 本地 > 内置
3. 提示词安装
将此提示词复制到 OpenClaw 即可自动安装。
请帮我使用 Clawhub 安装 rest-to-graphql。如果尚未安装 Clawhub,请先安装(npm i -g clawhub)。
REST 转 GraphQL 转换器 应用场景
- 无需手动重写,将旧有的 REST API 迁移到基于 GraphQL 的架构。
- 在现有微服务之上构建 GraphQL 网关或 BFF 层。
- 通过将当前 REST 端点可视化为图,快速原型化数据模型。
- 生成 TypeScript 类型和解析器存根(Resolver Stubs),以确保迁移过程中的端到端类型安全。
- 该工具扫描包含 REST 路由定义的指定目录或文件。
- 解析 HTTP 方法和 URI 模式,以识别资源和 CRUD 操作。
- GET 端点自动映射为 GraphQL 查询,而 POST、PUT 和 DELETE 方法则转换为变更。
- 分析请求和响应体以推断数据结构并生成相应的 GraphQL 类型。
- 该工具生成一个 Schema 文件,并可选地生成将请求代理到现有 REST 处理程序的解析器存根。
REST 转 GraphQL 转换器 配置指南
REST 转 GraphQL 转换器需要 Node.js 18 或更高版本。作为 Openclaw Skills 工具包的一部分,您可以通过 npx 直接运行它,无需永久安装:
# 处理整个路由目录
npx ai-rest-to-graphql ./src/routes
# 处理特定的资源文件
npx ai-rest-to-graphql ./src/routes/users.ts
# 查看所有配置选项
npx ai-rest-to-graphql --help
REST 转 GraphQL 转换器 数据架构与分类体系
该工具根据您的源 REST 架构将输出组织成结构化的 GraphQL 组件:
| 输出组件 | 描述 |
|---|---|
| Schema 定义 | 包含所有推断类型、查询和变更的 .graphql 文件。 |
| 解析器存根 | 将 GraphQL 层桥接到现有 REST 路由的可选实现文件。 |
| TypeScript 映射 | 用于整个堆栈中一致数据处理的推断类型定义。 |
name: rest-to-graphql
description: Convert REST API routes to a GraphQL schema. Use when migrating APIs or adding a GraphQL layer.
REST to GraphQL Converter
Got a REST API and want GraphQL? Point this at your routes and get a complete schema with types, queries, and mutations. No manual translation required.
One command. Zero config. Just works.
Quick Start
npx ai-rest-to-graphql ./src/routes
What It Does
- Analyzes your REST endpoints and generates equivalent GraphQL schema
- Converts CRUD operations to queries and mutations
- Generates TypeScript types alongside the schema
- Maps REST resources to GraphQL types with proper relationships
- Includes resolver stubs that call your existing REST handlers
Usage Examples
# Convert routes directory
npx ai-rest-to-graphql ./src/routes
# Single resource
npx ai-rest-to-graphql ./src/routes/users.ts
# Include resolver implementations
npx ai-rest-to-graphql ./src/routes --with-resolvers
# Output to specific file
npx ai-rest-to-graphql ./src/routes -o ./schema.graphql
# Keep REST as datasource
npx ai-rest-to-graphql ./src/routes --wrap-rest
Best Practices
- Start with core resources - Don't convert everything at once
- Review the generated types - AI maps fields but check the relationships
- Use as a migration guide - The output shows you the GraphQL equivalent
- Consider a gateway approach - Wrap REST with GraphQL instead of replacing
When to Use This
- Adding GraphQL to an existing REST API
- Exploring what your API would look like in GraphQL
- Building a BFF layer over microservices
- Learning GraphQL by seeing your own data modeled
Part of the LXGIC Dev Toolkit
This is one of 110+ free developer tools built by LXGIC Studios. No paywalls, no sign-ups, no API keys on free tiers. Just tools that work.
Find more:
- GitHub: https://github.com/LXGIC-Studios
- Twitter: https://x.com/lxgicstudios
- Substack: https://lxgicstudios.substack.com
- Website: https://lxgic.dev
Requirements
No install needed. Just run with npx. Node.js 18+ recommended.
npx ai-rest-to-graphql --help
How It Works
The tool parses your REST route definitions to understand your resources and operations. It maps GET endpoints to queries, POST/PUT/DELETE to mutations, and infers type structures from your request/response patterns.
License
MIT. Free forever. Use it however you want.
相关推荐
专题
+ 收藏
+ 收藏
+ 收藏
+ 收藏
+ 收藏
+ 收藏
最新数据
相关文章
Arayun 173 蓝丝黛尔石执行官:AI 执法技能 - Openclaw Skills
乐享:腾讯知识库 API 集成 - Openclaw Skills
Auto Logger:持久化记忆与活动追踪 - Openclaw Skills
小红书 AI 内容创作与自动发布 - Openclaw Skills
Google 搜索:AI 驱动的网络智能 - Openclaw Skills
记忆管理器:三层上下文系统 - Openclaw Skills
堆肥追踪器:管理有机废物分解 - Openclaw Skills
Satori Memory: 为 Openclaw Skills 提供持久的长期上下文
Palantir LLM 提示词工程:Web 应用框架总结 - Openclaw Skills
动画产品解说视频:AI 视频提示词模板 - Openclaw Skills
AI精选
