【问题标题】:Cannot use GraphQLSchema from another module or realm无法从其他模块或领域使用 GraphQLSchema
【发布时间】:2020-12-31 10:08:07
【问题描述】:

我正在使用 type-graphqlapollo-server-lambda 构建无服务器后端,但是在向 graphql 端点发出大约第三次请求后,我收到了错误

Error: Cannot use GraphQLSchema "[object GraphQLSchema]" 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.

我已经降级了type-graphql,因为版本1.0.0 需要graphql@^15.3.0 而其他一些依赖项需要graphql <= 14

这是我的依赖项

{
  "dependencies": {
    "@types/graphql": "14.5.0",
    "apollo-server-lambda": "^2.17.0",
    "aws-sdk": "^2.750.0",
    "graphql": "^14.7.0",
    "pg": "^8.3.3",
    "reflect-metadata": "^0.1.13",
    "source-map-support": "^0.5.19",
    "type-graphql": "^0.17.0",
  },
  "devDependencies": {
    "@types/aws-lambda": "^8.10.62",
    "@types/aws-sdk": "^2.7.0",
    "@types/jest": "^26.0.13",
    "@types/node": "^14.10.0",
    "apollo-server-testing": "^2.17.0",
    "serverless": "^2.0.0",
    "serverless-offline": "^6.7.0",
    "serverless-webpack": "^5.3.4",
    "ts-jest": "^26.3.0",
    "ts-loader": "^8.0.3",
    "typescript": "^4.0.2",
    "webpack": "^4.44.1",
    "webpack-node-externals": "^2.5.2"
  },
  "resolutions": {
    "graphql": "^14.7.0",
  },

npm ls graphql结果

dives-backend@1.0.0
├─┬ @types/graphql@14.5.0
│ └── graphql@14.7.0  deduped
└── graphql@14.7.0 

index.ts

import "reflect-metadata"
import { buildSchemaSync } from "type-graphql"
import { HelloResolver } from "./src/resolvers/example"

const schema = buildSchemaSync({
    resolvers: [HelloResolver],
    validate: false,
})

const server = new ApolloServer({
    schema,
    playground: { endpoint: "/dev/graphql" },
})

export const handler = server.createHandler({
        cors: {
            origin: true,
            credentials: true,
        },
    })

【问题讨论】:

  • apollo-server-lambda 不也依赖于graphql 版本吗?
  • 是的,apollo-server-core@2.17.0 需要 "graphql": "^0.12.0 || ^0.13.0 || ^14.0.0" 作为 peerDependency

标签: javascript graphql apollo-server graphql-js


【解决方案1】:

我终于找到了解决办法。我将apollo-server-lambda 都升级为3.0.0-alpha.3 并将type-graphql 恢复为^1.0.0。我还将resolution 升级为"graphql": "15.3.0"

将 type-graphql 与 lambda 一起使用时,还有一个重要步骤是覆盖全局对象(名称 schema 很重要)-reference issue

if (!global.schema) {
  global.schema = buildSchemaSync({
    resolvers: [UserResolver],
    validate: false,
  });
}
const schema = global.schema;

【讨论】:

    【解决方案2】:

    我遇到了类似的问题,并在typegraphql FAQ 中找到了建议。 npm dedupe 帮我解决了。

    【讨论】:

      猜你喜欢
      • 2019-03-09
      • 2021-01-05
      • 1970-01-01
      • 2019-07-27
      • 1970-01-01
      • 1970-01-01
      • 2013-06-29
      • 1970-01-01
      • 2014-09-22
      相关资源
      最近更新 更多