-
Notifications
You must be signed in to change notification settings - Fork 0
/
local.js
87 lines (72 loc) · 2.19 KB
/
local.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
83
84
85
86
87
const http = require('http');
const {graphql} = require('graphql');
const config = require('./config');
const DynamoDB = require('./src/storages/DynamoDB');
const storage = new DynamoDB({
tables: config.dynamo.entityTables,
tableSuffix: process.env.NODE_ENV === 'production' ? config.dynamo.prodTableSuffix : '',
dynamo: {
accessKeyId: config.aws.keyId,
secretAccessKey: config.aws.secret,
region: config.aws.region
}
});
const ModelFactory = require('./src/models');
const modelFactory = new ModelFactory(storage);
const Cognito = require('./src/auth/Cognito');
const auth = new Cognito({
accessKeyId: config.aws.keyId,
secretAccessKey: config.aws.secret,
region: config.aws.region
});
const appRegistry = require('./src/appRegistry');
appRegistry.set('models', modelFactory);
appRegistry.set('auth', auth);
const graphqlSchema = require('./src/graphqlSchema');
const server = http.createServer((req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Content-Type', 'application/json');
if (req.method !== 'POST') {
res.statusCode = 405;
res.end(JSON.stringify('Method Not Allowed'));
return;
}
const chunks = [];
req.on('data', chunk => chunks.push(chunk.toString()));
req.on('end', async () => {
const body = chunks.join('');
const result = await handleRequest(body);
res.statusCode = result.statusCode;
res.end(JSON.stringify(result.data));
});
});
async function handleRequest(body) {
try {
const result = await graphql(graphqlSchema, body);
if (result.errors) {
console.error(JSON.stringify(result.errors));
return {
statusCode: 500,
data: 'Internal error'
};
}
return {
statusCode: 200,
data: result,
};
} catch(err) {
console.error(err);
return {
statusCode: 500,
data: 'Internal error',
};
}
}
const port = 3001;
server.listen(port, 'localhost', err => {
if (err) {
console.error(err);
return;
}
console.log('Listening on', port);
});