qrapghql apollo server

ยท

1 min read

async function startApolloServer() {
  const server = new ApolloServer({ typeDefs, resolvers, context: async({req})=>{
    const {isAuth} = await auth(req)
    if(!isAuth)throw new AuthenticationError('operation is not allowed')

    return {
      db,
      pubsub,
      isAuth

    }
  }  });
    await server.start();
    server.applyMiddleware({app})

    const httpServer = http.createServer(app);
    server.installSubscriptionHandlers(httpServer);

    mongoose.connect('mongodb://127.0.0.1:27017/users', {
      useNewUrlParser: true,useUnifiedTopology: true 
    })
    .then(()=>{
      console.log('connect to database successfully');
    }).catch(e=> console.log(e.message) )

    // Make sure to call listen on httpServer, NOT on app.
    await new Promise(resolve => httpServer.listen(PORT, resolve));
    console.log(`๐Ÿš€ Server ready at http://localhost:${PORT}${server.graphqlPath}`);
    console.log(`๐Ÿš€ Subscriptions ready at ws://localhost:${PORT}${server.subscriptionsPath}`);
    return { server, app, httpServer };
  }

  startApolloServer()