-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
gatsby-node.js
98 lines (93 loc) · 1.75 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
const fetch = require('node-fetch');
const path = require('path');
exports.sourceNodes = async ({
actions,
createContentDigest,
createNodeId,
}) => {
// Download cities data from our remote API.
const populatedCities = await fetch(
'https://raw.githubusercontent.com/shootismoke/cities/master/all.json'
).then((r) => r.json());
populatedCities.forEach((city) => {
const node = {
...city,
id: createNodeId(`shootismoke-city-${city.slug}`),
internal: {
type: `ShootismokeCity`,
contentDigest: createContentDigest(city),
},
};
actions.createNode(node);
});
return;
};
/**
* For each hardcoded city, we create a static page. Example: For Paris, we
* create `/city/paris`
*/
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions;
// Query all cities and all their data.
return graphql(`
query AllCitiesQuery {
allShootismokeCity {
nodes {
adminName
api {
normalized {
coordinates {
latitude
longitude
}
parameter
value
lastUpdated
unit
sourceName
city
country
location
}
pm25 {
coordinates {
latitude
longitude
}
parameter
value
lastUpdated
unit
sourceName
city
country
location
}
shootismoke {
dailyCigarettes
isAccurate
}
}
country
gps {
latitude
longitude
}
name
photoUrl
slug
}
}
}
`).then((result) => {
result.data.allShootismokeCity.nodes.forEach((city) => {
createPage({
path: `/city/${city.slug}`,
component: path.resolve(`./src/templates/city.tsx`),
context: {
city,
},
});
});
});
};