-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgatsby-node.js
53 lines (50 loc) · 1.42 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
const path = require('path');
const slash = require('slash');
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions;
return new Promise((resolve, reject) => {
// 1. Define set of templates that should be used
const templates = {
'category': path.resolve(`src/templates/category.js`),
'vehicle': path.resolve(`src/templates/vehicle.js`)
};
// 2. Run the graphql query to load all pages
graphql(`
{
mesh {
pages: nodes {
elements {
path
schema {
name
}
}
}
}
}
`).then(result => {
if (result.errors) {
console.log(result.errors);
}
// 3. Create pages for each loaded element.
// Use the `page.schema.name` to select the
// matching template. Discard any other page
// that can't be mapped to our templates.
// Use the `path` field from the loaded
// element to generate the page
result.data.mesh.pages.elements
.filter(page => templates[page.schema.name] != null)
.map(page => {
var template = templates[page.schema.name];
createPage({
path: page.path,
component: slash(template),
context: {
nodePath: page.path,
},
});
});
resolve();
});
});
};