-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
70 lines (60 loc) · 1.8 KB
/
index.ts
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
68
69
70
// /* eslint-disable no-console */
import mongoose from 'mongoose';
import dotenv from 'dotenv';
import { createServer } from 'http';
import express from 'express';
import { execute, subscribe } from 'graphql';
import { ApolloServer } from 'apollo-server-express';
import { PubSub } from 'graphql-subscriptions';
import { SubscriptionServer } from 'subscriptions-transport-ws';
import { makeExecutableSchema } from '@graphql-tools/schema';
import { ApolloServerPluginLandingPageGraphQLPlayground } from 'apollo-server-core';
import typeDefs from './typeDefs';
import resolvers from './resolvers';
import GameModel from './models/GameModel';
import UserModel from './models/UserModel';
dotenv.config();
export interface IContext {
UserModel: typeof UserModel;
GameModel: typeof GameModel;
pubsub: PubSub;
}
export const pubsub = new PubSub();
(async () => {
const PORT = 5000;
const app = express();
const httpServer = createServer(app);
const schema = makeExecutableSchema({ typeDefs, resolvers });
const context: IContext = {
UserModel,
GameModel,
pubsub,
};
mongoose.connect(process.env.MONGO_DB!, (err) => {
if (err) throw err;
console.log('MongoDB connected successfully');
});
const server = new ApolloServer({
schema,
context,
plugins: [
ApolloServerPluginLandingPageGraphQLPlayground(),
],
});
await server.start();
server.applyMiddleware({ app });
SubscriptionServer.create(
{
schema, execute, subscribe,
},
{ server: httpServer, path: server.graphqlPath },
);
httpServer.listen(PORT, () => {
console.log(
`🚀 Query endpoint ready at http://localhost:${PORT}${server.graphqlPath}`,
);
console.log(
`🚀 Subscription endpoint ready at ws://localhost:${PORT}${server.graphqlPath}`,
);
});
})();