monaco-graphql 如何为 JSON 变量编辑器启用 schema 驱动的校验(validateVariablesJson)?
monaco-graphql 如何为 JSON 变量编辑器启用 schema 驱动的校验validateVariablesJson【免费下载链接】graphiqlGraphiQL the GraphQL LSP Reference Ecosystem for building browser IDE tools.项目地址: https://gitcode.com/GitHub_Trending/gr/graphiql用monaco-graphql自建类 VS Code 的 GraphQL Web IDE 时通常会有两个编辑器一个编辑 GraphQL 操作.graphqlmodel另一个编辑以 JSON 形式提交的 variables。想让 variables 编辑器里的值按照操作声明的变量类型做 schema 驱动校验缺必填变量、类型不符时出诊断并能获得补全入口是MonacoGraphQLAPI的setDiagnosticSettings()方法其中的键就是标题里的validateVariablesJson——源码类型定义中的实际拼写是validateVariablesJSON。开始之前需要明确两点适用前提均来自仓库文档monaco-graphql处于 pre-release 状态README 明确提示 codemirror-graphql 拥有更多功能包括 JSON variables validation且更稳定见 packages/monaco-graphql/README.md变量校验依赖 monaco 的json语言模式内置 JSON Schema 支持且操作 model 必须能匹配到某个 schema 配置JSON Schema 才会被计算出来。先确认字段名README 与源码拼写不一致仓库中两处文档对该字段使用了不同拼写packages/monaco-graphql/README.md 的 Full Sync Demo with Variables JSON 与getDiagnosticOptions两节写的是validateVariablesJson实际的类型定义 packages/monaco-graphql/src/typings/index.ts 中DiagnosticSettings的字段是validateVariablesJSONwebpack 示例 examples/monaco-graphql-webpack/src/index.ts 使用的也是validateVariablesJSON。运行时读取的正是源码里的validateVariablesJSON见 packages/monaco-graphql/src/languageFeatures.ts 中defaults.diagnosticSettings.validateVariablesJSON?.[modelUri]所以代码里请以validateVariablesJSON为准。准备条件安装、model 与 schema 注册安装依赖README 给出的安装命令yarn add monaco-graphql创建两个 model。操作 model 使用graphql语言variables model 使用json语言各自有独立的 uri取自 webpack 示例 examples/monaco-graphql-webpack/src/editors.ts 的做法const operationModel editor.createModel( query {}, graphql, Uri.file(/1/operation.graphql), ); const variablesModel editor.createModel( {}, json, Uri.file(/1/variables.json), );注册 worker。monaco-graphql的 language worker 需要挂到MonacoEnvironment.getWorker上graphqllabel 返回 GraphQLWorkerREADME Sync Exampleimport * as monaco from monaco-editor/esm/vs/editor/editor.api; import { initializeMode } from monaco-graphql/initializeMode; // README 中 webpack 场景使用 worker-loader!monaco-graphql/esm/graphql.worker // README 同时提示 monaco-graphql/esm/* 模式已弃用规范子路径为 monaco-graphql/graphql.worker import GraphQLWorker from worker-loader!monaco-graphql/esm/graphql.worker; globalThis.MonacoEnvironment { getWorker(_workerId: string, label: string) { if (label graphql) { return new GraphQLWorker(); } return new Worker(editor.worker.js); }, };用initializeMode注册 schema且fileMatch必须能匹配到操作 model 的 uri。匹配逻辑由SchemaConfig.fileMatch提供支持具体 uri 和picomatch表达式README Features 与类型定义 packages/monaco-graphql/src/typings/index.ts 均如此说明const MonacoGraphQLAPI initializeMode({ schemas: [ { // anything that monaco.URI.from() is compatible with uri: https://my-schema.com, // match the monaco file uris for this schema. fileMatch: [**/*.graphql], schema: myGraphqlSchema as GraphQLSchema, }, ], });这里的myGraphqlSchema是你自己接入的GraphQLSchema实例属于读者必须提供的值。启用变量 JSON 校验setDiagnosticSettings在两个 model 都创建好之后调用setDiagnosticSettings把操作 model 的 uri映射到variables model 的 uri 列表README Full Sync Demo with Variables JSON 的高层配置方法MonacoGraphQLAPI.setDiagnosticSettings({ validateVariablesJSON: { // Urls, uris, anything that monaco.URI.from() is compatible with. // Match operation model to variables editor, // and the language service will automatically listen for changes, // and compute the json schema using the GraphQLWorker. // This is in the main process is applied to the global monaco json settings // for validation, completion and more using monaco-jsons built-in JSON Schema support. [operationModel.uri.toString()]: [variablesModel.uri.toString()], }, jsonDiagnosticSettings: { allowComments: true, // allow json, parse with a jsonc parser to make requests }, });各配置的作用依据 README 与类型定义注释validateVariablesJSON值为Recordstring, string[]键是操作 model 的 uri 字符串值是 variables model 的 uri 字符串数组。配置后语言服务会自动监听变更并用 GraphQLWorker 计算 JSON SchemaREADME 原文说明。jsonDiagnosticSettings透传给 monacojson模式的JSONDiagnosticOptions。类型定义中给出的可用项示例包括allowComments: true启用 jsonc 编辑、schemaValidation: warning、trailingCommas。不配置时api.ts中的默认值是{ schemaValidation: error }packages/monaco-graphql/src/api.ts 的diagnosticSettingDefault且实际写入 json 设置时固定带上schemaValidation: error、validate: true。setDiagnosticSettings是运行时 API随时调用都会触发 webworker 重新加载相关 language featuresREADME MonacoGraphQLAPI 一节如果你同步import monaco-graphql也可以通过monaco.languages.graphql.api拿到同一个 API。校验如何生效worker 计算 schema写入 monaco json 全局设置理解生效机制有助于判断为什么我的 variables 没有报错/补全。源码路径是 packages/monaco-graphql/src/languageFeatures.tsDiagnosticsAdapter只监听语言 id 为graphql的 model即你的操作 model并在内容变更后以 400ms 防抖触发校验若该操作 model 的 uri 在validateVariablesJSON中有登记则动态 import monaco 的 json 模式monaco-editor/esm/vs/language/json/monaco.contribution.js再调用 worker 的doGetVariablesJSONSchemaworker 侧由LanguageService.getVariablesJSONSchema基于该文件的 schema 与操作文档计算variableToType并生成 JSON Schemapackages/monaco-graphql/src/LanguageService.tsworker 会给结果加上$id: monaco://variables-schema.json和title: GraphQL Variablespackages/monaco-graphql/src/GraphQLWorker.ts主进程把该 schema 以fileMatch指向 variables model 的 uri 写入languages.json.jsonDefaults的诊断选项此后 variables 编辑器的校验、补全等由 monaco-json 的内置 JSON Schema 支持完成README 原文applied to the global monaco json settings for validation, completion and more using monaco-jsons built-in JSON Schema support。由此可以读出两个前置条件schema 配置必须能匹配操作 model 的 uri否则getSchemaForFile拿不到 schema直接返回null操作文档内容需可解析且非空源码中要求documentText.length 3。验证运行示例工程观察变量编辑器仓库自带一个minimal example with variables (C)JSON support的 vite react 示例以及一个功能更全的 webpack 示例。monorepo 的启动方式见 examples/monaco-graphql-react-vite/README.md# 在 monorepo 根目录 pnpm install pnpm build # 在对应示例目录 pnpm dev以 webpack 示例为例examples/monaco-graphql-webpack/src/index.ts它完整演示了上述配置monacoGraphQLAPI.setDiagnosticSettings({ validateVariablesJSON: { [operationUri]: [variablesModel.uri.toString()], }, jsonDiagnosticSettings: { // jsonc tip! allowComments: true, schemaValidation: error, // this is nice too trailingCommas: warning, }, });该示例预置的 variables 内容examples/monaco-graphql-webpack/src/editors.ts文档示例与示例查询query Example($owner: String! $name: String! $reviewEvent: PullRequestReviewEvent! $user: FollowUserInput!)明显不一致——缺少$owner/$username给的是 Boolean{ reviewEvent: graphql, name: true }启动示例后在 variables 编辑器中编辑这段内容即可按 README 所述观察 monaco-json 基于计算出的 JSON Schema 给出的 validation 与 completion 行为操作文档中变量声明变化时schema 会随操作 model 的变更被重新计算。边界与注意包版本getVariablesJSONSchema方法自monaco-graphql0.5.0引入README Variables JSON Support!README TODO 中 variables JSON validation 与 variables completion 均已完成。lite 导入从monaco-graphql/lite导入时默认只有 highlighting 和 validationcompletion 等需要手动 import 对应 monaco contrib 后才会生效README monaco-graphql/lite 一节的 Warning。json 模式打包variables 校验依赖 monaco 的 json 语言模式。README Avoid Bundle All monaco-editors Languages 一节说明monaco-graphql1.3.0及以上可用monaco-graphql/monaco-editor替换全部monaco-editor导入只加载graphql与json两种语言。弃用子路径monaco-graphql/esm/*导入模式已弃用保留以兼容新代码应使用规范子路径monaco-graphql/monaco-editor、monaco-graphql/initializeMode、monaco-graphql/graphql.worker、monaco-graphql/lite。成熟度如开头所述monaco-graphql是 pre-release如果 JSON 变量校验是核心诉求且要求稳定README 建议的替代是codemirror-graphql。【免费下载链接】graphiqlGraphiQL the GraphQL LSP Reference Ecosystem for building browser IDE tools.项目地址: https://gitcode.com/GitHub_Trending/gr/graphiql创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

相关新闻

Zemax光线光扇图全解析:像差判读与镜头优化实战

Zemax光线光扇图全解析:像差判读与镜头优化实战

真正上手Zemax做光学设计的人,几乎每天都要跟各种分析图打交道。镜头数据表告诉你系统长什么样,光线追迹告诉你光走没走对,但要说哪个图能让你“一眼看出问题”,我首推光线光扇图(Ray Fan Plot)。这玩意儿看…

2026/11/13 9:55:57 阅读更多 →
OpenSandbox /v1/isolated/capabilities 返回 available:false 怎么排查

OpenSandbox /v1/isolated/capabilities 返回 available:false 怎么排查

OpenSandbox /v1/isolated/capabilities 返回 available:false 怎么排查 【免费下载链接】OpenSandbox Secure, Fast, and Extensible Sandbox runtime for AI agents. 项目地址: https://gitcode.com/GitHub_Trending/ope/OpenSandbox GET /v1/isolated/capabilities 是…

2026/11/7 14:22:23 阅读更多 →
InsForge 自托管部署完全指南:从 Docker Compose 到生产级安全加固

InsForge 自托管部署完全指南:从 Docker Compose 到生产级安全加固

InsForge 自托管部署完全指南:从 Docker Compose 到生产级安全加固 【免费下载链接】InsForge The all-in-one, open-source backend platform for agentic coding. InsForge gives your coding agent database, auth, storage, compute, hosting, and AI gateway t…

2026/11/10 13:02:05 阅读更多 →

最新新闻

Electron桌面应用开发实战:从环境搭建到打包发布全流程

Electron桌面应用开发实战:从环境搭建到打包发布全流程

做前端这些年,我大部分时间都在跟浏览器标签页打交道,直到第一次把桌面应用跑起来,才发现原来自己一直被限制在地址栏和刷新按钮围起来的框框里。Electron这个词,最开始是在某段时间频繁出现在同事的讨论里,一个用HTML…

2026/11/14 5:13:29 阅读更多 →
滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

简介:亚太天能T109滑盖智能锁产品说明书是一份PDF格式的官方技术文档,主要面向智能锁安装人员、终端用户及售后维护者,帮助快速掌握产品安装、配置与常见故障处理。全包仅1个PDF文件,大小4.37MB,内容结构清晰&#xff…

2026/11/14 5:13:29 阅读更多 →
PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

上个月我在调试一台高速AGV的驱动系统,整车要求空载跑到8m/s,电机转速已经拉到额定点,驱动器的输出电压也顶到了母线电压极限附近。再往上踩速度指令,转速纹丝不动,三相电流反而开始震荡。那会儿我的第一反应就是&…

2026/11/14 5:13:29 阅读更多 →
C++核心知识点:从内存管理到STL的实战指南

C++核心知识点:从内存管理到STL的实战指南

C 这门语言我前前后后折腾了十几年,用它写过嵌入式驱动、做过游戏服务端,也搭过基础组件库。每次看到新人一头扎进"C 核心知识点"这个搜索词,我其实挺感慨的——这门语言的难点从来不是语法多,而是知识点之间的耦合太深…

2026/11/13 22:08:41 阅读更多 →
T109滑盖智能锁怎么装?安装调试与故障排查全攻略

T109滑盖智能锁怎么装?安装调试与故障排查全攻略

简介:亚太天能T109滑盖智能锁产品说明书以PDF格式发布,适合智能锁用户、安装人员与售后维修人员阅读。内容围绕T109系列指纹密码刷卡锁展开,先介绍前面板、后面板、锁体、机械钥匙、电池盒盖等部件名称与功能,再给出完整安装步骤&…

2026/11/14 2:41:37 阅读更多 →
LLM知识降熵:从信息垄断到理解权下放

LLM知识降熵:从信息垄断到理解权下放

你有没有过这种经历:翻开源码注释、论文摘要或者一份专业报告,每个字都认识,连在一起却像天书。想找人聊聊,身边要么没人懂,要么懂的懒得跟你解释。后来我试着把同一段内容丢给一个大语言模型,几秒钟之后&a…

2026/11/14 5:13:29 阅读更多 →

日新闻

周新闻

月新闻