-
Notifications
You must be signed in to change notification settings - Fork 43
/
source-nodes.js
42 lines (36 loc) · 949 Bytes
/
source-nodes.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
const crypto = require(`crypto`)
const R = require('ramda')
const { VK } = require('vk-io')
const { VK_GROUP_ID, VK_TOKEN } = process.env
let vk = new VK({ token: VK_TOKEN })
let sourceNodes = async ({ actions: { createNode } }) => {
let { items: albums } = await vk.api.photos.getAlbums({
owner_id: VK_GROUP_ID,
})
let albumsWithPhotos = await Promise.all(
albums.map(album =>
vk.api.photos
.get({ owner_id: VK_GROUP_ID, album_id: album.id })
.then(({ items }) => ({
...album,
photos: items,
})),
),
)
albumsWithPhotos
.map(album => ({
...album,
id: album.title,
parent: null,
children: [],
internal: {
type: 'VkAlbum',
contentDigest: crypto
.createHash(`md5`)
.update(JSON.stringify(album))
.digest(`hex`),
},
}))
.forEach(R.unary(createNode))
}
module.exports = sourceNodes