-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample-server.js
54 lines (45 loc) · 1.33 KB
/
example-server.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
import express from 'express';
import { ApolloServer } from '@apollo/server';
import { expressMiddleware } from '@apollo/server/express4';
import http from 'http';
import cors from 'cors';
import { json } from 'body-parser';
import NodeCache from 'node-cache';
import { schema } from './src';
const debug = require('debug')('untappd-graphql');
debug.enabled = true;
const port = process.env.PORT || 9090;
if (!process.env.UNTAPPD_CLIENT_ID || !process.env.UNTAPPD_CLIENT_SECRET) {
debug('UNTAPPD_CLIENT_ID and UNTAPPD_CLIENT_SECRET must be set in env.');
process.exit(1);
}
async function startServer() {
// Cache in memory
const context = {
cache: new NodeCache({
stdTTL: 60 * 60 * 24 * 7, // cache for one week
}),
};
const server = new ApolloServer({
schema,
context,
formatError: (err) => {
const { status, message } = err.originalError;
return { status, message };
},
});
const app = express();
const httpServer = http.createServer(app);
await server.start();
app.use(
'/graphql',
cors(),
json(),
expressMiddleware(server),
);
// Start the server
// eslint-disable-next-line no-promise-executor-return
await new Promise((resolve) => httpServer.listen({ port }, resolve));
debug(`🚀 Server ready at http://localhost:${port}/graphql`);
}
startServer();