使用 apollo-server-express 迁移到 3.5.0

Migrating to 3.5.0 with apollo-server-express

我正在尝试将我的一个项目的版本从 node 12.x.x 更新到 node.14.x.x。 现在我看到了 apollo 迁移到 v3 的文档,但完全不清楚有人知道从哪里开始?从头开始制作所有阿波罗零件系统会更容易吗? 应更新的当前文件:

const { ApolloServer } = require('apollo-server-express');
const { makeExecutableSchema } = require('graphql-tools');
const { applyMiddleware } = require('graphql-middleware');
const { gql } = require('apollo-server-express');
const { resolvers, typeDefs } = require('graphql-scalars');

const { types } = require('./typedefs/types');
const { inputs } = require('./typedefs/inputs');
const { queries } = require('./typedefs/queries');
const { mutations } = require('./typedefs/mutations');

const customResolvers = require('./resolvers');
const { permissions } = require('./permissions/permissions');

const graphqlApis = gql`
  ${types}
  ${queries}
  ${inputs}
  ${mutations}
`;

const schema = makeExecutableSchema({
  typeDefs: [...typeDefs, graphqlApis],
  resolvers: {
    ...customResolvers,
    ...resolvers,
  },
});

const server = new ApolloServer({
  context: ({ req }) => {
    const headers = req.headers || '';
    return headers;
  },
  schema: applyMiddleware(schema, permissions),
});

module.exports = server;
尝试 运行 时的当前错误:Error: You must 'await server.start()' before calling 'server.applyMiddleware()'

我很想用它得到一些 solutions/help/insight。

如文档中所述,您需要使用 Async 函数包围您的代码 https://www.apollographql.com/docs/apollo-server/migration/

    (async function () {
       // ....
    })();

或者像这样:... 首先定义你的类型定义和解析器

const typeDefs = "" // your typedefs here
const resolvers = "" // your resolvers here
async function startApolloServer(typeDefs, resolvers) {
  // Apollo Express 3.5.x code goes here
  // ....
  await server.start();
 // promise resolve...
}
startApolloServer(typeDefs, resolvers)