-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
107 lines (83 loc) · 2.88 KB
/
app.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
// see https://github.com/mu-semtech/mu-javascript-template for more info
import { app, query, errorHandler, sparqlEscapeUri } from 'mu';
app.get('/', async function (req, res) {
const subject = req.query["subject"];
const defaultPageNumber = 0;
const defaultPageSize = 500;
const queryResult = await getDirectedLinks(subject, defaultPageNumber, defaultPageSize);
const inverseQueryResult = await getInverseLinks(subject, defaultPageNumber, defaultPageSize);
const response = {
directed: queryResult.triples,
inverse: inverseQueryResult.triples
};
res.send(response);
});
app.get('/direct', async function (req, res) {
const subject = req.query["subject"];
const pageNumber = req.query["pageNumber"] || 0;
const pageSize = req.query["pageSize"] || 500;
const response = await getDirectedLinks(subject, pageNumber, pageSize);
res.send(response);
});
app.get('/inverse', async function (req, res) {
const subject = req.query["subject"];
const pageNumber = req.query["pageNumber"] || 0;
const pageSize = req.query["pageSize"] || 500;
const response = await getInverseLinks(subject, pageNumber, pageSize)
res.send(response);
});
async function getDirectedLinks(subject, pageNumber, pageSize) {
const offset = pageSize * pageNumber;
const queryResult = await query(`SELECT DISTINCT ?p ?o
WHERE {
GRAPH <http://mu.semte.ch/application> {
${sparqlEscapeUri(subject)} ?p ?o.
}
} LIMIT ${pageSize} OFFSET ${offset}
`);
const queryResultCount = await query(`SELECT (COUNT( (?p)) as ?count)
WHERE {
GRAPH <http://mu.semte.ch/application> {
SELECT DISTINCT ?p ?o WHERE {
${sparqlEscapeUri(subject)} ?p ?o.
}
}
}
`);
const count = queryResultCount.results.bindings[0]['count'].value;
const response = {
triples: queryResult.results.bindings.map(({ p: { value: predicate }, o }) => {
return { subject, predicate, object: o };
}),
count: count
};
return response;
}
async function getInverseLinks(subject, pageNumber, pageSize) {
const offset = pageSize * pageNumber;
const inverseQueryResult = await query(`SELECT DISTINCT ?s ?p
WHERE {
GRAPH <http://mu.semte.ch/application> {
?s ?p ${sparqlEscapeUri(subject)}.
}
} LIMIT ${pageSize} OFFSET ${offset}
`);
const inverseQueryResultCount = await query(`SELECT (COUNT( (?s)) as ?count)
WHERE {
GRAPH <http://mu.semte.ch/application> {
SELECT DISTINCT ?s ?o WHERE {
?s ?p ${sparqlEscapeUri(subject)}.
}
}
}
`);
const count = inverseQueryResultCount.results.bindings[0]['count'].value;
const response = {
triples: inverseQueryResult.results.bindings.map(({ s: { value: inverseSubject }, p: { value: predicate } }) => {
return { subject: inverseSubject, predicate, object: subject };
}),
count: count
};
return response;
}
app.use(errorHandler);