-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
73 lines (60 loc) · 2.22 KB
/
index.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
const renderGraphiql = require('./renderGraphiql');
const anagraphCreator = require('./Parser/anagraphCreator');
const schemaParser = require('./Parser/schemaParser');
const ruleValidator = require('./Parser/ruleValidator');
const queryValidator = require('./Parser/queryValidator');
const anagraphql = options => ((req, res, next) => {
if (req.method !== 'GET' && req.method !== 'POST') {
res.setHeader('Allow', 'GET, POST');
res.status(405).send('GraphQL only supports GET and POST requests.');
res.end();
}
const { schema, graphiql, rules } = options;
if (!schema) throw new Error('GraphQL middleware options must contain a schema.');
const applicableRules = schemaParser(schema);
if (req.body.operationName !== undefined && graphiql) {
const oldSend = res.send;
res.send = function (...data) {
// arguments[0] (or `data`) contains the response body
res.send = oldSend;
data[0] = JSON.stringify({ ...JSON.parse(data[0]), applicableRules });
oldSend.apply(res, data);
};
return next();
}
if (graphiql && req.method === 'GET') {
if (!graphiql) return next();
if (rules === undefined) res.send(renderGraphiql());
if (rules) res.send(renderGraphiql(rules));
return res.end();
}
if (req.body.query) {
const RULES_TO_CHECK = req.body.currRule || rules
console.log(`RULES IN BACKEND: ${JSON.stringify(RULES_TO_CHECK)}`);
const anagraph = anagraphCreator(req.body.query);
if (RULES_TO_CHECK) {
const validateRules = ruleValidator(applicableRules, RULES_TO_CHECK);
if (validateRules.error) {
res.status(422).send({ error: validateRules.error });
return res.end();
}
const evaluation = queryValidator(anagraph, RULES_TO_CHECK);
if (evaluation.error) {
res.status(422).send({ error: evaluation.error });
return res.end();
}
}
if (graphiql) {
const oldSend = res.send;
res.send = function(...data){
// arguments[0] (or `data`) contains the response body
res.send = oldSend
data[0] = JSON.stringify({ ...JSON.parse(data[0]), anagraph });
oldSend.apply(res, data);
}
return next();
}
return next();
}
});
module.exports = anagraphql;