-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
64 lines (57 loc) · 1.66 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
const fetch = require("node-fetch");
const tinyRick = require("rickmortyapi"); // https://github.com/afuh/rick-and-morty-api-node
const NODE_TYPE_CHARACTER = "Character";
const NODE_TYPE_EPISODE = "Episode";
const NODE_TYPE_LOCATION = "Location";
exports.sourceNodes = async ({
actions,
createNodeId,
createContentDigest,
}) => {
const { createNode } = actions;
const characters = await tinyRick.getCharacter([]);
const episodes = await tinyRick.getEpisode([]);
const locations = await tinyRick.getLocation([]);
const resultsCharacters = characters.results;
const resultsEpisodes = episodes.results;
const resultsLocations = locations.results;
resultsCharacters.forEach((node, index) => {
createNode({
...node,
id: createNodeId(`${NODE_TYPE_CHARACTER}-${node.id}`),
parent: null,
children: [],
internal: {
type: NODE_TYPE_CHARACTER,
content: JSON.stringify(node),
contentDigest: createContentDigest(node),
},
});
});
resultsEpisodes.forEach((node, index) => {
createNode({
...node,
id: createNodeId(`${NODE_TYPE_EPISODE}-${node.id}`),
parent: null,
children: [],
internal: {
type: NODE_TYPE_EPISODE,
content: JSON.stringify(node),
contentDigest: createContentDigest(node),
},
});
});
resultsLocations.forEach((node, index) => {
createNode({
...node,
id: createNodeId(`${NODE_TYPE_LOCATION}-${node.id}`),
parent: null,
children: [],
internal: {
type: NODE_TYPE_LOCATION,
content: JSON.stringify(node),
contentDigest: createContentDigest(node),
},
});
});
};