-
Notifications
You must be signed in to change notification settings - Fork 6
/
gatsby-node.js
61 lines (56 loc) · 1.67 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
const { createRemoteFileNode } = require(`gatsby-source-filesystem`);
exports.createResolvers = (
{ actions, cache, createNodeId, createResolvers, store, reporter },
configOptions
) => {
const { createNode } = actions;
// Gatsby adds a configOption that's not needed for this plugin, delete it
delete configOptions.plugins;
const { images } = configOptions;
const state = store.getState();
for (let i = 0; i < images.length; ++i) {
const { schemaName, typeName, fieldName, baseUrl } = images[i];
const schema = state.schemaCustomization.thirdPartySchemas.filter(
s => s._typeMap[schemaName]
)[0];
if (!schema) {
console.warn(`Schema "${schemaName}" does not exist`);
continue;
}
const typeMap = schema._typeMap;
if (!typeMap[typeName]) {
console.warn(
`TypeName "${typeName}" does not exist in schema "${schemaName}"`
);
continue;
}
const typeEntry = typeMap[typeName];
const typeFields =
(typeEntry && typeEntry.getFields && typeEntry.getFields()) || {};
if (!typeFields[fieldName]) {
console.warn(`Field "${fieldName}" does not exist on type ${typeName}`);
continue;
}
createResolvers({
[typeName]: {
[`${fieldName}Sharp`]: {
type: "File",
resolve(source) {
const url = (baseUrl || "") + source[fieldName];
if (url) {
return createRemoteFileNode({
url,
store,
cache,
createNode,
createNodeId,
reporter
});
}
return null;
}
}
}
});
}
};