-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
91 lines (79 loc) · 2.29 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
const Utils = require('./src/utils');
const { GraphQLClient } = require('graphql-request');
const { createRemoteFileNode } = require(`gatsby-source-filesystem`);
let node_type = [];
exports.sourceNodes = async (
{ actions, createNodeId, createContentDigest, options },
{ url, apiToken, workspaceId, graphqlQuery},
) => {
const { createNode } = actions;
//Connect to workspace 8base
const client = new GraphQLClient(`${url}/${workspaceId}`, {
headers: {
Authorization: `Bearer ${apiToken}`,
},
});
// Request graphql query
const query = await client.request(graphqlQuery);
const data = await query;
//I extract the name from the answer query and assign the prefix 8Base
Object.keys(data).map((value, i) => {
node_type.push(`${value}8Base`)
});
// Process data into nodes.
Object.keys(data).map((value, i) => {
data[value].items.map((dataValue) => {
//validate if objects
let resp = Utils.validateObject(dataValue, {});
if (resp.validate) {
dataValue['validate'] = resp.validate;
}
const nodeContent = JSON.stringify(dataValue);
const nodeMeta = {
id: createNodeId(`${node_type[i]}-${dataValue.id}`),
parent: null,
children: [],
internal: {
type: node_type[i],
content: nodeContent,
contentDigest: createContentDigest(dataValue),
},
};
const node = Object.assign({}, dataValue, nodeMeta);
createNode(node);
});
});
return;
};
exports.onCreateNode = async ({
node, // the node that was just created
actions: { createNode, createNodeField },
createNodeId,
getCache
}) => {
if (node_type.includes(node.internal.type)) {
//if exists image, create remoteUrl
if (node.validate) {
const images = await Promise.all(
node.imageUrl.items.map(data => {
return createRemoteFileNode({
url: data.downloadUrl,
parentNodeId: node.id,
createNode,
createNodeId,
getCache,
})
})
);
// Field with image list
await createNodeField({
node,
name: "images",
value: images,
});
node.fields.images.forEach((image, i) => {
image.remoteImage___NODE = images[i].id
})
}
}
};