-
Notifications
You must be signed in to change notification settings - Fork 2
/
gatsby-node.js
77 lines (69 loc) · 1.94 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
const path = require('path');
const _ = require('lodash');
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions;
const pageFields = `
edges {
node {
contentful_id
sys {
contentType {
sys {
id
}
}
}
title {
title
}
slug
}
}`;
return graphql(`
{
allContentfulLanding {
${pageFields}
}
allContentfulPage {
${pageFields}
}
allContentfulPost {
${pageFields}
}
}
`).then(result => {
if (result.errors) {
return Promise.reject(result.errors);
}
const pageNodes = _.concat(
_.map(result.data.allContentfulLanding.edges, ({node}) => node),
_.map(result.data.allContentfulPage.edges, ({node}) => node),
_.map(result.data.allContentfulPost.edges, ({node}) => node)
);
pageNodes.forEach(node => {
const template = node.sys.contentType.sys.id;
const contentfulId = node.contentful_id;
const component = path.resolve(`./src/templates/${template}.js`);
const slug = node.slug;
// if slug is not defined, don't create a page
if (!slug) {
console.error(`Error: page of type "${template}" and contentful id "${contentfulId}" does not have a "slug" field, page will not be created`);
return;
}
const pagePath = template === 'post' ? `posts/${_.trim(slug, '/')}` : slug;
const page = {
path: pagePath,
component: component,
context: {
contentfulId: contentfulId
}
};
createPage(page);
});
});
};