无法使用来自另一个模块或领域的 GraphQLObjectType \"Query\"

Posted

技术标签:

【中文标题】无法使用来自另一个模块或领域的 GraphQLObjectType \\"Query\\"【英文标题】:Cannot use GraphQLObjectType \"Query\" from another module or realm无法使用来自另一个模块或领域的 GraphQLObjectType \"Query\" 【发布时间】:2021-01-05 12:09:05 【问题描述】:

我正在使用apollo-server-express 构建graphql 服务器

server.js 中的解析器就是这么简单

const express = require('express');
const  ApolloServer  = require('apollo-server-express');
const  importSchema  = require('graphql-import');
const typeDefs = importSchema('./src/schema.graphql');
const prisma = require('./src/prisma');

const server = new ApolloServer(
  typeDefs,
  resolvers: 
    Query: 
       users(parent, args,  prisma , info) 
        return prisma.query.users(args, info);
      
    
  ,
  context: ( req ) => ( ...req, prisma )
);

const app = express();
server.applyMiddleware( app );

app.listen( port: 4000 , () =>
  console.log(`???? Server ready at http://localhost:4000$server.graphqlPath`)
);

当我运行用户查询时出现此错误


  "errors": [
    
      "message": "Cannot use GraphQLObjectType \"Query\" from another module or realm.\n\nEnsure that there is only one instance of \"graphql\" in the node_modules\ndirectory. If different versions of \"graphql\" are the dependencies of other\nrelied on modules, use \"resolutions\" to ensure only one version is installed.\n\nhttps://yarnpkg.com/en/docs/selective-version-resolutions\n\nDuplicate \"graphql\" modules cannot be used at the same time since different\nversions may have different capabilities and behavior. The data from one\nversion used in the function from another could produce confusing and\nspurious results.",
      "locations": [
        
          "line": 2,
          "column": 3
        
      ],
      "path": [
        "users"
      ],
      "extensions": 
        "code": "INTERNAL_SERVER_ERROR",
        "exception": 
          "stacktrace": [
            "Error: Cannot use GraphQLObjectType \"Query\" 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 instanceOf (D:\\00. DEVELOPMENT\\FULL PROJECTS\\social-template-2\\node_modules\\graphql-binding\\node_modules\\graphql\\jsutils\\instanceOf.js:28:13)",
            "    at isObjectType (D:\\00. DEVELOPMENT\\FULL PROJECTS\\social-template-2\\node_modules\\graphql-binding\\node_modules\\graphql\\type\\definition.js:116:34)",
            "    at TypeInfo.enter (D:\\00. DEVELOPMENT\\FULL PROJECTS\\social-template-2\\node_modules\\graphql-binding\\node_modules\\graphql\\utilities\\TypeInfo.js:163:61)",
            "    at Object.enter (D:\\00. DEVELOPMENT\\FULL PROJECTS\\social-template-2\\node_modules\\graphql-binding\\node_modules\\graphql\\language\\visitor.js:369:16)",
            "    at Object.visit (D:\\00. DEVELOPMENT\\FULL PROJECTS\\social-template-2\\node_modules\\graphql-binding\\node_modules\\graphql\\language\\visitor.js:242:26)",
            "    at replaceFieldsWithFragments (D:\\00. DEVELOPMENT\\FULL PROJECTS\\social-template-2\\node_modules\\graphql-binding\\node_modules\\graphql-tools\\src\\transforms\\ReplaceFieldWithFragment.ts:67:10)",
            "    at ReplaceFieldWithFragment.transformRequest (D:\\00. DEVELOPMENT\\FULL PROJECTS\\social-template-2\\node_modules\\graphql-binding\\node_modules\\graphql-tools\\src\\transforms\\ReplaceFieldWithFragment.ts:45:22)",
            "    at D:\\00. DEVELOPMENT\\FULL PROJECTS\\social-template-2\\node_modules\\graphql-binding\\node_modules\\graphql-tools\\src\\transforms\\transforms.ts:24:21",
            "    at Array.reduce (<anonymous>)",
            "    at Object.applyRequestTransforms (D:\\00. DEVELOPMENT\\FULL PROJECTS\\social-template-2\\node_modules\\graphql-binding\\node_modules\\graphql-tools\\src\\transforms\\transforms.ts:21:21)"
          ]
        
      
    
  ],
  "data": null

这是我的package.json


  "name": "social-template-2",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": 
    "server": "nodemon --ext js,graphql --exec babel-node server"
  ,
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": 
    "apollo-server-express": "^2.17.0",
    "babel-cli": "^6.26.0",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "bcryptjs": "^2.4.3",
    "express": "^4.17.1",
    "graphql": "^15.3.0",
    "graphql-import": "^1.0.2",
    "jsonwebtoken": "^8.5.1",
    "prisma-binding": "^2.3.16"
  ,
  "devDependencies": 
    "@prisma/cli": "^2.7.1",
    "concurrently": "^5.3.0",
    "nodemon": "^2.0.4"
  

你知道这里有什么问题吗?

【问题讨论】:

看起来像 graphql 包版本不匹配。您可以尝试使用像 14 这样的早期版本并检查吗? @Ryan 刚刚这样做,安装了 graphql@14.0.0 => 相同的错误! 然后我发现它需要对等 14.2.1。然后我安装了它。现在我有 graphql@14.2.1 并且仍然有同样的问题,我每次都删除 package-lock 并再次 npm i。但还是同样的错误! yarn why graphql 的输出是什么? 【参考方案1】:

您的 node_modules 中可能还有另一个 graphql。 尝试检查:

find node_modules -name graphql

然后查看每个graphql模块的package.json并检查那里的版本。

最后,对我来说有帮助的只是卸载所有包含 graphql 的包并再次一起安装它们:

npm uninstall apollo-server apollo-server-express graphql neo4j-driver neo4j-graphql-js

npm install apollo-server apollo-server-express graphql neo4j-driver neo4j-graphql-js --force

【讨论】:

以上是关于无法使用来自另一个模块或领域的 GraphQLObjectType \"Query\"的主要内容,如果未能解决你的问题,请参考以下文章

错误:无法使用来自另一个模块或领域的 GraphQLObjectType“__Directive”

使用npm链接时,无法从其他模块或领域使用GraphQLSchema“[object GraphQLSchema]”

如何使用来自另一个模块的模块内的 getter 进行 Vuex 状态

无法将类从一个模块导入到另一个模块 - Scala

错误 TS4058:导出函数的返回类型具有或正在使用来自外部模块 Y 的名称 X,但无法命名

领域逻辑组织