-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_handler.js
60 lines (53 loc) · 1.41 KB
/
api_handler.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
const fs = require('fs');
require('dotenv').config();
const { ApolloServer } = require('apollo-server-express');
const GraphQLDate = require('./graphql_date.js');
const about = require('./about.js');
const issue = require('./issue.js');
const auth = require('./auth.js');
const resolvers = {
Query: {
about: about.getMessage,
user: auth.resolveUser,
issueList: issue.list,
issue: issue.get,
issueCounts: issue.counts,
},
Mutation: {
setAboutMessage: about.setMessage,
issueAdd: issue.add,
issueUpdate: issue.update,
issueDelete: issue.delete,
issueRestore: issue.restore,
},
GraphQLDate,
};
function getContext({ req }) {
const user = auth.getUser(req);
return { user };
}
const server = new ApolloServer({
typeDefs: fs.readFileSync('schema.graphql', 'utf-8'),
resolvers,
context: getContext,
formatError: (error) => {
console.log(error);
return error;
},
playground: true,
introspection: true,
});
function installHandler(app) {
const enableCors = (process.env.ENABLE_CORS || 'true') === 'true';
console.log('CORS setting:', enableCors);
let cors;
if (enableCors) {
const origin = process.env.UI_SERVER_ORIGIN || 'http://localhost:8000';
const methods = 'POST';
cors = { origin, methods, credentials: true };
} else {
cors = 'false';
}
server.applyMiddleware({ app, path: '/graphql', cors });
}
module.exports = { installHandler };