sublime text 中 graphql 补全失效的根本原因是 schema 源格式(必须为 sdl)、codegen 输出路径与 lsp-graphql 配置三者未严格对齐:schema 必须是 utf-8 无 bom 的 sdl 文件(非 schema.json),codegen.yml 中 schema 路径需带 ./ 前缀,且需启用 typescript-operations 插件并正确配置 documents;sublime build system 必须使用 npx 调用 graphql-codegen 并设 working_dir 为 $project_path;lsp-graphql 要求 graphql.config.yml 在项目根目录,且 js/ts 文件中 gql 模板需独占一行、正确导入。

Sublime Text 本身不生成类型,但能高效触发和验证 graphql-codegen 的执行结果——关键在于 Build System 配置、schema 源格式、以及 TypeScript 输出路径的三方对齐。
graphql-codegen 的 schema 输入必须是 SDL 文本,不是 schema.json
很多团队卡在第一步:用 get-graphql-schema 导出 schema.json 后,graphql-codegen 能跑通但 Sublime 里补全失效。这不是插件问题,而是 LSP-graphql 只读 SDL(即 type Query { ... } 这种纯文本格式),对 introspection JSON 完全无视。
- 正确做法:用
npx @graphql-codegen/cli@latest init初始化时选 SDL;或用apollo schema:download --endpoint=http://localhost:4000/graphql --format=SDL -f src/schema.graphql -
schema.graphql文件必须 UTF-8 编码、无 BOM、只用#注释(不能用//或/* */) - 如果 Schema 拆成多个文件(如
types.graphql、queries.graphql),codegen.yml中要写成数组:schema: ["./src/types.graphql", "./src/queries.graphql"]
Sublime 的 Build System 必须指向正确的 codegen 命令和工作目录
Sublime 的 Ctrl+B 默认不认 graphql-codegen,你得手动建一个 Build System,否则每次改完 Schema 都得切终端敲命令。
完整的Galileo AI平台 TypeScript/JS SDK 参考,用于评估、观察和保护 GenAI 应用。适用于构建 Node.js 或 TypeScript 项目。
- 新建
GraphQL Codegen.sublime-build,内容为:
{
"shell_cmd": "npx graphql-codegen --config codegen.yml",
"working_dir": "$project_path",
"variants": [
{
"name": "Watch",
"shell_cmd": "npx graphql-codegen --config codegen.yml --watch"
}
]
}
-
working_dir必须设为$project_path,否则codegen.yml里相对路径(如schema: ./src/schema.graphql)会解析失败 - 确保项目根目录下有
codegen.yml,且其中generates路径指向 TypeScript 文件(如app/javascript/types/graphql.ts),Sublime 才能实时看到类型变化 - 不要把
node_modules/.bin加进 PATH 试图全局调用 —— Sublime 的 Build System 不继承 shell 的 PATH,npx最稳
LSP-graphql 补全失效的三个常见原因
即使 graphql-codegen 成功生成了 graphql.ts,Sublime 里字段补全还是灰色?大概率是 LSP-graphql 没连上 schema 源。
-
graphql.config.yml必须放在 Project 根目录(即通过Project → Add Folder to Project加入的那个文件夹),不能放在子目录里 - 配置中路径必须带
./前缀:schema: ./src/schema.graphql✅,schema: src/schema.graphql❌(YAML 解析会忽略前导点) - JS/TS 文件里要启用 gql 补全,需同时满足:
"enable_jsx_graphql": true(GraphQL 插件设置)、import { gql } from '@apollo/client'(gql 是导入的命名)、模板字符串独占一行且无拼接(const q = gql`query { user { id } }`✅)
codegen.yml 中的 typescript-operations 插件容易漏配
只生成基础类型(typescript 插件)不够——你写查询时,Sublime 不知道这个 query 返回什么结构。必须加 typescript-operations,它会根据 documents 下的 .graphql 查询文件,生成对应 Operation 接口。
-
documents路径要覆盖所有查询文件:documents: ["src/**/*.graphql", "app/javascript/**/*.graphql"] - 别忘了配
config: skipTypename: true,否则每个对象都带__typename字段,补全干扰大 - 如果用了 Apollo Client,建议加
plugins: [typescript, typescript-operations, typescript-react-query],这样 React Query 的useQuery类型也能自动推导
真正麻烦的不是配置步骤,而是 schema 源、codegen 输出、LSP-graphql 三者的路径和格式必须严丝合缝——少一个 .、多一个 //、错一个 import 别名,补全就静默退化成语法高亮。调试时先确认 schema.graphql 在 Sublime 状态栏显示为 GraphQL 语言,再看 Build System 是否成功生成 graphql.ts,最后检查 LSP 控制台(Ctrl+Shift+P → LSP: Toggle Log Panel)有没有 schema 读取错误。










