forked from howtographql/howtographql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
algoliasync.js
47 lines (44 loc) · 1.14 KB
/
algoliasync.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
const {request} = require('graphql-request')
const algoliasearch = require('algoliasearch')
//
// const query = `{
// allMarkdownRemark {
// edges {
// node {
// frontmatter {
// title
// }
// fields {
// slug
// }
// excerpt(pruneLength: 1000)
// }
// }
// }
// }`
//
// request('http://localhost:8000/___graphql', query)
// .then(data => {
// })
module.exports = {
syncToAlgolia: function syncToAlgolia(data) {
const client = algoliasearch('EGOD51Z7AV', '6c8ed811f9c39a0aeca852b94d897f2e')
const index = client.initIndex('howtographql')
const objects = data.allMarkdownRemark.edges
.map(edge => edge.node)
.map(node => ({
title: node.frontmatter.title,
objectID: node.fields.slug,
body: node.excerpt
}))
index.clearIndex((clearErr, clearContent) => {
index.saveObjects(objects, (err, content) => {
if (!err) {
console.log(`Successfully synced ${objects.length} items to Algolia`)
} else {
console.error(`Error while syncing to Algolia`, err)
}
})
})
}
}