必须安装GraphQL for VSCode(作者Kumar Harsh),非其他同名插件;需配置graphql.config.yml显式声明extensions.endpoints.default.url及headers,schema字段仅用于补全;运行查询须通过命令面板或右键触发,变量为合法JSON格式,响应含locations表明GraphQL层错误。

VSCode 本身不带 GraphQL Playground,但通过正确配置插件,你能在编辑器内直接发送查询、查看响应、调试变量和 headers——效果接近原生 Playground,且无需切换窗口。
装哪个插件才真正支持运行查询
必须安装 GraphQL for VSCode(作者 Kumar Harsh),不是“GraphQL”“GraphQL Language Service”或已归档的旧插件。后者只做语法高亮,不提供执行能力。装完后打开 .graphql 文件,右下角必须显示 GraphQL: connected;没出现就等于没连上 schema,所有运行功能都不可用。
配置 graphql.config.yml 让查询能真正发出
插件靠这个文件知道 endpoint 在哪、要不要传 token、是否启用 introspection。常见错误是直接写 schema: http://localhost:4000/graphql 就以为万事大吉——但实际运行查询时,它默认用的是 extensions.endpoints.default.url,不是 schema 字段。
-
schema只用于补全和校验,不影响 query 执行 - 必须显式声明
extensions.endpoints.default.url,否则点击“Run Query”会报错Cannot resolve endpoint - 需要认证时,
headers必须写成对象格式:Authorization: "Bearer xyz",不能用字符串拼接 - introspect 设为
true才能自动拉取 schema(仅限开发环境),生产环境建议关掉
示例:
schema: ./schema.graphql
extensions:
endpoints:
default:
url: http://localhost:4000/graphql
headers:
Authorization: "Bearer abc123"
introspect: true
运行查询时变量和 headers 怎么填
命令面板(Ctrl+Shift+P)里搜 GraphQL: Run Query 或在 .graphql 文件里右键选 “Run Query in GraphQL Playground”。这时弹出的面板才支持填变量和 headers。
- 变量必须是合法 JSON 格式:
{"id": 123},不是 JS 对象或带单引号的字符串 - headers 只接受键值对,
Content-Type会被自动设为application/json,别手动覆盖 - 如果后端要求
X-Api-Key这类自定义头,必须在 config 里预设,运行时无法临时添加 - 响应体里出现
locations字段(如"locations": [{"line": 3, "column": 12}]),说明是 GraphQL 层报错,不是网络问题
为什么点“Run”没反应或报 400/401
多数情况不是插件问题,而是 endpoint 不响应或返回非标准格式。
- 后端关闭 introspection 时,
schema加载失败不会阻断查询运行,但extensions.endpoints配置错误会导致静默失败 - CORS 被拦截时,浏览器控制台有提示,VSCode 插件里只显示空响应或
Network error - 某些 NestJS/Lighthouse 默认把 GraphQL endpoint 放在
/graphql,但实际路径可能是/api/graphql,URL 少一个路径段就 404 - 用 Docker 或 WSL 开发时,
localhost指向容器内部,应改用host.docker.internal或宿主机真实 IP
最稳的方式是先用 curl 测试 endpoint 是否返回有效 JSON:curl -X POST http://localhost:4000/graphql -H "Content-Type: application/json" -d '{"query":"{__schema{types{name}}"}' 。能跑通,VSCode 才可能跑通。


















