Here is the official Graphql (Apollo server) script which eventually end up in

throw new Error('You must `await server.start()` before calling `server.'   
//index.js
const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');

const typeDefs = gql`
type Query {
hello: String
}
`;

const resolvers = {
Query: {
hello: () => 'Hello world!',
},
};

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

const app = express();
server.applyMiddleware({ app });

app.listen({ port: 4000 }, () =>
console.log('Now browse to http://localhost:4000' + server.graphqlPath)
);

Here the server , which is the Apollo server object invoked (it is an async function), mean time the next line of code, the middle ware going to execute, before the server complete its initialization, that is cause for the error.

So what is required is that, make await apollo to start completely and then apply the middle ware tot it.

Todo this we either need to wrap middleware in a async function or wait for the promise.

....   
const app = express();
server.start().then(res => {
server.applyMiddleware({ app });
app.listen({ port: 3000 }, () =>
    console.log('Now browse to http://localhost:4000' + server.graphqlPath)
)
})

I hope this will help you

Here is a list of Apollo posts that may save your time


This free site is ad-supported. Learn more