-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph-data-provider.js
68 lines (61 loc) · 1.82 KB
/
graph-data-provider.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
export class GraphDataProvider {
getRandomData() {
const articleParameters = [
{ articleId: 100431, dictionary: 'Nynorskordboka', depth: 2 },
{ articleId: 68019, dictionary: 'Nynorskordboka', depth: 3 },
{ articleId: 68722, dictionary: 'Nynorskordboka', depth: 3 },
{ articleId: 18652, dictionary: 'Bokmaalsordboka', depth: 3 },
];
const randomIndex = Math.floor(Math.random() * articleParameters.length);
const { articleId, dictionary, depth } = articleParameters[randomIndex];
return this.getArticleData(articleId, dictionary, depth);
}
getArticleData(articleId, dictionary, depth) {
const url = `https://api.ordbokapi.org/graphql`;
const query = `query ArticleGraphQuery($articleId: Int!, $dictionary: Dictionary!, $depth: Int!) {
articleGraph(id: $articleId, dictionary: $dictionary, depth: $depth) {
nodes {
id
lemmas {
lemma
}
}
edges {
sourceId
targetId
}
}
}
`;
const variables = {
articleId,
dictionary,
depth,
};
return fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query, variables }),
})
.then((response) => response.json())
.then((data) => {
if (data.errors)
throw new Error(data.errors.map((error) => error.message).join('\n'));
return data;
})
.then((data) => this.convertToD3Graph(data.data.articleGraph))
.catch((error) => console.error(error));
}
convertToD3Graph(graphData) {
const nodes = graphData.nodes.map((node) => ({
id: node.id,
text: node.lemmas[0].lemma,
}));
const links = graphData.edges.map((edge) => ({
source: edge.sourceId,
target: edge.targetId,
}));
console.log({ nodes, links });
return { nodes, links };
}
}