-
Notifications
You must be signed in to change notification settings - Fork 2
/
gatsby-node.js
110 lines (102 loc) · 2.8 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
const kebabcase = require("lodash.kebabcase")
module.exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions
createTypes(
`
interface BlogPost @nodeInterface {
id: ID!
title: String!
slug: String!
content: String!
}
type MarkdownRemark implements BlogPost & Node {
title: String! @proxy(from: "frontmatter.title")
slug: String! @proxy(from: "fields.slug")
content: String!
}
type ContentfulBlogPost implements BlogPost & Node {
title: String!
slug: String!
content: String!
}
`
)
}
module.exports.createResolvers = ({ createResolvers }) => {
createResolvers({
MarkdownRemark: {
content: {
resolve: async (source, args, context, info) => {
const fieldName = "html"
const type = info.schema.getType(`MarkdownRemark`)
const resolver = type.getFields()[fieldName].resolve
const result = await resolver(source, args, context, {
fieldName,
})
return result
},
},
},
ContentfulBlogPost: {
content: {
resolve: async (source, args, context, info) => {
const node = context.nodeModel.getNodeById({
id: source.body___NODE,
})
const fieldName = "childMarkdownRemark"
const type = info.schema.getType(`contentfulBlogPostBodyTextNode`)
const resolver = type.getFields()[fieldName].resolve
const result = await resolver(node, args, context, {
fieldName,
})
const markdownRemarkType = info.schema.getType(`MarkdownRemark`)
const markdownRemarkResolver = markdownRemarkType.getFields()["html"]
.resolve
const markdownRemarkResult = await markdownRemarkResolver(
result,
args,
context,
{
fieldName: "html",
}
)
return markdownRemarkResult
},
},
},
})
}
module.exports.onCreateNode = ({ node, actions }) => {
const { createNodeField } = actions
if (node.internal.type === `MarkdownRemark`) {
createNodeField({
node,
name: "slug",
value: kebabcase(node.frontmatter.title),
})
}
}
module.exports.createPages = async ({ actions, graphql }) => {
const { createPage } = actions
const blogPostTemplate = require.resolve(`./src/templates/blog-post.js`)
const { data } = await graphql(`
{
allBlogPost(filter: { title: { ne: "" } }) {
nodes {
title
slug
}
}
}
`)
data.allBlogPost.nodes.forEach(post => {
createPage({
name: post.title,
path: `/blog-posts/${post.slug}`,
component: blogPostTemplate,
context: {
title: post.title,
},
})
})
}