-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node-es6.js
67 lines (61 loc) · 1.67 KB
/
gatsby-node-es6.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
import fetch from 'node-fetch'
import categories from './src/utils/categoryMappings'
export const sourceNodes = async (
{ actions, createNodeId, createContentDigest },
configOptions
) => {
const { createNode } = actions
delete configOptions.plugins
const processEntity = (entityName, entity) => {
const nodeId = createNodeId(`${entityName}-${entity.id}`)
const nodeContent = JSON.stringify(entity)
const nodeData = Object.assign({}, entity, {
id: nodeId,
parent: null,
children: [],
internal: {
type: entityName,
content: nodeContent,
contentDigest: createContentDigest(entity),
},
})
return nodeData
}
const apiEndpoint = 'https://carrythroughcovid.herokuapp.com/api'
const response = await fetch(`${apiEndpoint}/businesses`)
const data = await response.json()
data.forEach(business => {
const nodeData_1 = processEntity(`Businesses`, business)
createNode(nodeData_1)
})
}
export const createPages = async ({ actions, graphql }) => {
const { data } = await graphql(`
query {
allBusinesses {
edges {
node {
name
id
slug
}
}
}
}
`)
data.allBusinesses.edges.forEach(edge => {
const slug = edge.node.slug
actions.createPage({
path: `business/${slug}`,
component: require.resolve(`./src/templates/BusinessDetailsPage.js`),
context: { slug },
})
})
categories.forEach(({ slug, name }) => {
actions.createPage({
path: `category/${slug}`,
component: require.resolve(`./src/templates/CategoryLandingPage.js`),
context: { slug, name },
})
})
}