-
Notifications
You must be signed in to change notification settings - Fork 2
/
gatsby-node.js
297 lines (277 loc) · 7.41 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
const axios = require("axios");
const constellationData = require("./content/constellationsForInclusion.json");
const PeopleComponent = require.resolve(`./src/components/Person.js`);
const bgComponent = require.resolve(`./src/templates/BgComponent.js`);
const { createFilePath } = require(`gatsby-source-filesystem`);
const path = require("path");
//Creates nodes for the markdown, which are used later to create pages.
exports.onCreateNode = ({ node, getNode, actions }) => {
//called when node created/updated
const { createNodeField } = actions;
if (node.internal.type === `MarkdownRemark`) {
const slug = createFilePath({ node, getNode, basePath: `pages` });
createNodeField({
//adds slug to pages graphql query -> allMarkdownRemark { edges { node { fields { slug }}}}
node,
name: `slug`,
value: slug,
});
}
};
// Pull in constellation data and add to GraphQL
let constellations = [];
exports.sourceNodes = async ({actions,createNodeId, createContentDigest}) => {
const { createNode } = actions;
console.log("(1)", "read");
//fetch the data
let x = 0;
for (const i of constellationData) {
const fetchConstellation = () =>
axios.get(`https://api.snaccooperative.org`, {
data: {
command: "read",
constellationid: i["SNAC ID"],
},
// timeout: 10000,
});
let result = await fetchConstellation()
if("constellation" in result.data){
x++;
constellations.push(result);
}
}
console.log(constellations.length)
console.log(x);
//Assumed data is retrieved
console.log("(2)", "retrieved");
let i = 0;
constellations.forEach(c => {
const { constellation } = c.data
const constNode = {
// // Required fields
id: `${constellationData[i]["SNAC 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
arkId: constellation.ark.split("/").pop() || null,
triCoID: constellationData[i]["TriCo ID"] || null,
mentions: parseInt(constellationData[i]["Mntns"]) || 0,
imageSrc: constellationData[i]["ImgSrc"] || 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 {
allCetei {
nodes {
prefixed
elements
parent {
... on File {
name
}
}
}
}
allConstellation {
nodes {
id
arkId
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
}
role {
term
}
}
relations {
sourceArkID
targetArkID
sourceConstellation
targetConstellation
type {
term
}
content
note
id
}
sameAsRelations {
uri
}
subjects {
term {
term
}
}
genders {
term {
term
type
}
}
dates {
fromDate
fromDateOriginal
fromType {
term
}
toDate
toDateOriginal
toType {
term
}
}
}
}
}
`);
if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`);
return;
}
/////////// Create entity pages ///////////
let arks = []
for (const node of result.data.allConstellation.nodes){
arks.push(node.arkId);
}
console.log(arks.length);
for (const node of result.data.allConstellation.nodes) {
createPage({
path: `people/${node.arkId}`,
component: PeopleComponent,
context: {
id: node.id,
arkId: node.arkId,
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,
allArks: arks,
arkRegex: `/${node.arkId}/`,
},
});
}
/////////// Create document pages ///////////
const component = require.resolve(`./src/gatsby-theme-ceteicean/components/Ceteicean.tsx`)
for (const node of result.data.allCetei.nodes) {
const name = node.parent.name;
createPage({
path: "writings/" + name,
component,
context: {
name,
prefixed: node.prefixed,
elements: node.elements
}
})
}
/////////// Create Markdown pages ///////////
return (
graphql(`
{
allMarkdownRemark {
edges {
node {
frontmatter {
slug
title
}
html
rawMarkdownBody
}
}
}
}
`).then((result) => {
if (result.errors) {
return Promise.reject(result.errors);
}
result.data.allMarkdownRemark.edges.forEach(({ node }) => {
createPage({
path: node.frontmatter.slug,
component: bgComponent,
context: {
body: node.html,
rawBody: node.rawMarkdownBody,
title: node.frontmatter.title
}, // additional data can be passed via context
});
});
})
);
};