-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgatsby-node.js
208 lines (180 loc) · 5.87 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
const agility = require('./src/agility/utils')
let usingDefaultData = false;
exports.sourceNodes = async (args, configOptions) => {
const { actions, createNodeId, createContentDigest, getNode, getNodes, store, cache, reporter } = args;
const { createNode, deleteNode, deletePage, touchNode } = actions
const existingNodes = await getNodes().filter(n => n.internal.type.indexOf(`agility`) !== -1)
if (existingNodes.length === 0) {
console.log("Agility CMS Starter => Creating Default Content")
usingDefaultData = true
//create the default agility nodes so that the project can be built with no config
await createNode({
id: createNodeId(`sitemap-0-0`),
parent: null,
children: [],
languageCode: "en-us",
pagePath: "/",
path: "/",
menuText: "x",
pageID: 0,
internal: {
type: "agilitySitemapNode",
content: "",
contentDigest: "defaultsitemapnode"
}
});
await createNode({
id: createNodeId(`agilitypage-0-0`),
parent: null,
children: [],
languageCode: "z",
itemID: 0,
pageJson: "",
internal: {
type: "agilitypage",
content: "",
contentDigest: "agilitypage"
}
});
await createNode({
id: createNodeId(`agilityitem-0-0`),
parent: null,
children: [],
languageCode: "z",
itemID: 0,
itemJson: "",
internal: {
type: "agilityitem",
content: "",
contentDigest: "agilityitem"
}
});
await createNode({
id: createNodeId(`agilityGlobalHeader-0-0`),
parent: null,
children: [],
languageCode: "en-us",
itemid: 0,
contentID: 0,
customFields: { siteName: "x" },
properties: { referenceName: "globalheaderx" },
internal: {
type: "agilityGlobalHeader",
content: "",
contentDigest: "agilityGlobalHeader"
}
});
await createNode({
id: createNodeId(`agilityPost-0-0`),
parent: null,
children: [],
languageCode: "en-us",
itemid: 0,
contentID: 0,
customFields: {
title: "", details: "", image: {
url: "https://via.placeholder.com/350x150",
label: "Placeholder",
width: 350,
height: 150,
pixelWidth: 350,
pixelHeight: 150
}
},
sitemapNode: {},
properties: { referenceName: "postsx" },
internal: {
type: "agilityPost",
content: "",
contentDigest: "agilityPost"
}
});
}
}
exports.createPages = async (args, configOptions) => {
const { graphql, actions, getNode, createNodeId, createContentDigest, store } = args;
const { createPage, deletePage, createNode, createRedirect, createPageDependency } = actions;
if (usingDefaultData) {
createPage({
path: `/`,
component: require.resolve(`./src/BlankPage.jsx`),
context: {},
})
}
}
//gatsy-node.js
//CREATE RESOLVERS *******************************************************************************************
exports.createResolvers = (args) => {
const { createResolvers, getNode, createNodeId, createNode, createContentDigest, configOptions } = args;
const resolvers = {
//on the 'agilityPost' node type...
agilityPost: {
//get the sitemap node that represents this item - useful for retrieving the URL for the item
sitemapNode: agility.getDynamicPageItemSitemapNode(),
//[Not Implemented]
//if we had a linked content field for 'author', this is how we'd get the author for this post in a single GraphQl query
//linkedContent_agilityAuthor: agility.getLinkedContentItem({ type: 'agilityAuthor', linkedContentFieldName: 'author' })
},
//[Not Implemented]
//if we had an 'Image Slider' module and it had a list of slides via a linked content field called 'slides', this is how we'd retrieve a list of those slides in a single GraphQL query
// agilityImageSlider: {
// linkedContent_agilitySlides: agility.getLinkedContentList({ type: 'agilitySlide', linkedContentFieldName: 'slides' })
// }
}
createResolvers(resolvers)
}
exports.onCreateNode = async ({
node,
actions: { createNode },
store,
cache,
createNodeId,
}) => {
// For all Agility nodes that have an attachment field, call createRemoteFileNode
if (
node.internal.type.indexOf(`agility`) > -1 &&
node.customFields
&& (
node.internal.type.indexOf(`agilitypage`) == -1 &&
node.internal.type.indexOf(`agilitystate`) == -1 &&
node.internal.type.indexOf(`agilitysitemap`) == -1 &&
node.internal.type.indexOf(`agilitynestedsitemap`) == -1 &&
node.internal.type.indexOf(`agilitySitemapNode`) == -1 &&
node.internal.type.indexOf(`agilityitem`) == -1
)
) {
const customFields = Object.keys(node.customFields);
await asyncForEach(customFields, async (field) => {
if (node.customFields[field] !== null) {
const fieldKeys = Object.keys(node.customFields[field]);
// if(
// fieldKeys !== undefined &&
// fieldKeys !== null &&
// fieldKeys.includes(`url`) &&
// fieldKeys.includes(`pixelHeight`) &&
// fieldKeys.includes(`pixelWidth`) &&
// fieldKeys.includes(`width`) &&
// fieldKeys.includes(`height`)
// ) {
// let fileNode = await createRemoteFileNode({
// url: node.customFields[field].url, // string that points to the URL of the image
// parentNodeId: node.id, // id of the parent node of the fileNode you are going to create
// createNode, // helper function in gatsby-node to generate the node
// createNodeId, // helper function in gatsby-node to generate the node id
// cache, // Gatsby's cache
// store, // Gatsby's redux store
// })
// // if the file was created, attach the new node to the parent node
// if (fileNode) {
// node.customFields[`${field}LocalImg___NODE`] = fileNode.id
// }
// }
}
})
}
}
const asyncForEach = async (array, callback) => {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}