ZeroTool Workbench
GraphQL 格式化工具
免费在线 GraphQL 格式化工具。在浏览器中美化、压缩并校验 query、mutation 与 SDL 架构,按行列定位语法错误。零上传,零注册。
使用步骤
- 把 GraphQL query、mutation、subscription 或 SDL 架构粘进输入框,或点击 载入示例 从样例开始。
- 点击 格式化 美化、压缩 缩短,或 校验 仅检查语法。
- 在 2 / 4 空格之间切换缩进,默认 2 空格与 graphql-js 原生输出一致。
- 点 复制 或 下载 .graphql 带走结果。按 Cmd/Ctrl + Enter 可用键盘触发格式化。
实际示例
格式化前
query GetUser($id:ID!,$withPosts:Boolean!){user(id:$id){id name email posts @include(if:$withPosts){...PostSummary}}} fragment PostSummary on Post{id title publishedAt}
格式化后
query GetUser($id: ID!, $withPosts: Boolean!) {
user(id: $id) {
id
name
email
posts @include(if: $withPosts) {
...PostSummary
}
}
}
fragment PostSummary on Post {
id
title
publishedAt
}
SDL 架构
type Post {
id: ID!
title: String!
publishedAt: DateTime
author: User!
}
type Query {
post(id: ID!): Post
posts(authorId: ID, limit: Int = 20): [Post!]!
}
为什么要格式化 GraphQL?
一致的 GraphQL 格式直接影响 code review、schema diff 与 .graphql 文件入库。压缩后的 query 在没有 persisted query 时能减少线上传输体积;美化后的 query 在 PR 里更易扫读。把 query 单独拿出来做校验,可以在不发请求的情况下秒级定位 fragment 拼写错误。
实现原理
本工具在浏览器中跑 graphql-js(GraphQL Foundation 维护的参考实现)。parse() 从文本构建 AST 并在首个语法错误处报出行列号;print() 把 AST 序列化为规范 GraphQL;stripIgnoredCharacters() 按 spec 移除所有 ignored token,得到压缩形式。整个过程零网络往返,数据完全留在本地。
FAQ
支持哪些 GraphQL 语法结构?
支持 query、mutation、subscription、fragment、inline fragment、variable、directive(@include / @skip / @deprecated 与自定义)以及 SDL 类型系统定义(type / interface / union / enum / input / scalar / extend / schema)。底层使用官方 `graphql` 参考实现,凡是 graphql-js 接受的都接受。
我的 schema 或查询会被上传吗?
不会。解析、打印、校验全部在你的浏览器标签页内完成,数据不会离开设备。本工具按设计就没有上传接口。
能针对远端 schema 做类型校验吗?
仅做语法级校验。本工具只检查文本是否符合 GraphQL 语法,不验证字段是否存在于某个具体 schema。需要类型感知的校验请在本地运行 `graphql-cli`、`apollo client:check` 或 `graphql-inspector` 对接你的 endpoint。
最大支持多大输入?
10 MB 硬上限。GraphQL operation 很少能写到这种体量;如果你的输入确实超过,多半是 schema introspection dump,请改用本地的 `prettier --parser graphql`。
Minify 和 Format 有什么区别?
Minify 调用 graphql-js 的 `stripIgnoredCharacters`,按 GraphQL spec 移除所有 ignored token(空白与注释),用于线上传输。Format 则把 AST 重新打印为 2 或 4 空格缩进,便于人读。