`不能使用来自另一个模块或领域的“__Schema”。` 和使用 ApolloClient 的`复制“graphql”模块`

lai*_*j84 4 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)
Run Code Online (Sandbox Code Playgroud)

依赖项是用纱线安装的,我已将该resolutions字段添加到 package.json。

    "resolutions": {
        "graphql": "^14.5.8"
    },
Run Code Online (Sandbox Code Playgroud)

我检查了 yarn.lock 并且只能找到一个关于 graphql 包的参考。 npm ls graphql不显示任何重复项。

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

任何人都可以提出其他建议来帮助我解决这个问题吗?

lai*_*j84 7

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

然而,在所述的WebPack mode(见https://webpack.js.org/configuration/mode/),其设置process.env.NODE_ENV,只接受nonedevelopmentproduction作为有效的值。这会触发 graphql 包中的 env 检查失败并触发此错误消息。

在我们的例子中,我们需要区分暂存和生产,因为我们的 API 端点基于此而不同,但我们实施的解决方案是不依赖于process.env.NODE_ENV,而是在构建时分配自定义变量(例如process.env.API_URL


小智 1

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

尝试这个:

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
Run Code Online (Sandbox Code Playgroud)

有一次我在使用 Gatsby 和使用不同版本的 GraphQL 的 2 个不同主题时遇到了这个问题。还要更明确地使用版本(不带插入符号)并检查错误是否仍然存在。

你有回购协议吗?这也将有助于我们帮助您:)