-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgatsby-node.js
69 lines (64 loc) · 1.68 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
const axios = require("axios")
exports.sourceNodes = (
{ actions, createNodeId, createContentDigest },
configOptions
) => {
const { createNode } = actions
const API = `https://restcountries.eu/rest/v2`
delete configOptions.plugins
return new Promise((resolve, reject) => {
// Fetch data and return items array
axios.get(`${API}/all`).then(res => {
res.data.forEach(country => {
const nodeMeta = {
id: createNodeId(`country-id-${country.numericCode}`),
parent: null,
children: [],
internal: {
type: "countries",
content: JSON.stringify(country),
contentDigest: createContentDigest(country),
},
}
const node = Object.assign({}, country, nodeMeta)
createNode(node)
})
resolve()
})
})
}
exports.createPages = ({ graphql, actions }) => {
const path = require("path")
const slug = require("slug")
const slash = require("slash")
const { createPage } = actions
return new Promise((resolve, reject) => {
graphql(`
{
allCountries {
edges {
node {
name
alpha3Code
}
}
}
}
`).then(result => {
const countryTemplate = path.resolve(`./src/templates/country-detail.js`)
result.data.allCountries.edges.forEach(({ node }) => {
createPage({
path: `country/${slug(node.name, { lower: true })}`,
component: slash(countryTemplate),
context: {
countryId: node.alpha3Code,
},
})
})
})
resolve()
}).catch(error => {
console.log(error)
reject()
})
}