-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
82 lines (72 loc) · 1.83 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* Load environment variables
* https://github.com/motdotla/dotenv
*/
require('dotenv').config()
/**
* Init Objection
*/
require('./lib/objection')
/**
* GraphQL related code
*/
const { Action } = require('./models/Action')
const { User } = require('./models/User')
const { ApolloServer } = require('apollo-server')
const { GraphQLJSONObject } = require('graphql-type-json')
const { queryResolvers, mutationResolvers, setContext } = require('./resolvers')
const typeDefs = require('./schema')
const Sentry = require('@sentry/node')
const resolvers = {
/**
* Custom scalar in order to support "Object" without
* strong type properties
* WARNING: this should be used only for specific proposes
*/
JSONObject: GraphQLJSONObject,
Query: {
...queryResolvers
},
Mutation: {
...mutationResolvers
},
// Allow to append action data into the UserAction request
UserAction: {
action: async (userAction) => {
const { actionId } = userAction
const result = await Action.query().findById(actionId)
return result
}
},
// Allow to append campaigns data into User request
User: {
campaigns: async (user) => {
const result = await User.query().findById(user.id).joinEager('campaigns')
if (result) {
return result.campaigns
}
return []
}
}
}
// Init Sentry
Sentry.init({
dsn: process.env.SENTRY_DSN
})
const originSafelist = (process.env.CORS_ORIGIN || '').split(',')
const corsOptions = {
origin: originSafelist,
credentials: true
}
const server = new ApolloServer({
cors: corsOptions,
typeDefs,
resolvers,
introspection: process.env.INTROSPECTION === 'true',
playground: process.env.PLAYGROUND === 'true',
context: setContext
})
server.listen().then(({ url }) => {
// eslint-disable-next-line
console.log(`🚀 Server ready at ${url}`)
})