forked from swat-ds/friendly-networks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
197 lines (176 loc) · 4.57 KB
/
gatsby-node.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const axios = require("axios");
const apiData = require("./src/assets/data/ids_arks.json");
const component = require.resolve(`./src/components/People.js`);
const constellationData = apiData.ids_arks;
let constellations = [];
exports.sourceNodes = async ({
actions,
createNodeId,
createContentDigest,
}) => {
const { createNode } = actions;
console.log("(1)", "read");
//fetch the data
for (const i of constellationData) {
const fetchConstellation = () =>
axios.put(`https://api.snaccooperative.org`, {
command: "read",
constellationid: i.id,
});
constellations.push(await fetchConstellation());
}
console.log(constellations.length)
//Assumed data is retrieved
console.log("(2)", "retrieved");
let i = 0;
for (const c of constellations) {
const { constellation } = c.data
const constNode = {
// // Required fields
id: `${constellationData[i].id}`,
parent: `__SOURCE__`,
internal: {
type: `Constellation`, // name of the graphQL query --> allRandomUser {}
// // contentDigest will be added just after
// // but it is required
},
children: [],
// // Other fields that you want to query with graphQl
ark: constellation.ark || null,
entityType: constellation.entityType || null,
sources: constellation.sources || null,
nameEntries: constellation.nameEntries || null,
occupations: constellation.occupations || null,
biogHists: constellation.biogHists || null,
relations: constellation.relations || null,
sameAsRelations: constellation.sameAsRelations || null,
resourceRelations: constellation.resourceRelations || null,
places: constellation.places || null,
subjects: constellation.subjects || null,
genders: constellation.genders || null,
dates: constellation.dates || null,
};
// add it to userNode
constNode.internal.contentDigest = c.data.constellation.ark;
// Create node with the gatsby createNode() API
// });
createNode(constNode);
i++;
}
return;
};
//Create pages for each entity, depends on the creation of entity nodes above
exports.createPages = async ({ actions, graphql, reporter }) => {
const { createPage } = actions;
const result = await graphql(`
query Constellations {
allConstellation {
nodes {
id
ark
nameEntries {
original
components {
dataType
id
order
text
type {
term
}
}
id
}
occupations {
term {
term
}
}
entityType {
term
}
biogHists {
language {
language {
term
description
}
}
text
}
places {
confirmed
original
note
geoplace {
administrationCode
countryCode
id
latitude
longitude
name
uri
}
}
relations {
sourceArkID
targetArkID
sourceConstellation
targetConstellation
type {
term
}
content
note
id
}
sameAsRelations {
uri
}
subjects {
term {
term
}
}
genders {
term {
term
type
}
}
dates {
fromDate
fromDateOriginal
toDate
toDateOriginal
}
}
}
}
`);
if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`);
return;
}
for (const node of result.data.allConstellation.nodes) {
createPage({
path: `entities/${node.id}`,
component,
context: {
id: node.id,
ark: node.ark,
nameEntries: node.nameEntries,
occupations: node.occupations,
entityType: node.entityType,
biogHists: node.biogHists,
places: node.places,
relations: node.relations,
sameAsRelations: node.sameAsRelations,
resourceRelations: node.resourceRelations,
subjects: node.subjects,
genders: node.genders,
dates: node.dates,
},
});
}
};