-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
370 lines (353 loc) · 10.1 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
const fetch = require("node-fetch")
const path = require("path")
const { createRemoteFileNode } = require("gatsby-source-filesystem")
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions
const typeDefs = `
type campaign implements Node {
aliases: [alias] @link
deployments: [deployment] @link
dois: [doi] @link
focus_areas: [focus_area] @link
geophysical_concepts: [geophysical_concept] @link
instruments: [instrument] @link
iops: [iop] @link
partner_orgs: [partner_org] @link
platform_types: [platform_type] @link
platforms: [platform] @link
repositories: [repository] @link
seasons: [season] @link
significant_events: [significant_event] @link
websites: [website] @link
logo: image @link
bounds: String
spatial_bounds: String
}
type collection_period implements Node {
dois: [doi] @link
platform: platform @link
deployment: deployment @link
instruments: [instrument] @link
}
type deployment implements Node {
aliases: [alias] @link
campaign: campaign @link
collection_periods: [collection_period] @link
geographical_regions: [geographical_region] @link
iops: [iop] @link
significant_events: [significant_event] @link
}
type cmr_science_keyword implements Node {
Term: String
Topic: String
Category: String
VariableLevel1: String
VariableLevel2: String
VariableLevel3: String
}
type doi implements Node {
cmr_entry_title: String
doi: String
long_name: String
cmr_data_formats: [String]
cmr_science_keywords: [cmr_science_keyword]
campaigns: [campaign] @link
instruments: [instrument] @link
platforms: [platform] @link
}
type focus_area implements Node {
campaigns: [campaign] @link
}
type geographical_region implements Node {
order_priority: String
image: NasaImagesJson @link(by: "shortname", from: "short_name")
}
type instrument implements Node {
campaigns: [campaign] @link
collection_periods: [collection_period] @link
gcmd_phenomena: [gcmd_phenomenon] @link
image: image @link
measurement_type: measurement_type @link
measurement_style: measurement_style @link
measurement_regions: [measurement_region] @link
platforms: [platform] @link
repositories: [repository] @link
dois: [doi] @link
}
type platform implements Node {
campaigns: [campaign] @link
image: image @link
instruments: [instrument] @link
dois: [doi] @link
platform_type: platform_type @link
collection_periods: [collection_period] @link
}
type website implements Node {
website_type: website_type @link
}
type NasaImagesJson implements Node {
gatsbyImg: File @link(from: "fields.gatsbyImg")
}
type image implements Node {
gatsbyImg: File @link(from: "fields.gatsbyImg")
}
`
createTypes(typeDefs)
}
exports.onCreateNode = async ({
node,
actions: { createNode, createNodeField },
createNodeId,
getCache,
}) => {
if (
node.internal.type === "image" ||
(node.internal.type === "NasaImagesJson" && node.image !== null)
) {
if (node.image.includes(".gif")) return // .gif format breaks gatsby build
try {
const fileNode = await createRemoteFileNode({
url: node.image, // string that points to the URL of the image
parentNodeId: node.id, // id of the parent node of the fileNode you are going to create
createNode, // helper function in gatsby-node to generate the node
createNodeId, // helper function in gatsby-node to generate the node id
getCache,
})
// if the file was created, extend the node with "localFile"
if (fileNode) {
createNodeField({ node, name: "gatsbyImg", value: fileNode.id })
}
} catch (error) {
console.log(error)
}
}
}
exports.sourceNodes = async ({ actions, createContentDigest }) => {
const { createNode } = actions
try {
const endpoints = [
"alias",
"campaign",
"collection_period",
"deployment",
"doi",
"focus_area",
"gcmd_phenomenon",
"geographical_region",
"geophysical_concept",
"image",
"instrument",
"iop",
"measurement_type",
"measurement_style",
"measurement_region",
"partner_org",
"platform",
"platform_type",
"repository",
"season",
"significant_event",
"website",
"website_type",
]
const countProps = {
campaign_count: "campaigns",
}
const responses = []
for (const key of endpoints) {
const response = await fetchData(key)
responses.push(response)
}
responses.forEach(response => {
if (response.success) {
response.data.forEach(item => {
let additionalProps = {}
for (const countProp in countProps) {
const arrayProp = countProps[countProp]
if (item[arrayProp]) {
additionalProps[countProp] = item[arrayProp].length
}
}
createNode({
// Data for the node.
...item,
...additionalProps,
// Required fields.
id: item.uuid,
parent: null,
children: [],
internal: {
type: response.type,
contentDigest: createContentDigest(item),
description: `Creating nodes from ${response.type} endpoint`,
},
})
})
} else {
console.log("request failed for ", response.type, response.message)
}
})
} catch (error) {
console.log("catch error", error)
}
// You're done, return.
return
}
const fetchData = async endpoint => {
const response = await fetch(`https://admg.nasa-impact.net/api/${endpoint}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.ADMG_ACCESS_TOKEN}`,
},
})
const json = await response.json()
return { type: endpoint, ...json }
}
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
// **Note:** The graphql function call returns a Promise
// see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise for more info
const result = await graphql(`
{
allCampaign {
nodes {
id
short_name
}
}
allInstrument {
nodes {
id
short_name
}
}
allPlatform {
nodes {
id
short_name
}
}
allFocusArea {
nodes {
id
short_name
}
}
}
`)
result.data.allCampaign.nodes.forEach(node => {
createPage({
path: `campaign/${node.short_name}`,
component: path.resolve(`./src/templates/campaign/index.js`),
context: {
slug: node.id,
},
})
})
result.data.allInstrument.nodes.forEach(node => {
createPage({
path: `instrument/${node.short_name}`,
component: path.resolve(`./src/templates/instrument/index.js`),
context: {
slug: node.id,
},
})
})
result.data.allPlatform.nodes.forEach(node => {
createPage({
path: `platform/${node.short_name}`,
component: path.resolve(`./src/templates/platform/index.js`),
context: {
slug: node.id,
},
})
})
result.data.allFocusArea.nodes.forEach(node => {
createPage({
path: `focus/${node.short_name}`,
component: path.resolve(`./src/templates/focus/index.js`),
context: {
slug: node.id,
},
})
})
}
exports.onCreateWebpackConfig = ({ stage, loaders, actions }) => {
if (stage === "build-html" || stage === "develop-html") {
actions.setWebpackConfig({
module: {
rules: [
{
test: /mapbox-gl/,
use: loaders.null(),
},
{
test: /timelinejs/,
use: loaders.null(),
},
],
},
})
}
}
exports.createResolvers = ({ createResolvers }) => {
const resolvers = {
doi: {
cmr_data_formats: {
resolve: source => {
if (!source.cmr_data_formats) return null
// parse strings and log errors for arrays in string format
return typeof source.cmr_data_formats === "string"
? source.cmr_data_formats &&
!source.cmr_data_formats.includes("null") &&
source.cmr_data_formats.includes("['") &&
source.cmr_data_formats.includes("']")
? source.cmr_data_formats
?.split("[")[1]
.split("]")[0]
.split(",")
.map(s =>
s
.replace(/[^a-zA-Z ]/g, "")
.replace(/^\s+|\s+$|\s+(?=\s)/g, "")
)
.filter(f => f !== "")
: []
: source.cmr_data_formats?.filter(f => f !== "")
},
},
cmr_science_keywords: {
resolve: source => {
if (
source.cmr_science_keywords &&
typeof source.cmr_science_keywords === "string"
) {
// The doi keywords are stored as a JSON string in the database
// parsing them here into an object
let keywords = []
try {
keywords = JSON.parse(source.cmr_science_keywords).filter(
keyword => keyword.term && keyword.term !== "Not provided"
)
} catch (e) {
if (source.cmr_science_keywords !== null) {
console.error(
`ERROR: Could not parse ${source.cmr_science_keywords}`
)
}
}
return keywords
}
return source.cmr_science_keywords
},
},
},
}
createResolvers(resolvers)
}