我正在使用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"
}
}你知道这里会出什么问题吗?
发布于 2021-01-22 01:30:36
您的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 --forcehttps://stackoverflow.com/questions/63957674
复制相似问题