错误:在调用server.applyMiddleware()之前必须' await server.start() '.&



apollo-server更新到版本3后,在控制台中显示以下错误

C:projectsmy_projectnode_modulesapollo-server-coresrcApolloServer.ts:554
throw new Error(
^
Error: You must `await server.start()` before calling `server.applyMiddleware()`
at ApolloServer.assertStarted (C:projectsmy_projectnode_modulesapollo-server-coresrcApolloServer.ts:554:13)
at ApolloServer.applyMiddleware (C:projectsmy_projectnode_modulesapollo-server-expresssrcApolloServer.ts:60:10)
at Object.<anonymous> (C:projectsmy_projectsrcindex.ts:17:14)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Module.m._compile (C:projectsmy_projectnode_modulests-nodesrcindex.ts:1225:23)
at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Object.require.extensions.<computed> [as .ts] (C:projectsmy_projectnode_modulests-nodesrcindex.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()并不能解决问题。有什么建议吗?

这就是我作为await server.start()替代品的工作

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

这篇文章让我找到了答案。

在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文档

我按照文档中的指南解决了这个问题。

这个例子是从文档

复制粘贴过来的
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}`);
}

如果您正在使用Nextjs并且想要实现一个apollo服务器,这可能会对您有所帮助:

pages/api/graphql.ts

import { ApolloServer } from "apollo-server-micro";
import { typeDefs } from "./schemas";
import { NextApiRequest, NextApiResponse } from "next";
import { ApolloServerPluginLandingPageGraphQLPlayground } from "apollo-server-core";
import { resolvers } from "./resolvers";
const GraphQLServer = new ApolloServer({
introspection: true,
typeDefs,
resolvers,
plugins: [ApolloServerPluginLandingPageGraphQLPlayground()],
});
const startServer = GraphQLServer.start();
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
res.setHeader("Access-Control-Allow-Credentials", "true");
res.setHeader(
"Access-Control-Allow-Origin",
"https://studio.apollographql.com"
);
res.setHeader(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Access-Control-Allow-Methods, Access-Control-Allow-Origin, Access-Control-Allow-Credentials, Access-Control-Allow-Headers"
);
res.setHeader(
"Access-Control-Allow-Methods",
"POST, GET, PUT, PATCH, DELETE, OPTIONS, HEAD"
);
if (req.method === "OPTIONS") {
res.end();
return false;
}
await startServer;
await GraphQLServer.createHandler({
path: "/api/graphql",
})(req, res);
}
export const config = {
api: {
bodyParser: false,
},
};

/pages/api/resolvers.js

import axios from "axios";
export const resolvers = {
Query: {
getUsers: async () => {
console.log("users");
try {
const users = await axios.get("https://api.github.com/users");
return users.data.map(({ id, login, avatar_url }) => ({
id,
login,
avatar_url,
}));
} catch (error) {
throw error;
}
},
getUser: async (_, args) => {
try {
const user = await axios.get(
`https://api.github.com/users/${args.name}`
);
return {
id: user.data.id,
login: user.data.login,
avatar_url: user.data.avatar_url,
};
} catch (error) {
throw error;
}
},
},
};

/pages/api/schemas.ts

import { gql } from "apollo-server-micro";
export const typeDefs = gql`
type User {
id: ID
login: String
avatar_url: String
}
type Query {
getUsers: [User]
getUser(name: String!): User!
}
`;

最新更新