-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathgraphql.js
More file actions
109 lines (101 loc) · 3.38 KB
/
Copy pathgraphql.js
File metadata and controls
109 lines (101 loc) · 3.38 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const { ApolloServer } = require('apollo-server-express')
const { ApolloServerPluginDrainHttpServer } = require('apollo-server-core')
const { mergeSchemas, makeExecutableSchema } = require('graphql-tools')
const { GraphQLSchema, printSchema } = require('graphql')
const { Server: WebSocketServer } = require('ws')
const { useServer } = require('graphql-ws/lib/use/ws')
const { createServer } = require('http')
// const { SharedSchema } = require('@hackjunction/shared/schemas')
/** Schemas */
const Registration = require('./registration/graphql')
const Event = require('./event/graphql')
const UserProfile = require('./user-profile/graphql')
const Organization = require('./organization/graphql')
const Meeting = require('./meeting/graphql')
const Message = require('./message/graphql')
const Alert = require('./alert/graphql')
// const Team = require('./team/graphql')
const buildGetController = require('./graphql-controller-factory')
const { verifyWsToken } = require('../misc/jwt')
module.exports = app => {
const modules = [
UserProfile,
Registration,
Event,
Organization,
Message,
Alert,
Meeting,
// Team,
]
const executableSchemas = modules.map(
({ QueryType, MutationType, SubscriptionType, Resolvers }) => {
const rawSchema = new GraphQLSchema({
query: QueryType,
mutation: MutationType,
subscription: SubscriptionType,
})
return makeExecutableSchema({
typeDefs: printSchema(rawSchema),
resolvers: Resolvers,
})
},
)
const schema = mergeSchemas({
schemas: executableSchemas,
})
const httpServer = createServer(app)
const wsServer = new WebSocketServer({
// This is the `httpServer` we created in a previous step.
server: httpServer,
// Pass a different path here if your ApolloServer serves at
// a different path.
path: '/graphql',
})
const serverCleanup = useServer(
{
schema,
onConnect: async ctx => {
const verified = await verifyWsToken(
ctx.connectionParams.authToken,
)
// console.info(`got token ${JSON.stringify(user)}`)
ctx.tokenData = verified
},
context: ctx => {
if (!ctx.tokenData) {
return { user: null }
}
return { user: ctx.tokenData }
},
},
wsServer,
)
const server = new ApolloServer({
schema,
playground: false,
introspection: false,
context: ({ req, res }) => ({
req,
res,
userId: req.user ? req.user.sub : null,
controller: buildGetController(),
}),
plugins: [
// Proper shutdown for the HTTP server.
ApolloServerPluginDrainHttpServer({ httpServer }),
// Proper shutdown for the WebSocket server.
{
async serverWillStart() {
return {
async drainServer() {
await serverCleanup.dispose()
},
}
},
},
],
})
server.applyMiddleware({ app })
return httpServer
}