-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
127 lines (113 loc) · 4.11 KB
/
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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
'use strict';
// import * as dotenv from 'dotenv'
// dotenv.config()
// import './dotenv.js'
// import '../config/config.js'
import { ConfigReader } from '../config/config-reader.js';
const cr = new ConfigReader('../docker/.env');
const cfg = cr.getConfig();
console.debug('config', cfg)
import { ApolloServer } from '@apollo/server';
import { ApolloGateway, IntrospectAndCompose, RemoteGraphQLDataSource } from '@apollo/gateway';
// import { loadErrorMessages, loadDevMessages } from "@apollo/client/dev";
// if (__DEV__) { // Adds messages only in a dev environment
// loadDevMessages();
// loadErrorMessages();
// }
import { buildSubgraphSchema } from '@apollo/subgraph';
import { startStandaloneServer } from '@apollo/server/standalone';
import jwt from 'jsonwebtoken';
import { typeDefs } from './lib/typeDefs.graphql.js';
import { resolvers } from './lib/resolvers.js';
import { db } from './models/index.js'
import { readFileSync } from 'fs'
// const JWT_SECRET_KEY = process.env.JWT_SECRET_KEY || 'SECRET_KEY'
const JWT_SECRET_KEY = cfg.jwtSecretKey
const PORT = cfg.graphql.port
// // const CUBEJS_API_URL = 'http://localhost:4001'
// const CUBEJS_API_URL = cr.readEnv('SUBLEDGR_CUBEJS_API_URL', 'http://localhost:4001')
// class CubeJsDataSource extends RemoteGraphQLDataSource {
// async load(options) {
// const res = await fetch(`${CUBEJS_API_URL}/cubejs-api/v1/load?query=${encodeURIComponent(JSON.stringify(options))}`);
// return res.json();
// }
// }
// handle graceful shutdown, incl. running in docker...
function shutdown(signal) {
return (err) => {
console.log(`${ signal }...`);
if (err) console.error(err.stack || err);
setTimeout(() => {
console.log('...waited 5s, exiting.');
process.exit(err ? 1 : 0);
}, 5000).unref();
};
}
// handle crtl-c
process
.on('SIGTERM', shutdown('SIGTERM'))
.on('SIGINT', shutdown('SIGINT'))
.on('uncaughtException', shutdown('uncaughtException'));
async function getUserFromToken(token) {
// console.debug('getUserFromToken', token)
if (token === '') return null
try {
token = token.split(' ')[1]
// console.debug('getUserFromToken', token)
const { userId } = jwt.verify(token, JWT_SECRET_KEY);
// console.debug('userId', userId)
const user = await db.User.findOne({ where: { id: userId } }) //.then((user) => {
if (!user) console.log('we have no user for', userId)
return user
// })
} catch (error) {
console.debug(error)
return null;
}
}
// // const supergraphSdl = readFileSync('./supergraph.graphql').toString();
// const gateway = new ApolloGateway({
// serviceList: [
// // { name: 'default', url: 'http://localhost:4000' },
// { name: 'indexDb', url: CUBEJS_API_URL },
// ],
// // supergraphSdl: new IntrospectAndCompose({
// // subgraphs: [
// // { name: 'indexDb', url: 'http://localhost:4001' }
// // ]
// // }),
// buildService({ name, url }) {
// if(name === 'indexDb') {
// return new CubeJsDataSource({ url });
// }
// return new RemoteGraphQLDataSource({ url });
// }
// });
// The ApolloServer constructor requires two parameters: your schema
// definition and your set of resolvers.
const server = new ApolloServer({
// gateway,
// typeDefs,
// resolvers
// to support federation, we need to build a subgraph schema
schema: buildSubgraphSchema({ typeDefs, resolvers }),
});
// Passing an ApolloServer instance to the `startStandaloneServer` function:
// 1. creates an Express app
// 2. installs your ApolloServer instance as middleware
// 3. prepares your app to handle incoming requests
;(async () => {
const pkg = JSON.parse(readFileSync('./package.json', 'utf-8'))
console.log(`Package: ${pkg.name} v. ${pkg.version}`)
const { url } = await startStandaloneServer(server, {
listen: { port: PORT },
context: async ({ req }) => {
// this runs for each query! keep it light...
// console.debug('context firing...', req)
const token = req.headers.authorization || '';
const user = await getUserFromToken(token);
return { token, user, db };
},
});
console.log(`🚀 Server ready at: ${url}`);
})()