-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgatsby-node.js
More file actions
95 lines (84 loc) · 2.15 KB
/
gatsby-node.js
File metadata and controls
95 lines (84 loc) · 2.15 KB
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
const firebase = require('firebase/app')
require('firebase/firestore')
exports.sourceNodes = async (
{ actions, createNodeId, createContentDigest },
pluginOptions
) => {
const { createTypes, createNode } = actions
const {
apiKey,
authDomain,
projectId,
storageBucket,
messagingSenderId,
appId,
measurementId,
} = pluginOptions
const commentTypeDefinition = `
type StackCheatsComments implements Node {
_id: String
_parentId: String
author: String
slug: String
content: String
createdAt: Date
}
`
createTypes(commentTypeDefinition)
let config = {
apiKey: apiKey,
authDomain: authDomain,
projectId: projectId,
storageBucket: storageBucket,
messagingSenderId: messagingSenderId,
appId: appId,
measurementId: measurementId,
}
firebase.default.initializeApp(config)
const commentCollection = await firebase.default
.firestore()
.collection(`comments`)
.get()
function commentToNode(comment, { createContentDigest, createNode }) {
const nodeMeta = {
id: createNodeId(`comments-${comment._id}`),
parent: null,
children: [],
internal: {
type: `StackCheatsComments`,
mediaType: `text/plain`,
content: JSON.stringify(comment),
contentDigest: createContentDigest(comment),
},
}
const node = Object.assign({}, comment, nodeMeta)
createNode(node)
}
commentCollection.forEach((comment) => {
var c = comment.data()
c._id = comment.id
c.createdAt = comment.data()['createdAt'].toDate()
commentToNode(c, { createNode, createContentDigest })
})
}
exports.createResolvers = ({ createResolvers }) => {
const resolver = {
Mdx: {
comments: {
type: ['StackCheatsComments'],
resolve(source, args, context, info) {
return context.nodeModel.runQuery({
query: {
filter: {
slug: { eq: source.fields.slug },
},
},
type: 'StackCheatsComments',
firstOnly: false,
})
},
},
},
}
createResolvers(resolver)
}