从Strapi市场安装GraphQL时出现GraphQLObjectType "Query"报错如何解决?
原始报错信息
error Error: Cannot use GraphQLObjectType "Query" from another module or realm.
Ensure that there is only one instance of "graphql" in the node_modules
directory. If different versions of "graphql" are the dependencies of other
relied on modules, use "resolutions" to ensure only one version is installed.Duplicate "graphql" modules cannot be used at the same time since different
versions may have different capabilities and behavior. The data from one
version used in the function from another could produce confusing and
spurious results.
报错中文翻译
错误:无法调用来自其他模块或域的GraphQL对象类型“Query”。
请确保node_modules目录中仅存在一个graphql实例。如果其他依赖模块引用了不同版本的graphql,可以使用版本锁定配置确保仅安装一个版本。
不能同时使用重复的graphql模块,因为不同版本的功能和行为存在差异,跨版本调用数据可能会产生难以排查的错误结果。
问题本质
你的项目node_modules目录下存在多个不同版本的graphql依赖包,不同版本的graphql实例无法共用,触发框架内置的校验报错。
修复步骤
如果你使用yarn作为包管理器
- 打开项目根目录的
package.json,新增resolutions字段强制锁定graphql版本:
{ "name": "your-strapi-project", "version": "1.0.0", // 其他原有配置保持不变,新增以下配置 "resolutions": { "graphql": "^16.8.0" } }
- 删除项目根目录的
node_modules文件夹和yarn.lock锁文件 - 执行
yarn install重新安装所有依赖 - 重启Strapi服务后再安装GraphQL插件即可
如果你使用npm作为包管理器
- 打开项目根目录的
package.json,新增overrides字段强制锁定graphql版本:
{ "name": "your-strapi-project", "version": "1.0.0", // 其他原有配置保持不变,新增以下配置 "overrides": { "graphql": "^16.8.0" } }
- 删除项目根目录的
node_modules文件夹和package-lock.json锁文件 - 执行
npm install重新安装所有依赖 - 重启Strapi服务后再安装GraphQL插件即可
内容的提问来源于stack exchange,提问作者Ghassen Jemaî

