无法从另一个模块或领域使用GraphQLObjectType \“ Query \”

问题描述

我正在使用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"
  }
}

您知道这里可能有什么问题吗?

解决方法

您的 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