【发布时间】: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的输出是什么?
标签: express graphql apollo-server prisma prisma-graphql