我正在尝试用next.js、graphql和apollo服务器micro启动一个项目。
但是我在运行localhost:3000/api/graphql
时遇到了一个错误,我不知道如何解决它
Playground正在按预期启动,当我点击"查询你的服务器"时,我收到了这个错误。
npm run dev
> next-graphql@0.1.0 dev
> next dev
ready - started server on 0.0.0.0:3000, url: http://localhost:3000
info - SWC minify release candidate enabled. https://nextjs.link/swcmin
event - compiled client and server successfully in 421 ms (177 modules)
wait - compiling /api/graphql...
event - compiled successfully in 54 ms (49 modules)
MongoDB connected 🚀
error - Error: called start() with surprising state started
at ApolloServer._start (/home/floslv/Flo/Dev/Learning/next-graphql/node_modules/apollo-server-core/dist/ApolloServer.js:185:19)
at ApolloServer.start (/home/floslv/Flo/Dev/Learning/next-graphql/node_modules/apollo-server-core/dist/ApolloServer.js:180:27)
at handler (webpack-internal:///(api)/./pages/api/graphql.js:30:24)
at Object.apiResolver (/home/floslv/Flo/Dev/Learning/next-graphql/node_modules/next/dist/server/api-utils/node.js:184:15)
at DevServer.runApi (/home/floslv/Flo/Dev/Learning/next-graphql/node_modules/next/dist/server/next-server.js:381:27)
at async Object.fn (/home/floslv/Flo/Dev/Learning/next-graphql/node_modules/next/dist/server/base-server.js:491:37)
at async Router.execute (/home/floslv/Flo/Dev/Learning/next-graphql/node_modules/next/dist/server/router.js:213:36)
at async DevServer.run (/home/floslv/Flo/Dev/Learning/next-graphql/node_modules/next/dist/server/base-server.js:610:29)
at async DevServer.run (/home/floslv/Flo/Dev/Learning/next-graphql/node_modules/next/dist/server/dev/next-dev-server.js:532:20)
at async DevServer.handleRequest (/home/floslv/Flo/Dev/Learning/next-graphql/node_modules/next/dist/server/base-server.js:311:20) {
page: '/api/graphql'
}
wait - compiling /_error (client and server)...
event - compiled client and server successfully in 108 ms (196 modules)
MongoDB connected 🚀
API resolved without sending a response for /api/graphql, this may result in stalled requests.
error - Error: called start() with surprising state started
at ApolloServer._start (/home/floslv/Flo/Dev/Learning/next-graphql/node_modules/apollo-server-core/dist/ApolloServer.js:185:19)
at ApolloServer.start (/home/floslv/Flo/Dev/Learning/next-graphql/node_modules/apollo-server-core/dist/ApolloServer.js:180:27)
at handler (webpack-internal:///(api)/./pages/api/graphql.js:30:24)
at Object.apiResolver (/home/floslv/Flo/Dev/Learning/next-graphql/node_modules/next/dist/server/api-utils/node.js:184:15)
at DevServer.runApi (/home/floslv/Flo/Dev/Learning/next-graphql/node_modules/next/dist/server/next-server.js:381:27)
at async Object.fn (/home/floslv/Flo/Dev/Learning/next-graphql/node_modules/next/dist/server/base-server.js:491:37)
at async Router.execute (/home/floslv/Flo/Dev/Learning/next-graphql/node_modules/next/dist/server/router.js:213:36)
at async DevServer.run (/home/floslv/Flo/Dev/Learning/next-graphql/node_modules/next/dist/server/base-server.js:610:29)
at async DevServer.run (/home/floslv/Flo/Dev/Learning/next-graphql/node_modules/next/dist/server/dev/next-dev-server.js:532:20)
at async DevServer.handleRequest (/home/floslv/Flo/Dev/Learning/next-graphql/node_modules/next/dist/server/base-server.js:311:20) {
page: '/api/graphql'
}
这是我的package.json
:
{
"name": "next-graphql",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"@apollo/client": "^3.6.9",
"apollo-server-micro": "^3.10.0",
"graphql": "^16.5.0",
"mongoose": "^6.5.1",
"next": "12.2.4",
"react": "18.2.0",
"react-dom": "18.2.0"
},
"devDependencies": {
"eslint": "8.21.0",
"eslint-config-next": "12.2.4"
}
}
这是我的pages/api/graphql.js
:
import { ApolloServer } from 'apollo-server-micro';
import typeDefs from '../../backend/graphql/typeDefs.js';
import resolvers from '../../backend/graphql/resolvers/index.js';
import connectDb from '../../backend/db/config/connectDb.js';
// Connection to DB
connectDb();
// Graphql
const apolloServer = new ApolloServer({
typeDefs,
resolvers
});
// Next config
export const config = {
api: {
bodyParser: Boolean(false),
}
};
// Run server
const handler = async (req, res) => {
await apolloServer.start();
apolloServer.createHandler({ path: '/api/graphql' })(req, res);
};
export default handler;
有人知道我做错了什么吗?
我认为它在我的处理程序函数中,但我不知道为什么。
我发现我做错了什么,只需要在调用createHandler
之前将apolloServer.start()
保留在一个变量中
const startServer = apolloServer.start();
// Run server
const handler = async (req, res) => {
await startServer;
apolloServer.createHandler({ path: '/api/graphql' })(req, res);
};