-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (61 loc) · 2.01 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const { ApolloServer } = require('apollo-server-express');
const cors = require('cors')
const express = require('express');
const http = require('http');
const { sequelize } = require('./models')
require('dotenv').config()
const corsOptions = {
origin: 'http://localhost:3000',
credentials: true
}
// The GraphQL schema
// A map of functions which return data for the schema.
const resolvers = require('./graphql/resolvers')
const typeDefs = require('./graphql/typeDefs')
const ctxMiddleware = require('./utils/ctxMiddleware')
async function startApolloServer() {
const PORT = 4000;
const app = express();
app.use(cors(corsOptions))
app.use(express.static(__dirname + '/public/images'))
const server = new ApolloServer({
typeDefs,
resolvers,
context: ctxMiddleware,
// subscriptions: {
// onConnect: async (connectionParams, webSocket) => {
// console.log('xxx', connectionParams);
// },
// },
});
await server.start();
server.applyMiddleware({ app })
const httpServer = http.createServer(app);
server.installSubscriptionHandlers(httpServer);
// 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}`);
try {
await sequelize.authenticate();
console.log('Database connected!');
} catch (error) {
console.error('Database connection failed:', error);
}
return { server, app, httpServer };
}
startApolloServer()
// const server = new ApolloServer({
// typeDefs,
// resolvers,
// context: ctxMiddleware
// });
// const httpServer = http.createServer(app);
// server.listen().then(({ url }) => {
// console.log(`🚀 Server ready at ${url}`);
// sequelize.authenticate().then(() => {
// console.log('Database connected!');
// }).catch((err) => {
// console.log('Database connection failed');
// })
// });