Magento 2.4.7-p2全新安装后调用GraphQL遇语法错误求助
Magento 2.4.7/2.4.7-p2 GraphQL "Unexpected " 错误解决方案
问题原因
直接通过浏览器以GET方式访问GraphQL端点(https://app.demo.test/graphql)会触发该错误——GraphQL接口要求通过POST请求提交具体查询内容,GET请求未携带任何查询时,解析器会因缺少输入内容抛出EOF(文件结束符)错误。
解决方法
用POST请求发送查询内容:
借助Postman、curl等工具发送POST请求到目标端点,请求体为JSON格式,示例如下:{ "query": "{ storeConfig { base_url } }" }curl命令示例:
curl -X POST https://app.demo.test/graphql \ -H "Content-Type: application/json" \ -d '{"query": "{ storeConfig { base_url } }"}'启用GraphiQL可视化调试界面:
- 登录Magento后台,进入
Stores > Configuration > Advanced > Developer > GraphQL Settings - 将
Enable GraphiQL设为Yes,保存配置后清除缓存 - 再次访问
https://app.demo.test/graphql,即可通过可视化界面编写并执行查询
- 登录Magento后台,进入
验证请求路由配置:
确认Warden的Nginx/Apache配置正确转发GraphQL请求:- Apache:
.htaccess文件需包含RewriteRule ^graphql$ graphql/index.php [L]规则 - Nginx:配置文件需包含
location /graphql { try_files $uri $uri/ /graphql/index.php?$args; }规则
- Apache:
清除Magento缓存:
执行以下命令确保配置变更生效:bin/magento cache:clean bin/magento cache:flush
内容的提问来源于stack exchange,提问作者user2217288
相关产品推荐
相关产品推荐

