我正在使用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实例吗?
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的库副本,而不是其自身的副本。