/
server.js
146 lines (123 loc) · 3.67 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { host, port, mongoURI } from './config';
import http from 'http';
import express from 'express';
import mongoose from 'mongoose';
import path from 'path';
import fs from 'fs-extra';
import fetch from 'node-fetch';
import jwt_decode from 'jwt-decode';
import { ApolloServer, PubSub } from 'apollo-server-express';
import { typeDefs, resolvers } from './graphql/schema';
import startRfidServer from './rfid-server';
import startQueueWorker from './queue-worker';
import User from './models/User';
const pubsub = new PubSub();
const app = express();
// Connect to MongoDB
mongoose
.connect(mongoURI, { useNewUrlParser: true, useFindAndModify: false, useCreateIndex: true })
.then(() => console.log('MongoDB Connected'))
.catch(err => console.log(err));
app.set('view engine', 'pug');
app.get('/', (req, res) => {
res.render('index');
});
app.use(express.static(path.join(__dirname, 'public')));
app.all( /^\/(?!graphql)(.*)\/?$/i, (req, res) => {
res.render('index');
});
const server = new ApolloServer({
// These will be defined for both new or existing servers
typeDefs,
resolvers,
introspection: true,
context: async ({ req, connection }) => {
let context = { pubsub };
if (connection)
return { ...connection.context, pubsub };
// get the user token from the headers
const token = req.headers.authorization || '';
if(token) {
// try to retrieve a user with the token
const { login } = jwt_decode(token);
const getUser = async (login) => {
let user = await User.findOne({ login }, 'login');
return user;
};
let loggedUser = await getUser(login);
context.user = loggedUser;
}
context.db_conn = mongoose.connection;
return context;
},
playground: {
settings: {
'schema.polling.enable': false,
}
},
// subscriptions: {
// path: '/subscriptions',
// onConnect: (connectionParams, webSocket, context) => {
// ...
// },
// onOperation: (message, params, webSocket) => {
// Manipulate and return the params, e.g.
// params.context.randomId = uuid.v4();
// Or specify a schema override
// if (shouldOverrideSchema()) {
// params.schema = newSchema;
// }
// return params;
// },
// onOperationComplete: webSocket => {
// ...
// },
// onDisconnect: (webSocket, context) => {
// ...
// },
// },
});
server.applyMiddleware({ app }); // app is from an existing express app
const httpServer = http.createServer(app);
server.installSubscriptionHandlers(httpServer);
httpServer.listen(port, host, () => {
console.log(`🚀 Server ready at http://${host}:${port}${server.graphqlPath}`);
console.log(`🚀 Subscriptions ready at ws://${host}:${port}${server.subscriptionsPath}`);
});
fetch(`http://${host}:${port}/graphql`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
variables: {},
operationName: '',
query: `
{
__schema {
types {
kind
name
possibleTypes {
name
}
}
}
}
`,
}),
})
.then(result => result.json())
.then(result => {
// here we're filtering out any type information unrelated to unions or interfaces
const filteredData = result.data.__schema.types.filter(
type => type.possibleTypes !== null,
);
result.data.__schema.types = filteredData;
// Start RFID TCP server
startRfidServer(result.data);
startQueueWorker(result.data);
fs.writeFile(path.join(__dirname, 'src', 'fragmentTypes.json'), JSON.stringify(result.data), err => {
if (err) {
console.error('Error writing fragmentTypes file', err);
}
});
});