-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
gatsby-node.js
234 lines (206 loc) · 6.46 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
const axios = require(`axios`)
const crypto = require(`crypto`)
const _ = require(`lodash`)
const { createRemoteFileNode } = require(`gatsby-source-filesystem`)
const { URL } = require(`url`)
const { nodeFromData } = require(`./normalize`)
// Get content digest of node.
const createContentDigest = obj =>
crypto
.createHash(`md5`)
.update(JSON.stringify(obj))
.digest(`hex`)
exports.sourceNodes = async (
{ actions, getNode, hasNodeChanged, store, cache, createNodeId },
{ baseUrl, apiBase, basicAuth, filters }
) => {
const { createNode } = actions
// Default apiBase to `jsonapi`
apiBase = apiBase || `jsonapi`
// Touch existing Drupal nodes so Gatsby doesn't garbage collect them.
// _.values(store.getState().nodes)
// .filter(n => n.internal.type.slice(0, 8) === `drupal__`)
// .forEach(n => touchNode({ nodeId: n.id }))
// Fetch articles.
// console.time(`fetch Drupal data`)
console.log(`Starting to fetch data from Drupal`)
// TODO restore this
// let lastFetched
// if (
// store.getState().status.plugins &&
// store.getState().status.plugins[`gatsby-source-drupal`]
// ) {
// lastFetched = store.getState().status.plugins[`gatsby-source-drupal`].status
// .lastFetched
// }
const data = await axios.get(`${baseUrl}/${apiBase}`, { auth: basicAuth })
const allData = await Promise.all(
_.map(data.data.links, async (url, type) => {
if (type === `self`) return
if (!url) return
if (!type) return
const getNext = async (url, data = []) => {
if (typeof url === `object`) {
// url can be string or object containing href field
url = url.href
// Apply any filters configured in gatsby-config.js. Filters
// can be any valid JSON API filter query string.
// See https://www.drupal.org/docs/8/modules/jsonapi/filtering
if (typeof filters === `object`) {
if (filters.hasOwnProperty(type)) {
url = url + `?${filters[type]}`
}
}
}
let d
try {
d = await axios.get(url, { auth: basicAuth })
} catch (error) {
if (error.response && error.response.status == 405) {
// The endpoint doesn't support the GET method, so just skip it.
return []
} else {
console.error(`Failed to fetch ${url}`, error.message)
console.log(error.data)
throw error
}
}
data = data.concat(d.data.data)
if (d.data.links.next) {
data = await getNext(d.data.links.next, data)
}
return data
}
const data = await getNext(url)
const result = {
type,
data,
}
// eslint-disable-next-line consistent-return
return result
})
)
// Make list of all IDs so we can check against that when creating
// relationships.
const ids = {}
_.each(allData, contentType => {
if (!contentType) return
_.each(contentType.data, datum => {
ids[datum.id] = true
})
})
// Create back references
const backRefs = {}
/**
* Adds back reference to linked entity, so we can later
* add node link.
*/
const addBackRef = (linkedId, sourceDatum) => {
if (ids[linkedId]) {
if (!backRefs[linkedId]) {
backRefs[linkedId] = []
}
backRefs[linkedId].push({
id: sourceDatum.id,
type: sourceDatum.type,
})
}
}
_.each(allData, contentType => {
if (!contentType) return
_.each(contentType.data, datum => {
if (datum.relationships) {
_.each(datum.relationships, (v, k) => {
if (!v.data) return
if (_.isArray(v.data)) {
v.data.forEach(data => addBackRef(data.id, datum))
} else {
addBackRef(v.data.id, datum)
}
})
}
})
})
// Process nodes
const nodes = []
_.each(allData, contentType => {
if (!contentType) return
_.each(contentType.data, datum => {
const node = nodeFromData(datum, createNodeId)
node.relationships = {}
// Add relationships
if (datum.relationships) {
_.each(datum.relationships, (v, k) => {
if (!v.data) return
if (_.isArray(v.data) && v.data.length > 0) {
// Create array of all ids that are in our index
node.relationships[`${k}___NODE`] = _.compact(
v.data.map(data => (ids[data.id] ? createNodeId(data.id) : null))
)
} else if (ids[v.data.id]) {
node.relationships[`${k}___NODE`] = createNodeId(v.data.id)
}
})
}
// Add back reference relationships.
// Back reference relationships will need to be arrays,
// as we can't control how if node is referenced only once.
if (backRefs[datum.id]) {
backRefs[datum.id].forEach(ref => {
if (!node.relationships[`${ref.type}___NODE`]) {
node.relationships[`${ref.type}___NODE`] = []
}
node.relationships[`${ref.type}___NODE`].push(createNodeId(ref.id))
})
}
if (_.isEmpty(node.relationships)) {
delete node.relationships
}
node.internal.contentDigest = createContentDigest(node)
nodes.push(node)
})
})
// Download all files.
await Promise.all(
nodes.map(async node => {
let fileNode
if (
node.internal.type === `files` ||
node.internal.type === `file__file`
) {
try {
let fileUrl = node.url
if (typeof node.uri === `object`) {
// Support JSON API 2.x file URI format https://www.drupal.org/node/2982209
fileUrl = node.uri.url
}
// Resolve w/ baseUrl if node.uri isn't absolute.
const url = new URL(fileUrl, baseUrl)
// If we have basicAuth credentials, add them to the request.
const auth =
typeof basicAuth === `object`
? {
htaccess_user: basicAuth.username,
htaccess_pass: basicAuth.password,
}
: {}
fileNode = await createRemoteFileNode({
url: url.href,
store,
cache,
createNode,
createNodeId,
parentNodeId: node.id,
auth,
})
} catch (e) {
// Ignore
}
if (fileNode) {
node.localFile___NODE = fileNode.id
}
}
})
)
nodes.forEach(n => createNode(n))
}