Error: You must `await server.start()` before calling `server.applyMiddleware()`
Error: You must `await server.start()` before calling `server.applyMiddleware()`
将 apollo-server
更新到版本 3 后,控制台中显示以下错误
C:\projects\my_project\node_modules\apollo-server-core\src\ApolloServer.ts:554
throw new Error(
^
Error: You must `await server.start()` before calling `server.applyMiddleware()`
at ApolloServer.assertStarted (C:\projects\my_project\node_modules\apollo-server-core\src\ApolloServer.ts:554:13)
at ApolloServer.applyMiddleware (C:\projects\my_project\node_modules\apollo-server-express\src\ApolloServer.ts:60:10)
at Object.<anonymous> (C:\projects\my_project\src\index.ts:17:14)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Module.m._compile (C:\projects\my_project\node_modules\ts-node\src\index.ts:1225:23)
at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Object.require.extensions.<computed> [as .ts] (C:\projects\my_project\node_modules\ts-node\src\index.ts:1228:12)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
[nodemon] app crashed - waiting for file changes before starting...
在 server.applyMiddleware()
之前添加 server.start()
并不能解决问题。有什么建议吗?
我按照文档中的 this guide 解决了这个问题。
此示例是从文档中复制粘贴的
import { ApolloServer } from 'apollo-server-express';
import express from 'express';
async function startApolloServer(typeDefs, resolvers) {
// Same ApolloServer initialization as before
const server = new ApolloServer({ typeDefs, resolvers });
// Required logic for integrating with Express
await server.start();
const app = express();
server.applyMiddleware({
app,
// By default, apollo-server hosts its GraphQL endpoint at the
// server root. However, *other* Apollo Server packages host it at
// /graphql. Optionally provide this to match apollo-server.
path: '/'
});
// Modified server startup
await new Promise(resolve => app.listen({ port: 4000 }, resolve));
console.log(` Server ready at http://localhost:4000${server.graphqlPath}`);
}
在 apollo server 3 中,您只包含您需要的内容。要将所需的逻辑与 Express 集成,您需要在实例化服务器后调用 await server.start()
。
示例:
...
const apolloServer = new ApolloServer({
schema,
... // other config
});
// without this, apollo will throw an error.
await apolloServer.start();
const app = express();
apolloServer.applyMiddleware({
app,
... // other config
});
...
检查新的 apollo 文档以了解更改 here
这对我有用,可以替代 await server.start()
server.start().then(res => {
server.applyMiddleware({ app, path: '/' });
app.listen({ port }, () =>
console.log(`Gateway API running at port: ${port}`)
);
});
这个 post 让我找到了答案。
将 apollo-server
更新到版本 3 后,控制台中显示以下错误
C:\projects\my_project\node_modules\apollo-server-core\src\ApolloServer.ts:554
throw new Error(
^
Error: You must `await server.start()` before calling `server.applyMiddleware()`
at ApolloServer.assertStarted (C:\projects\my_project\node_modules\apollo-server-core\src\ApolloServer.ts:554:13)
at ApolloServer.applyMiddleware (C:\projects\my_project\node_modules\apollo-server-express\src\ApolloServer.ts:60:10)
at Object.<anonymous> (C:\projects\my_project\src\index.ts:17:14)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Module.m._compile (C:\projects\my_project\node_modules\ts-node\src\index.ts:1225:23)
at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Object.require.extensions.<computed> [as .ts] (C:\projects\my_project\node_modules\ts-node\src\index.ts:1228:12)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
[nodemon] app crashed - waiting for file changes before starting...
在 server.applyMiddleware()
之前添加 server.start()
并不能解决问题。有什么建议吗?
我按照文档中的 this guide 解决了这个问题。
此示例是从文档中复制粘贴的
import { ApolloServer } from 'apollo-server-express';
import express from 'express';
async function startApolloServer(typeDefs, resolvers) {
// Same ApolloServer initialization as before
const server = new ApolloServer({ typeDefs, resolvers });
// Required logic for integrating with Express
await server.start();
const app = express();
server.applyMiddleware({
app,
// By default, apollo-server hosts its GraphQL endpoint at the
// server root. However, *other* Apollo Server packages host it at
// /graphql. Optionally provide this to match apollo-server.
path: '/'
});
// Modified server startup
await new Promise(resolve => app.listen({ port: 4000 }, resolve));
console.log(` Server ready at http://localhost:4000${server.graphqlPath}`);
}
在 apollo server 3 中,您只包含您需要的内容。要将所需的逻辑与 Express 集成,您需要在实例化服务器后调用 await server.start()
。
示例:
...
const apolloServer = new ApolloServer({
schema,
... // other config
});
// without this, apollo will throw an error.
await apolloServer.start();
const app = express();
apolloServer.applyMiddleware({
app,
... // other config
});
...
检查新的 apollo 文档以了解更改 here
这对我有用,可以替代 await server.start()
server.start().then(res => {
server.applyMiddleware({ app, path: '/' });
app.listen({ port }, () =>
console.log(`Gateway API running at port: ${port}`)
);
});
这个 post 让我找到了答案。