我使用graphql-js而不是SDL来设计我的graphql服务器。为此,我创建了一个依赖于graphql-js的小型库。
因此,我使用yarn (yarn add link:../lib)将这个库链接到我的主项目中,以构建graphql对象和模式。
我的package.json文件如下所示
graphql-lib/package.json
{
"name": "graphql-lib",
"private": true,
"version": "0.1.0",
"description": "",
"main": "index.ts",
"dependencies": {
"graphql-iso-date": "^3.6.1"
},
"devDependencies": {
"@types/graphql-iso-date": "^3.4.0",
"@types/jest": "^25.2.3",
"@types/node": "^14.0.5",
"jest": "^26.0.1",
"ts-jest": "^26.1.0",
"typescript": "^3.9.3"
},
"peerDependencies": {
"graphql": "^15.1.0"
}
}core/package.json
{
"name": "@core/schema",
"private": true,
"version": "0.1.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"graphql-lib": "link:../lib",
"graphql": "^15.1.0",
"graphql-iso-date": "^3.6.1"
},
"devDependencies": {
"@types/graphql-iso-date": "^3.4.0",
"@types/jest": "^26.0.0"
}
}使用ts-jest的graphql-lib测试运行良好。
然而,当我测试我的主项目时,我得到了以下错误-
Cannot use GraphQLScalarType "Float" 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.node_modules目录中的graphql模块仅包含graphql-js版本15.1.0。我已经删除并重新安装了这两个包中的node_modules。
我的理解是应该有一个graphql的执行实例。我是否遗漏了一些东西,以至于在两个项目中都创建了graphql实例?我可以使用yarn链接我的项目并维护单个graphql实例吗?
发布于 2020-06-13 17:33:42
只有当您的依赖项中有多个graphql-js副本时,才会出现该错误。最常见的原因是您的node_modules中有多个版本。您可以通过运行npm ls graphql或yarn ls graphql来验证这一点--如果您看到依赖项中列出了多个版本,这就是问题所在。通常,只有当您的依赖项直接依赖于graphql-js (而不是使其成为对等依赖项)时,才会发生这种情况。如果你使用yarn,你可以使用it's selective dependency feature来解决这个问题。
当您在本地开发多个包时,您也会遇到这个问题,因为您有两个不同的graphql-js副本--在两个项目中各有一个。这是因为npm link或yarn add link只创建了一个从一个项目的node_modules到另一个项目的符号链接。作为一种解决办法,您也可以链接graphql-js。进入项目A中的node_modules/graphql并运行npm link/yarn link。然后进入项目B的根目录并运行npm link graphql/yarn link graphql。现在项目B将使用项目A的库副本,而不是它自己的副本。
https://stackoverflow.com/questions/62356525
复制相似问题