Error: called start() with surprising state started #6436
-
Hello, I am using nextjs with const GraphQLServer = new ApolloServer({
schema: GraphQLSchema.buildSchema(),
introspection: true,
plugins: [ApolloServerPluginLandingPageGraphQLPlayground()],
});
async function handler(req: NextApiRequest, res: NextApiResponse) {
await GraphQLServer.start();
await GraphQLServer.createHandler({ path: "/api/graphql", })(req, res);
}
export const config = {
api: {
bodyParser: false,
},
};
export default withMongo(handler); I don't understand what |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I will answer my question for future viewers. The problem with my code is that I am initializing the server on every request, and you can guess this can introduce many errors, one of which is the one I posted. The solution is to use this code const GraphQLServer = new ApolloServer({
schema: GraphQLSchema.buildSchema(),
introspection: true,
plugins: [ApolloServerPluginLandingPageGraphQLPlayground()],
});
const startServer = GraphQLServer.start()
async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method === 'OPTIONS') {
res.end();
return false;
}
await startServer;
await GraphQLServer.createHandler({ path: "/api/graphql", })(req, res);
}
export const config = {
api: {
bodyParser: false,
},
};
export default withMongo(handler); The real fix (other than some code improvements) is the fact that I initiate a single promise with calling The promise lives there all alone, on it's own. Let's cheer the promise up, it's doing a great job keeping the server up! |
Beta Was this translation helpful? Give feedback.
-
A little modification on the accepted answer.
|
Beta Was this translation helpful? Give feedback.
I will answer my question for future viewers.
The problem with my code is that I am initializing the server on every request, and you can guess this can introduce many errors, one of which is the one I posted.
The solution is to use this code