-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
158 lines (146 loc) · 3.23 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
require("dotenv").config()
const path = require("path")
const moment = require("moment")
const paginator = require("./src/utils/paginator")
exports.createPages = async ({ actions: { createPage }, graphql }) => {
const { data } = await graphql(`
query {
categories: allGraphCmsCategory(filter: { stage: { eq: PUBLISHED } }) {
nodes {
id
stage
slug
name
}
}
pages: allGraphCmsPage(filter: { stage: { eq: PUBLISHED } }) {
nodes {
id
content {
markdownNode {
childMdx {
body
}
}
}
keywords
slug
subtitle
title
}
}
posts: allGraphCmsPost(
sort: { fields: date, order: DESC }
filter: { stage: { eq: PUBLISHED } }
) {
edges {
nextPost: next {
id
slug
title
}
page: node {
id
author {
id
name
title
}
content {
markdownNode {
childMdx {
body
}
}
}
date: formattedDate
productId
excerpt
keywords
slug
title
categories {
name
slug
}
tags
updatedAt
}
previousPost: previous {
id
slug
title
}
}
}
}
`)
if (data.errors) {
throw data.errors
}
const categories = {}
data.posts.edges.forEach(({ nextPost, page, previousPost }) => {
if (page.categories) {
page.categories.forEach(category => {
if (categories[category.slug]) {
categories[category.slug] += 1
} else {
categories[category.slug] = 1
}
})
}
createPage({
component: path.resolve("./src/templates/post.jsx"),
context: {
id: page.id,
page,
previousPost,
nextPost,
previousId: previousPost ? previousPost.id : null,
nextId: nextPost ? nextPost.id : null,
productId: page.productId,
type: "post",
},
path: `/${page.slug}`,
})
})
data.pages.nodes.forEach(page => {
createPage({
component: path.resolve("./src/templates/page.jsx"),
context: {
page,
type: "page",
},
path: `/pages/${page.slug}`,
})
})
data.categories.nodes.forEach(page => {
paginator.listing(createPage, {
limit: 15,
count: categories[page.slug],
page,
slug: "categories",
})
})
paginator.articles(createPage, {
limit: 15,
count: data.posts.edges.length,
slug: "articles",
})
}
exports.createResolvers = ({ createResolvers }) => {
const resolvers = {
GraphCMS_Post: {
formattedDate: {
type: "String",
resolve: source => {
const date = new Date(source.date)
const m = moment(date)
m.locale(process.env.LOCALE)
return m.format("l")
},
},
},
}
createResolvers(resolvers)
}