Skip to content

Error: called start() with surprising state started #6436

Discussion options

You must be logged in to vote

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", })

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@glasser
Comment options

Answer selected by jimfilippou
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants