使用ApolloClient

时间:2019-12-10 17:46:41

标签: graphql react-apollo apollo-client graphql-tools apollo-link

我有一个带有ApolloClient和Apollo-Link-Schema的React应用程序。该应用程序在本地运行良好,但在我们的暂存环境(使用GOCD)中,出现以下错误:

Uncaught Error: Cannot use e "__Schema" 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.

https://yarnpkg.com/en/docs/selective-version-resolutions

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.
    at t.a (instanceOf.mjs:21)
    at C (definition.mjs:37)
    at _ (definition.mjs:22)
    at X (definition.mjs:284)
    at J (definition.mjs:287)
    at new Y (definition.mjs:252)
    at Y (definition.mjs:254)
    at Object.<anonymous> (introspection.mjs:459)
    at u (NominationsApprovals.module.js:80)
    at Object.<anonymous> (validate.mjs:1)

依赖关系随纱线一起安装,我已将resolutions字段添加到package.json。

    "resolutions": {
        "graphql": "^14.5.8"
    },

我已经检查了yarn.lock,只能为graphql包找到一个参考。 npm ls graphql不显示任何重复项。

我认为这可能是webpack的构建问题-我有一个不同的构建脚本进行暂存,但是在本地运行该脚本,我仍然能够使react应用程序与该捆绑包一起运行。

有人可以提出其他建议来帮助我解决此问题吗?

2 个答案:

答案 0 :(得分:0)

我会尝试在本地复制错误并对其进行调试:

尝试一下:

rm -rf node_modules yarn.lock
# also remove any lock files if you have package-lock.json too
yarn install
# build the project locally and see if you got the error

在使用Gatsby和2个不同主题使用不同版本的GraphQL时,我遇到了这个问题。还要更明确地显示该版本(无尖号),并检查错误是否仍然存在。

您有回购股份吗?这也将帮助我们为您提供帮助:)

答案 1 :(得分:0)

如果这可以帮助其他人,我设法找到了问题的原因。问题根本与包的重复实例无关,这是由我们使用webpack的DefinePlugin将临时构建的process.env.NODE_ENV设置为staging触发的误报。

但是,在Webpack中,mode(请参阅https://webpack.js.org/configuration/mode/)设置了process.env.NODE_ENV,仅接受nonedevelopmentproduction作为有效值。这会触发graphql软件包中的env检查失败并触发此错误消息。

在我们的情况下,我们需要区分阶段和生产,因为我们的API终结点基于此而有所不同,但是我们实现的解决方案不是依靠process.env.NODE_ENV,而是在build上分配一个自定义变量(例如process.env.API_URL