forked from sakhisheikh/enigma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
78 lines (61 loc) · 1.88 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
72
73
74
75
76
77
78
const axios = require('axios');
const crypto = require('crypto');
const { createRemoteFileNode } = require(`gatsby-source-filesystem`)
exports.sourceNodes = async (
{ actions },
) => {
const { createNode } = actions;
// fetch raw data from the theMoviesDB api
const fetchMovies = () => axios.get(`https://api.themoviedb.org/4/list/96181?page=1&api_key=33ee74526d9cc83db3e2c3b5420f32e4`);
// await for results
const res = await fetchMovies();
// map into these results and create nodes
res.data.results.map(async (movie, i) => {
const movieNode = {
// Required fields
id: `${i}`,
parent: '__SOURCE__',
internal: {
type: 'Movie', // name of the graphQL query --> allMovie {}
// contentDigest will be added just after
// but it is required
},
children: [],
// Other fields that you want to query with graphQl
name: {
title: movie.title,
rating: movie.vote_average,
overview: movie.overview,
genres: movie.genre_ids,
releaseDate: movie.release_date,
popularity: movie.popularity,
},
url: `https://image.tmdb.org/t/p/w780${movie.poster_path}`
}
// Get content digest of node. (Required field)
const contentDigest = crypto
.createHash('md5')
.update(JSON.stringify(movieNode))
.digest('hex');
// add it to userNode
movieNode.internal.contentDigest = contentDigest;
// Create node with the gatsby createNode() API
createNode(movieNode);
});
};
exports.onCreateNode = async ({ node, actions, store, cache }) => {
if (node.internal.type !== "Movie") {
return
}
const { createNode } = actions
const fileNode = await createRemoteFileNode({
url: node.url,
store,
cache,
createNode,
createNodeId: id => `movie-image-${id}`,
})
if (fileNode) {
node.image___NODE = fileNode.id
}
}