【问题标题】:Error: You must `await server.start()` before calling `server.applyMiddleware()`错误:您必须在调用 `server.applyMiddleware()` 之前`await server.start()`
【发布时间】:2021-10-07 09:21:15
【问题描述】:

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() 并不能解决问题。有什么建议吗?

【问题讨论】:

    标签: apollo-server


    【解决方案1】:

    在 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

    【讨论】:

      【解决方案2】:

      这对我来说是await server.start()的替代方法

      server.start().then(res => {
        server.applyMiddleware({ app, path: '/' });
        app.listen({ port }, () => 
          console.log(`Gateway API running at port: ${port}`)
        );  
      });
      

      这个post 引导我找到答案。

      【讨论】:

        【解决方案3】:

        我可以按照文档中的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}`);
        }
        

        【讨论】:

        • 这解决了我的问题。
        猜你喜欢
        • 2021-10-09
        • 2021-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-22
        相关资源
        最近更新 更多