graphql and Apollo server error in start Apollo server

Asked

Viewed 16 times

0

I have a problem with graphql and Apollo server and do not know why this error persists and do not know how to solve. Even putting 'await server.start()' still giving error SyntaxError: await is only valid in async functions and the top level bodies of modules

console error

Error: You must `await server.start()` before calling `server.applyMiddleware()`
at Object.<anonymous> (C:\Desenvolvedor\Quiz_JovemGenios\jovemgenio-app\packages\server\src\main.js:15:8)
at Generator.next (<anonymous>)

main.js

import express from 'express';
import { ApolloServer }  from 'apollo-server-express';
import typeDefs  from './graphql/typeDefs';
import resolvers from './graphql/resolvers';

const app =  express();

const server = new ApolloServer ({
    typeDefs: typeDefs,
    resolvers: resolvers,
});


server.applyMiddleware({
    app,
    cors: {
       origin: 'http://localhost:30000'
    },
    bodyParserConfig: true,
});



 const PORT = process.env.PORT ? parseInt(process.env.PORT) : 80000
 const HOSTNAME = process.env.HOSTNAME || '127.0.0.1'

app.listen(8000, '127.0.0.1', async() => {
    console.log(`app is listening at http://${HOSTNAME}:${PORT}`)

});

1 answer

0

I solved the error as follows

    async function startApolloServer() {
    const app = express();
    const server = new ApolloServer({
      typeDefs,
      resolvers,
    });
    await server.start();
   server.applyMiddleware({
    app: app,
    cors: {
       origin: 'http://localhost:30000'
    },
    bodyParserConfig: true,
});
};

but the problem still persists after http://localhost:8000/graphql does not start

browser view

Cannot GET /graphql

Browser other questions tagged

You are not signed in. Login or sign up in order to post.