-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
71 lines (64 loc) · 1.75 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
const axios = require("axios")
const crypto = require("crypto")
/**
* @type {import('gatsby').GatsbyNode['createPages']}
*/
exports.createPages = async ({ actions }) => {
const { createPage } = actions
createPage({
path: "/using-dsg",
component: require.resolve("./src/templates/using-dsg.js"),
context: {},
defer: true,
})
}
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions
const typeDefs = `
type AllFirstNews implements Node {
nodes: Nodes!
}
type Nodes {
id: String!
author: String!
date: String!
title: String!
url: String!
}
`
createTypes(typeDefs)
}
exports.sourceNodes = ({ actions }) => {
const { createNode } = actions
return new Promise((resolve, reject) => {
axios
.get(`https://hacker-news.firebaseio.com/v0/topstories.json`)
.then(res => res.data.slice(0, 10))
.then(res =>
res.map(post => {
axios
.get(`https://hacker-news.firebaseio.com/v0/item/${post}.json`)
.then(({ data }) => {
const postNode = {
id: `${data.id}`,
parent: `__SOURCE__`,
internal: {
type: `FirstNews`,
},
author: `${data.by}`,
date: `${data.time}`,
title: `${data.title}`,
url: `${data.url}`,
}
const contentDigest = crypto
.createHash(`md5`)
.update(JSON.stringify(postNode))
.digest(`hex`)
postNode.internal.contentDigest = contentDigest
createNode(postNode)
resolve()
})
})
)
})
}