Gatsby v5升级后构建与开发报错求助(ERROR #85928/#11321)
问题概述
把 Gatsby 从 v4 升级到 v5.7.0 后,和 WordPress 集成的项目出了问题:
- 生产环境执行
gatsby build直接报错 - 开发环境
gatsby develop只有在gatsby clean后第一次能正常运行,第二次就卡壳构建失败
核心报错是 GraphQL 接口字段不匹配:WpConnectionType 要求有 nodes 字段,但 WpEnqueuedScriptConnectionType、WpEnqueuedStylesheetConnectionType 这些类型根本没提供这个字段。
报错日志示例
ERROR #85923 GRAPHQL
There was an error in your GraphQL query:
Field "nodes" of type "[WpEnqueuedScript]" must not have a selection since type "[WpEnqueuedScript]" has no subfields.
The GraphQL query from ./src/components/... includes a field
nodeson typeWpEnqueuedScriptConnectionType, but this type doesn't expose anodesfield.类似错误同时出现在
WpEnqueuedStylesheetConnectionType等类型上
项目依赖版本参考
gatsby: 5.7.0gatsby-source-wordpress: 7.7.0WordPress: 6.2.2
解决方案步骤
1. 升级 WordPress 源插件到兼容版本
Gatsby v5 对 gatsby-source-wordpress 版本有要求,至少要升到 7.10.0 以上才能适配 v5.7.0。直接执行命令更新:
npm update gatsby-source-wordpress
或者手动修改 package.json 里的插件版本号,再执行 npm install。
2. 修正 GraphQL 查询结构
旧版本的查询可能用的是 edges.node 结构,升级后部分连接类型(比如脚本、样式表)还没适配 nodes 直接返回的写法。把原来的查询:
query { allWpEnqueuedScript { nodes { handle src } } }
改成兼容的旧结构:
query { allWpEnqueuedScript { edges { node { handle src } } } }
不确定的话,直接查插件官方文档确认对应类型的正确查询方式。
3. 彻底清除缓存重建
别只依赖 gatsby clean,手动删掉 .cache 和 public 文件夹再构建,避免旧的 schema 残留搞事情:
rm -rf .cache public && gatsby develop
4. 排除不需要的 WordPress 数据类型
如果项目根本不需要获取 enqueued 脚本/样式表的数据,直接在 gatsby-config.js 里配置插件排除这些类型:
module.exports = { plugins: [ { resolve: `gatsby-source-wordpress`, options: { // 排除指定字段 excludeFieldNames: [`enqueuedScripts`, `enqueuedStylesheets`], // 或者直接排除整个类型 excludedTypes: [`EnqueuedScript`, `EnqueuedStylesheet`], }, }, ], };
5. 暂时降级 Gatsby 到稳定版本
要是上面的方法都没用,先降级到 Gatsby v5.6.0,这个版本和大多数 gatsby-source-wordpress 版本兼容性更好:
npm install gatsby@5.6.0
内容的提问来源于stack exchange,提问作者Mikalai Vinahorau

