REST 转 GraphQL:自动化 API Schema 生成 - Openclaw Skills

作者:互联网

2026-04-14

AI教程

什么是 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 转 GraphQL 转换器 工作原理
  1. 该工具扫描包含 REST 路由定义的指定目录或文件。
  2. 解析 HTTP 方法和 URI 模式,以识别资源和 CRUD 操作。
  3. GET 端点自动映射为 GraphQL 查询,而 POST、PUT 和 DELETE 方法则转换为变更。
  4. 分析请求和响应体以推断数据结构并生成相应的 GraphQL 类型。
  5. 该工具生成一个 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.