-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgatsby-node.js
43 lines (33 loc) · 864 Bytes
/
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
const path = require('path');
const routes = require('./src/lib/routes');
const createPostError = 'Could not create post pages';
exports.createPages = async ({ actions, graphql }) => {
const { createPage } = actions;
const postTemplate = path.resolve(`./src/templates/post.js`);
const { data, errors } = await graphql(`
query {
takeshape {
getPostList {
items {
_id
title
}
}
}
}
`);
if (errors) throw errors;
const posts = data.takeshape.getPostList && data.takeshape.getPostList.items;
if ( !Array.isArray(posts) ) {
throw new Error(`${createPostError}: Invalid posts`);
}
posts.forEach(({ _id, title }) => {
createPage({
path: routes.post(title),
component: postTemplate,
context: {
postId: _id
}
});
});
};