-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
69 lines (64 loc) · 1.61 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
const { createNodeHelpers } = require('gatsby-node-helpers');
const faunadb = require('faunadb');
const { GATSBY_FAUNA_SECRET } = process.env;
const getComments = async ({ secret, reporter }) => {
try {
const q = faunadb.query;
const client = new faunadb.Client({
secret,
});
const results = await client.query(
// Search for the get-comments index and filter for data.markedSpam set to false
q.Paginate(q.Match(q.Index('get-comments'), false)),
);
// console.log(JSON.stringify(results, null, 2));
return results.data.map(([ref, date, name, parentCommentId, slug, text]) => ({
id: ref.id,
date,
name,
parentCommentId: parentCommentId || null,
slug,
text,
}));
} catch (error) {
reporter.warn(error);
}
return [];
};
exports.sourceNodes = async ({
actions,
createNodeId,
createContentDigest,
reporter,
}) => {
const nodeHelpers = createNodeHelpers({
typePrefix: 'Comment',
createNodeId,
createContentDigest,
});
const CommentEntryNode = nodeHelpers.createNodeFactory('Entry');
const { createNode, createTypes } = actions;
const typeDefs = `
type CommentEntry implements Node {
id: String
date: Date @dateformat
name: String
parentCommentId: String
text: String
slug: String
}
`;
createTypes(typeDefs);
const comments = await getComments({
secret: GATSBY_FAUNA_SECRET,
reporter,
});
if (comments !== null) {
comments.forEach(async (element) => {
const node = CommentEntryNode({
...element,
});
createNode(node);
});
}
};