This repository has been archived by the owner on Sep 18, 2023. It is now read-only.
forked from hackjunction/JunctionApp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphql-controller-factory.js
49 lines (46 loc) · 1.84 KB
/
graphql-controller-factory.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
const UserProfileController = require('./user-profile/graphql-controller')
const RegistrationController = require('./registration/graphql-controller')
const EventController = require('./event/graphql-controller')
const OrganizationController = require('./organization/graphql-controller')
const MessageController = require('./message/graphql-controller')
const AlertController = require('./alert/graphql-controller')
const MeetingController = require('./meeting/graphql-controller')
function buildController(key, context) {
const user = context.req && context.req.user
switch (key) {
case 'UserProfile':
return new UserProfileController(user)
case 'Registration':
return new RegistrationController(user)
case 'Event':
return new EventController(user)
case 'Organization':
return new OrganizationController(user)
case 'Message':
return new MessageController(user)
case 'Alert':
return new AlertController(user)
case 'Meeting':
return new MeetingController(user)
default: {
throw new Error(`No controller specified for key ${key}!`)
}
}
}
/** This way we can have easy access to all of our controllers in the
* context without having to instantiate ALL controllers for every request.
*/
module.exports = function buildGetController() {
const store = new Map()
return function factory(key) {
/** If we already have an instance of the controller, return it */
if (store.has(key)) {
return store.get(key)
}
/** Otherwise create it, and add it to the map for later use.
* This refers to the context of the graphql query */
const controller = buildController(key, this)
store.set(key, controller)
return controller
}
}